Skip to content

Commit ce59164

Browse files
authored
Add callback interface to pool#query (brianc#11)
* Add callback interface to pool#query * Fix linting errors
1 parent 8b45ea1 commit ce59164

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,16 @@ Pool.prototype.connect = function (cb) {
8383

8484
Pool.prototype.take = Pool.prototype.connect
8585

86-
Pool.prototype.query = function (text, values) {
86+
Pool.prototype.query = function (text, values, cb) {
8787
return new this.Promise(function (resolve, reject) {
8888
this.connect(function (err, client, done) {
8989
if (err) return reject(err)
9090
client.query(text, values, function (err, res) {
9191
done(err)
9292
err ? reject(err) : resolve(res)
93+
if (cb) {
94+
cb(err, res)
95+
}
9396
})
9497
})
9598
}.bind(this))

test/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ describe('pool', function () {
3232
})
3333
})
3434

35+
it('can run a query with a callback', function (done) {
36+
const pool = new Pool()
37+
pool.query('SELECT $1::text as name', ['brianc'], function (err, res) {
38+
expect(res.rows[0]).to.eql({ name: 'brianc' })
39+
pool.end(function () {
40+
done(err)
41+
})
42+
})
43+
})
44+
3545
it('removes client if it errors in background', function (done) {
3646
const pool = new Pool()
3747
pool.connect(function (err, client, release) {

0 commit comments

Comments
 (0)