From ce59164ba1bc4932594be7f4cae5ab52d4576f2d Mon Sep 17 00:00:00 2001 From: Brian C Date: Fri, 24 Jun 2016 11:35:16 -0500 Subject: [PATCH] Add callback interface to pool#query (#11) * Add callback interface to pool#query * Fix linting errors --- index.js | 5 ++++- test/index.js | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ee09dc81..042d697b 100644 --- a/index.js +++ b/index.js @@ -83,13 +83,16 @@ Pool.prototype.connect = function (cb) { Pool.prototype.take = Pool.prototype.connect -Pool.prototype.query = function (text, values) { +Pool.prototype.query = function (text, values, cb) { return new this.Promise(function (resolve, reject) { this.connect(function (err, client, done) { if (err) return reject(err) client.query(text, values, function (err, res) { done(err) err ? reject(err) : resolve(res) + if (cb) { + cb(err, res) + } }) }) }.bind(this)) diff --git a/test/index.js b/test/index.js index 956f15cc..a7772b61 100644 --- a/test/index.js +++ b/test/index.js @@ -32,6 +32,16 @@ describe('pool', function () { }) }) + it('can run a query with a callback', function (done) { + const pool = new Pool() + pool.query('SELECT $1::text as name', ['brianc'], function (err, res) { + expect(res.rows[0]).to.eql({ name: 'brianc' }) + pool.end(function () { + done(err) + }) + }) + }) + it('removes client if it errors in background', function (done) { const pool = new Pool() pool.connect(function (err, client, release) {