Bundled templates

Zazler includes the following bundled formats:

Format Description
html Most simple HTML output.
Note: The header can be turned out using opts=-head.
pivot.html Pivoted html table.
min.datatable Table based on datatable js library
json JSON output as an array.
Note: Adding opts=-map will get data as list.
alldata.json JSON output with full information (result object)
xml XML output that adds XSD.
Note: XSD is a separate format.
tsv Tab separated values.
Notes: Be careful not to forget that TSV doesn't escape content!
The header can be turned out using opts=-head.
csv Comma separated values.
Notes: Be careful not to forget that CSV doesn't escape content!
The header can be turned out using opts=-head.
tree.json Makes tree using foreign keys. See example
dashboard Provides an overview of the database that comes from _schema table.
Note: This is the only bundled format that depends on the structure of the table.

Custom Templates

Let's create template jsonp.js:

var cb = vars.callback || "callback";
print(cb + "(" + JSON.stringify(result.data) + ");");

To use it, say where your templates are. Like this:

zazler = require('zazler');
zazler.conf({  templates: [ "/home/me/templates/" ] });

Now is jsonp useable for all connected databases and datasets. It's called as http://host/db/table.jsonp?callback=myfn (if you want use extension jsonp.js on URL, filename must be jsonp.js.js).

Template is javascript that has some special functions. Most importantly print. At the same time, template has no access to default nodejs API by default. Therefore template can't do anything harmful to operating system. You can achieve it by passing functions from nodejs to templates.

Available objects:

  • result – query results
    • data – all results in array
    • dataK – all results by key
    • cols – column headers
    • types – result general types in array ("str", "int", "float", "bool", "date", "datetime" or "time")
    • rawTypes – column types that is described by database
    • rowsTotal() – results rows if limit is omitted
    • format(_fmt_) – results are passed to other template and result is returned
  • req – request related data
    • user – username (basic auth)
    • dbName – database name (it is not original database name but name that is used on zazler.db call
    • table – tablename
    • format – what is formatname
    • pipe – what pipe
    • isMain – true if it is query is not requested from another template
    • vars – request related variables (GET parameters)
    • opts – all flags from opts
    • post – posted data
  • zazler
    • version – zazler version
  • vars – variables in that request (they may come from URL or config)

Available functions:

  • print – sends output
  • query – can make new query to database
  • post – can post to database
  • newDb – create temporary database (sqlite :memory: database)

Database communication

Queries to database can be done in very similar way as on URL. You have all same access limitation as from URL.

// gives result object
subq = query("table", { where: "id=3" });
// gives xml result from users where name begins with 'A'
subXml = query("users.xml", { where: "like(name,N)", N: "A%" });
// get all data from another connected database table
altDb = query("otherdb/table");

Similarly post can made. Post has three parameters where first and second parameters are same as for query (post also results data if returning is defined in database setup. Third parameter is posted data.

post("log", {}, [ { format: req.format, table: req.table } ])
// if returning is defined, you may want use post like this
newId = post("foo", {select: id}, [{ bar: "..." }]).data[0][0];

Additional functionality

Templates, as said, are pure – they are ment only for formatting. Additional functions can be provided by nodejs. Functions are added to database connection and are available to all templates.

var zazler = require('zazler');
var example = zazler.db("example", "sqlite:///tmp/db", { read: "*" })
var counter = 0;
example.export.addCount = function (reqData, nr) {
  counter += nr || 1;
  return counter;

In templates you have function addCount. In exported function first argument is event data and calling arguments are following. Event data is similar to data available in template but includes sensitive data like user passwords, more information about database etc.

  • zazler
    • version
  • db
    • name – database name as it is in database
    • driver
    • version
  • req – all that is template req and adds:
    • pass – user password if authenticated

Temporary database

It's possible to use temporary sqlite database in templates. It can be handy tool to work with data.

Here is short example:

foo = newDb();
foo.newTable("alfa", [ { a: 1, b: 2 } ])
print(JSON.stringify( foo.query("alfa", { select: "a" }) ));