var pg = require('pg'), config = require('../config'); var DATABASE_URI = process.env.DATABASE_URL || ('postgres://postgres@localhost/' + config.name) var db = module.exports = {} db.statements = [] pg.connect(DATABASE_URI, ready); function ready (err, client, done){ if (err) { return console.error('error fetching client from pool', err); } db.client = client db.done = done db.run() } db.query = function (statement) { db.statements.push(statement) } db.run = function(){ var statement = db.statements.shift() if (! statement) { return db.done() } db.client.query('SELECT $1::int AS number', ['1'], function(err, result) { console.log(result) db.done(); }); }