Add callback interface to pool#query (#11)

* Add callback interface to pool#query

* Fix linting errors
This commit is contained in:
Brian C 2016-06-24 11:35:16 -05:00 committed by GitHub
parent 8b45ea1e7d
commit ce59164ba1
2 changed files with 14 additions and 1 deletions

View File

@ -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))

View File

@ -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) {