Skip to content

Commit cb21c2a

Browse files
committed
Better compatibility with pg
1 parent 36d50ec commit cb21c2a

File tree

3 files changed

+11
-25
lines changed

3 files changed

+11
-25
lines changed

index.js

+8-20
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,6 @@ Pool.prototype._create = function (cb) {
4141
this.log('client connection error:', err)
4242
cb(err)
4343
}
44-
45-
var query = client.query;
46-
47-
client.queryAsync = function (text, values) {
48-
return new this.Promise((resolve, reject) => {
49-
client.query(text, values, function (err, res) {
50-
err ? reject(err) : resolve(res)
51-
})
52-
})
53-
}.bind(this)
54-
5544
cb(err, err ? null : client)
5645
}.bind(this))
5746
}
@@ -92,16 +81,15 @@ Pool.prototype.connect = function (cb) {
9281
Pool.prototype.take = Pool.prototype.connect
9382

9483
Pool.prototype.query = function (text, values) {
95-
return this.take().then(function (client) {
96-
return client.queryAsync(text, values)
97-
.then(function (res) {
98-
client.release()
99-
return res
100-
}).catch(function (error) {
101-
client.release(error)
102-
throw error
84+
return new this.Promise(function (resolve, reject) {
85+
this.connect(function (err, client, done) {
86+
if (err) return reject(err)
87+
client.query(text, values, function (err, res) {
88+
done(err)
89+
err ? reject(err) : resolve(res)
10390
})
104-
})
91+
})
92+
}.bind(this))
10593
}
10694

10795
Pool.prototype.end = function (cb) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"expect.js": "0.3.1",
3232
"lodash": "4.13.1",
3333
"mocha": "^2.3.3",
34-
"pg": "4.5.6",
34+
"pg": "5.1.0",
3535
"standard": "7.1.2",
3636
"standard-format": "2.2.1"
3737
},

test/index.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var expect = require('expect.js')
22
var co = require('co')
3-
var Promise = require('bluebird')
43
var _ = require('lodash')
54

65
var describe = require('mocha').describe
@@ -59,7 +58,7 @@ describe('pool', function () {
5958
var pool = new Pool()
6059
var client = yield pool.connect()
6160
expect(pool.pool.availableObjectsCount()).to.be(0)
62-
var res = yield client.queryAsync('select $1::text as name', ['hi'])
61+
var res = yield client.query('select $1::text as name', ['hi'])
6362
expect(res.rows).to.eql([{ name: 'hi' }])
6463
client.release()
6564
expect(pool.pool.getPoolSize()).to.be(1)
@@ -69,10 +68,9 @@ describe('pool', function () {
6968

7069
it('properly pools clients', co.wrap(function * () {
7170
var pool = new Pool({ poolSize: 9 })
72-
var count = 0
7371
yield _.times(30).map(function * () {
7472
var client = yield pool.connect()
75-
var result = yield client.queryAsync('select $1::text as name', ['hi'])
73+
yield client.query('select $1::text as name', ['hi'])
7674
client.release()
7775
})
7876
expect(pool.pool.getPoolSize()).to.be(9)

0 commit comments

Comments
 (0)