mirror of
https://github.com/brianc/node-postgres.git
synced 2026-02-01 16:47:23 +00:00
Add callback interface to pool#query (#11)
* Add callback interface to pool#query * Fix linting errors
This commit is contained in:
parent
8b45ea1e7d
commit
ce59164ba1
5
index.js
5
index.js
@ -83,13 +83,16 @@ Pool.prototype.connect = function (cb) {
|
|||||||
|
|
||||||
Pool.prototype.take = Pool.prototype.connect
|
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) {
|
return new this.Promise(function (resolve, reject) {
|
||||||
this.connect(function (err, client, done) {
|
this.connect(function (err, client, done) {
|
||||||
if (err) return reject(err)
|
if (err) return reject(err)
|
||||||
client.query(text, values, function (err, res) {
|
client.query(text, values, function (err, res) {
|
||||||
done(err)
|
done(err)
|
||||||
err ? reject(err) : resolve(res)
|
err ? reject(err) : resolve(res)
|
||||||
|
if (cb) {
|
||||||
|
cb(err, res)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}.bind(this))
|
}.bind(this))
|
||||||
|
|||||||
@ -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) {
|
it('removes client if it errors in background', function (done) {
|
||||||
const pool = new Pool()
|
const pool = new Pool()
|
||||||
pool.connect(function (err, client, release) {
|
pool.connect(function (err, client, release) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user