Predefined queries

To make APIs beautiful, give meaningful parameters and hide bussiness logic there are predefined queries aka "alias". They may seem quite similar to SQL views.

zazler.db("dbname", "mysql://locahost/db", { read: "dbtable",
  alias:
  [ {
     name: "alpha",   
     table: "dbtable"
     vars: {         
       select: "smth"
       "where.del": "deleted=false"
     },
     const: {
       "where.id": "id=KEY" // if KEY is not defined here, it must be applied to URL
     },
     require: { 
       "KEY": "Parameter KEY is missing"
     }
  } ]
})

Explanations:

  • name says how to access this query, in this example http://host/db/alpha.json (whatever format, of course)
  • table is SQL tablename
  • vars any variable to build query or pass to templates
  • const like vars but are not overrided by URL variables
  • require parameter can be used to return nice error messages if some parameter is missing. In this example, KEY is what must be defined and "Parameter KEY is missing" is error message showed to used if no parameter given.

Here is used multiple where conditions. where.whatever adds additional where condition. whatever part has no meaning.

Notice that with this alias it's not possible to query without id=? condition and by default answer is empty if field deleted is true. But user can override deleted condition if he knows how to do it. User can aslo define custom fields what to query. This goes like this:

http://host/db/alpha.json?KEY=3&where.del=true&select=a,b,c

You can prevent that if you put select and where.del to const section.