Skip to content

Commit ad73407

Browse files
committed
Initial commit
1 parent c0d3905 commit ad73407

File tree

5 files changed

+70
-53
lines changed

5 files changed

+70
-53
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
npm-debug.log

Makefile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.PHONY: jshint test publish-patch test
2+
3+
test:
4+
npm test
5+
6+
patch: test
7+
npm version patch -m "Bump version"
8+
git push origin master --tags
9+
npm publish
10+
11+
minor: test
12+
npm version minor -m "Bump version"
13+
git push origin master --tags
14+
npm publish

index.js

+23-23
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ var util = require('util')
33
var EventEmitter = require('events').EventEmitter
44
var debug = require('debug')
55

6-
var Pool = module.exports = function(options) {
6+
var Pool = module.exports = function (options, Client) {
77
EventEmitter.call(this)
88
this.options = options || {}
99
this.log = this.options.log || debug('pg:pool')
10-
this.Client = this.options.Client || require('pg').Client
10+
this.Client = this.options.Client || Client || require('pg').Client
1111
this.Promise = this.options.Promise || Promise
1212

1313
this.options.max = this.options.max || this.options.poolSize || 10
@@ -18,33 +18,33 @@ var Pool = module.exports = function(options) {
1818

1919
util.inherits(Pool, EventEmitter)
2020

21-
Pool.prototype._destroy = function(client) {
21+
Pool.prototype._destroy = function (client) {
2222
if (client._destroying) return
2323
client._destroying = true
2424
client.end()
2525
}
2626

27-
Pool.prototype._create = function(cb) {
27+
Pool.prototype._create = function (cb) {
2828
this.log('connecting new client')
2929
var client = new this.Client(this.options)
3030

31-
client.on('error', function(e) {
31+
client.on('error', function (e) {
3232
this.log('connected client error:', e)
3333
this.pool.destroy(client)
3434
e.client = client
3535
this.emit('error', e)
3636
}.bind(this))
3737

38-
client.connect(function(err) {
38+
client.connect(function (err) {
3939
this.log('client connected')
4040
if (err) {
41-
this.log('client connection error:', e)
41+
this.log('client connection error:', err)
4242
cb(err)
4343
}
4444

45-
client.queryAsync = function(text, values) {
45+
client.queryAsync = function (text, values) {
4646
return new this.Promise((resolve, reject) => {
47-
client.query(text, values, function(err, res) {
47+
client.query(text, values, function (err, res) {
4848
err ? reject(err) : resolve(res)
4949
})
5050
})
@@ -54,21 +54,21 @@ Pool.prototype._create = function(cb) {
5454
}.bind(this))
5555
}
5656

57-
Pool.prototype.connect = function(cb) {
58-
return new this.Promise(function(resolve, reject) {
57+
Pool.prototype.connect = function (cb) {
58+
return new this.Promise(function (resolve, reject) {
5959
this.log('acquire client begin')
60-
this.pool.acquire(function(err, client) {
60+
this.pool.acquire(function (err, client) {
6161
if (err) {
6262
this.log('acquire client. error:', err)
6363
if (cb) {
64-
cb(err, null, function() { })
64+
cb(err, null, function () {})
6565
}
6666
return reject(err)
6767
}
6868

6969
this.log('acquire client')
7070

71-
client.release = function(err) {
71+
client.release = function (err) {
7272
if (err) {
7373
this.log('release client. error:', err)
7474
this.pool.destroy(client)
@@ -89,26 +89,26 @@ Pool.prototype.connect = function(cb) {
8989

9090
Pool.prototype.take = Pool.prototype.connect
9191

92-
Pool.prototype.query = function(text, values) {
93-
return this.take().then(function(client) {
92+
Pool.prototype.query = function (text, values) {
93+
return this.take().then(function (client) {
9494
return client.queryAsync(text, values)
95-
.then(function(res) {
95+
.then(function (res) {
9696
client.release()
9797
return res
98-
}).catch(function(error) {
98+
}).catch(function (error) {
9999
client.release(error)
100100
throw error
101101
})
102102
})
103103
}
104104

105-
Pool.prototype.end = function(cb) {
105+
Pool.prototype.end = function (cb) {
106106
this.log('draining pool')
107-
return new this.Promise(function(resolve, reject) {
108-
this.pool.drain(function() {
107+
return new this.Promise(function (resolve, reject) {
108+
this.pool.drain(function () {
109109
this.log('pool drained, calling destroy all now')
110-
this.pool.destroyAllNow(function() {
111-
if(cb) {
110+
this.pool.destroyAllNow(function () {
111+
if (cb) {
112112
cb()
113113
}
114114
resolve()

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "pg-pool",
3-
"version": "0.0.1",
3+
"version": "1.0.0",
44
"description": "Connection pool for node-postgres",
55
"main": "index.js",
66
"directories": {
77
"test": "test"
88
},
99
"scripts": {
10-
"test": "node_modules/.bin/mocha"
10+
"test": "node_modules/.bin/standard && node_modules/.bin/mocha"
1111
},
1212
"repository": {
1313
"type": "git",
@@ -31,7 +31,9 @@
3131
"expect.js": "0.3.1",
3232
"lodash": "4.13.1",
3333
"mocha": "^2.3.3",
34-
"pg": "4.5.6"
34+
"pg": "4.5.6",
35+
"standard": "7.1.2",
36+
"standard-format": "2.2.1"
3537
},
3638
"dependencies": {
3739
"debug": "^2.2.0",

test/index.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
var expect = require('expect.js')
2-
var Client = require('pg').Client
32
var co = require('co')
43
var Promise = require('bluebird')
54
var _ = require('lodash')
65

7-
var Pool = require('../')
6+
var describe = require('mocha').describe
7+
var it = require('mocha').it
88

9-
describe('pool', function() {
9+
var Pool = require('../')
1010

11-
describe('with callbacks', function() {
12-
it('works totally unconfigured', function(done) {
11+
describe('pool', function () {
12+
describe('with callbacks', function () {
13+
it('works totally unconfigured', function (done) {
1314
const pool = new Pool()
14-
pool.connect(function(err, client, release) {
15+
pool.connect(function (err, client, release) {
1516
if (err) return done(err)
16-
client.query('SELECT NOW()', function(err, res) {
17+
client.query('SELECT NOW()', function (err, res) {
1718
release()
1819
if (err) return done(err)
1920
expect(res.rows).to.have.length(1)
@@ -22,37 +23,39 @@ describe('pool', function() {
2223
})
2324
})
2425

25-
it('passes props to clients', function(done) {
26+
it('passes props to clients', function (done) {
2627
const pool = new Pool({ binary: true })
27-
pool.connect(function(err, client, release) {
28+
pool.connect(function (err, client, release) {
2829
release()
30+
if (err) return done(err)
2931
expect(client.binary).to.eql(true)
3032
pool.end(done)
3133
})
3234
})
3335

34-
it('removes client if it errors in background', function(done) {
36+
it('removes client if it errors in background', function (done) {
3537
const pool = new Pool()
36-
pool.connect(function(err, client, release) {
38+
pool.connect(function (err, client, release) {
3739
release()
40+
if (err) return done(err)
3841
client.testString = 'foo'
39-
setTimeout(function() {
42+
setTimeout(function () {
4043
client.emit('error', new Error('on purpose'))
4144
}, 10)
4245
})
43-
pool.on('error', function(err) {
46+
pool.on('error', function (err) {
4447
expect(err.message).to.be('on purpose')
4548
expect(err.client).to.not.be(undefined)
4649
expect(err.client.testString).to.be('foo')
47-
err.client.connection.stream.on('end', function() {
50+
err.client.connection.stream.on('end', function () {
4851
pool.end(done)
4952
})
5053
})
5154
})
5255
})
5356

54-
describe('with promises', function() {
55-
it('connects and disconnects', co.wrap(function*() {
57+
describe('with promises', function () {
58+
it('connects and disconnects', co.wrap(function * () {
5659
var pool = new Pool()
5760
var client = yield pool.connect()
5861
expect(pool.pool.availableObjectsCount()).to.be(0)
@@ -64,13 +67,13 @@ describe('pool', function() {
6467
return yield pool.end()
6568
}))
6669

67-
it('properly pools clients', co.wrap(function*() {
70+
it('properly pools clients', co.wrap(function * () {
6871
var pool = new Pool({ poolSize: 9 })
6972
var count = 0
7073
while (count < 30) {
7174
count++
72-
pool.connect().then(function(client) {
73-
client.queryAsync('select $1::text as name', ['hi']).then(function(res) {
75+
pool.connect().then(function (client) {
76+
client.queryAsync('select $1::text as name', ['hi']).then(function (res) {
7477
client.release()
7578
})
7679
})
@@ -80,28 +83,25 @@ describe('pool', function() {
8083
return yield pool.end()
8184
}))
8285

83-
it('supports just running queries', co.wrap(function*() {
86+
it('supports just running queries', co.wrap(function * () {
8487
var pool = new Pool({ poolSize: 9 })
85-
var count = 0
86-
var queries = _.times(30).map(function() {
88+
var queries = _.times(30).map(function () {
8789
return pool.query('SELECT $1::text as name', ['hi'])
8890
})
89-
console.log('executing')
9091
yield queries
9192
expect(pool.pool.getPoolSize()).to.be(9)
9293
expect(pool.pool.availableObjectsCount()).to.be(9)
9394
return yield pool.end()
9495
}))
9596

96-
it('recovers from all errors', co.wrap(function*() {
97+
it('recovers from all errors', co.wrap(function * () {
9798
var pool = new Pool({ poolSize: 9 })
9899
var count = 0
99100

100-
while(count++ < 30) {
101+
while (count++ < 30) {
101102
try {
102103
yield pool.query('SELECT lksjdfd')
103-
} catch(e) {
104-
}
104+
} catch (e) {}
105105
}
106106
var res = yield pool.query('SELECT $1::text as name', ['hi'])
107107
expect(res.rows).to.eql([{ name: 'hi' }])

0 commit comments

Comments
 (0)