1.1.1 • Public • Published


Query Templates for Postgres in Node.js


Better SQL strings in NodeJS In Node.js, if you're writing SQL by hand (i.e. without an ORM, mongoDB or jQuery-styled query builder), then multiline strings and callbacks can be difficult to build and maintain.

Lets take a common SQL query using the excellent node-postgres package:

"insert into sample_table "+
"    (obj_name, obj_description, creator_user_id, some_number, json_column) "+
"  values "+
" ($1, $2, $3, $4, $5)",
[obj_name, obj_desc, req.session.user_id, num_array[0]],
function(err, results) {
  //do something with results

This contrasts with the new SQL statement syntax we've implemented:

var insertResult = yield pgClient.queryTmpl(sql`
  insert into sample_table
    (obj_name, obj_description, creator_user_id, some_number, json_column)
    (${obj_name},  ${obj_desc}, ${req.session.user_id}, ${num_array[0]}


Further inspection of the first query:

The first annoyance and notable contrast between the two is the strinct concatenation of the former. It makes it hard to take a plain .sql query and drop it into node.js, or debug in psql. There are other style syntaxes possible, such as :

insert into sample_table \

but it still is fragile. An extra space after newline escape breaks the whole query and is invisible to see.

Also, existing asyncronous methods such as callbacks (as seen above), the async package, or Promises are harder to deal with for a complex series of sql statements. They are not necessarily unweidly, but it certainly takes extra control structure than syncronous code.

Another issue is $1, $2, ..., $N can be hard to maintain in a larger query. What if you want a new parameter near the beginning (for logic readability)?

They could be renamed $2 to $3, $3 to $4, etc. Or of course have them out of order. Another option is to have a counter and litter the query building code ahead of time:


How to improve?

New Javascript features allow for a better design:

  • Template Strings - allows for multiline strings as well as variable binding. (docs) Generators - functions that allow syncronous-style programming, but still preserve asyncronous i/o handling. (docs)

  • Generators The "yield" keyword in the first line allows the query to run asyncronously, but the code to pause at this line until the insert finishes. That means Node is still non-blocking and other functions can be run in parallel as well as other requests to this function.

We're using the Bluebird Promises library to wrap node-postgres into a Promise:

var pg = Promise.promisifyAll(require('pg'));
and running this sql statement in a generator function:

exports.create = Promise.coroutine(function *(inputData) {

// some code here to validate incoming data

var insertResult = yield app.pgClient.query(sql`
  ) `

Template Strings

First, the backquote character (`) is what allows multi-line strings. Its the one that shares the key with tilde (~) :}

The special trick to making this all work is the 'sql' word before the template string. This is called a "Tagged Template", which you can read more about here:

Basically, we need to convert the template string into a prepared statement (to prevent SQL-Injection) via a custom tagged template:

  var sql = function(pieces) {
  var result = '';
  var vals = [];
  var substitutions = [], 1);  
  for (var i = 0; i < substitutions.length; ++i) {
    result += pieces[i] + '$' + (i + 1);

  result += pieces[substitutions.length];
  return {query: result, values: vals};

From there, we need to convert this object into the string and array paremeters to the original query function:

  pg.Client.prototype.queryTmpl = function(obj) {
    return, obj.query, obj.values);

A few notes:

  • You might have noticed the array lookup in the sql statement:


    Yes, you can do all sorts of things like function lookups:


    or truth tests:

    ${num_array[0] === 1}

    and also nested javascript objects can be used:


  • The code will error out if any variable is not instantiated (but an undefined property of an object is alright).



Package Sidebar


npm i pg-query-template

Weekly Downloads






Unpacked Size

26.1 kB

Total Files


Last publish


  • ivc-user