mirror of
https://github.com/brianc/node-postgres.git
synced 2026-01-18 15:55:05 +00:00
Initial commit
This commit is contained in:
parent
c0d39055f2
commit
ad73407aad
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
node_modules
|
||||
npm-debug.log
|
||||
|
||||
14
Makefile
Normal file
14
Makefile
Normal file
@ -0,0 +1,14 @@
|
||||
.PHONY: jshint test publish-patch test
|
||||
|
||||
test:
|
||||
npm test
|
||||
|
||||
patch: test
|
||||
npm version patch -m "Bump version"
|
||||
git push origin master --tags
|
||||
npm publish
|
||||
|
||||
minor: test
|
||||
npm version minor -m "Bump version"
|
||||
git push origin master --tags
|
||||
npm publish
|
||||
46
index.js
46
index.js
@ -3,11 +3,11 @@ var util = require('util')
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
var debug = require('debug')
|
||||
|
||||
var Pool = module.exports = function(options) {
|
||||
var Pool = module.exports = function (options, Client) {
|
||||
EventEmitter.call(this)
|
||||
this.options = options || {}
|
||||
this.log = this.options.log || debug('pg:pool')
|
||||
this.Client = this.options.Client || require('pg').Client
|
||||
this.Client = this.options.Client || Client || require('pg').Client
|
||||
this.Promise = this.options.Promise || Promise
|
||||
|
||||
this.options.max = this.options.max || this.options.poolSize || 10
|
||||
@ -18,33 +18,33 @@ var Pool = module.exports = function(options) {
|
||||
|
||||
util.inherits(Pool, EventEmitter)
|
||||
|
||||
Pool.prototype._destroy = function(client) {
|
||||
Pool.prototype._destroy = function (client) {
|
||||
if (client._destroying) return
|
||||
client._destroying = true
|
||||
client.end()
|
||||
}
|
||||
|
||||
Pool.prototype._create = function(cb) {
|
||||
Pool.prototype._create = function (cb) {
|
||||
this.log('connecting new client')
|
||||
var client = new this.Client(this.options)
|
||||
|
||||
client.on('error', function(e) {
|
||||
client.on('error', function (e) {
|
||||
this.log('connected client error:', e)
|
||||
this.pool.destroy(client)
|
||||
e.client = client
|
||||
this.emit('error', e)
|
||||
}.bind(this))
|
||||
|
||||
client.connect(function(err) {
|
||||
client.connect(function (err) {
|
||||
this.log('client connected')
|
||||
if (err) {
|
||||
this.log('client connection error:', e)
|
||||
this.log('client connection error:', err)
|
||||
cb(err)
|
||||
}
|
||||
|
||||
client.queryAsync = function(text, values) {
|
||||
client.queryAsync = function (text, values) {
|
||||
return new this.Promise((resolve, reject) => {
|
||||
client.query(text, values, function(err, res) {
|
||||
client.query(text, values, function (err, res) {
|
||||
err ? reject(err) : resolve(res)
|
||||
})
|
||||
})
|
||||
@ -54,21 +54,21 @@ Pool.prototype._create = function(cb) {
|
||||
}.bind(this))
|
||||
}
|
||||
|
||||
Pool.prototype.connect = function(cb) {
|
||||
return new this.Promise(function(resolve, reject) {
|
||||
Pool.prototype.connect = function (cb) {
|
||||
return new this.Promise(function (resolve, reject) {
|
||||
this.log('acquire client begin')
|
||||
this.pool.acquire(function(err, client) {
|
||||
this.pool.acquire(function (err, client) {
|
||||
if (err) {
|
||||
this.log('acquire client. error:', err)
|
||||
if (cb) {
|
||||
cb(err, null, function() { })
|
||||
cb(err, null, function () {})
|
||||
}
|
||||
return reject(err)
|
||||
}
|
||||
|
||||
this.log('acquire client')
|
||||
|
||||
client.release = function(err) {
|
||||
client.release = function (err) {
|
||||
if (err) {
|
||||
this.log('release client. error:', err)
|
||||
this.pool.destroy(client)
|
||||
@ -89,26 +89,26 @@ Pool.prototype.connect = function(cb) {
|
||||
|
||||
Pool.prototype.take = Pool.prototype.connect
|
||||
|
||||
Pool.prototype.query = function(text, values) {
|
||||
return this.take().then(function(client) {
|
||||
Pool.prototype.query = function (text, values) {
|
||||
return this.take().then(function (client) {
|
||||
return client.queryAsync(text, values)
|
||||
.then(function(res) {
|
||||
.then(function (res) {
|
||||
client.release()
|
||||
return res
|
||||
}).catch(function(error) {
|
||||
}).catch(function (error) {
|
||||
client.release(error)
|
||||
throw error
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Pool.prototype.end = function(cb) {
|
||||
Pool.prototype.end = function (cb) {
|
||||
this.log('draining pool')
|
||||
return new this.Promise(function(resolve, reject) {
|
||||
this.pool.drain(function() {
|
||||
return new this.Promise(function (resolve, reject) {
|
||||
this.pool.drain(function () {
|
||||
this.log('pool drained, calling destroy all now')
|
||||
this.pool.destroyAllNow(function() {
|
||||
if(cb) {
|
||||
this.pool.destroyAllNow(function () {
|
||||
if (cb) {
|
||||
cb()
|
||||
}
|
||||
resolve()
|
||||
|
||||
@ -1,13 +1,13 @@
|
||||
{
|
||||
"name": "pg-pool",
|
||||
"version": "0.0.1",
|
||||
"version": "1.0.0",
|
||||
"description": "Connection pool for node-postgres",
|
||||
"main": "index.js",
|
||||
"directories": {
|
||||
"test": "test"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "node_modules/.bin/mocha"
|
||||
"test": "node_modules/.bin/standard && node_modules/.bin/mocha"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -31,7 +31,9 @@
|
||||
"expect.js": "0.3.1",
|
||||
"lodash": "4.13.1",
|
||||
"mocha": "^2.3.3",
|
||||
"pg": "4.5.6"
|
||||
"pg": "4.5.6",
|
||||
"standard": "7.1.2",
|
||||
"standard-format": "2.2.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"debug": "^2.2.0",
|
||||
|
||||
@ -1,19 +1,20 @@
|
||||
var expect = require('expect.js')
|
||||
var Client = require('pg').Client
|
||||
var co = require('co')
|
||||
var Promise = require('bluebird')
|
||||
var _ = require('lodash')
|
||||
|
||||
var describe = require('mocha').describe
|
||||
var it = require('mocha').it
|
||||
|
||||
var Pool = require('../')
|
||||
|
||||
describe('pool', function() {
|
||||
|
||||
describe('with callbacks', function() {
|
||||
it('works totally unconfigured', function(done) {
|
||||
describe('pool', function () {
|
||||
describe('with callbacks', function () {
|
||||
it('works totally unconfigured', function (done) {
|
||||
const pool = new Pool()
|
||||
pool.connect(function(err, client, release) {
|
||||
pool.connect(function (err, client, release) {
|
||||
if (err) return done(err)
|
||||
client.query('SELECT NOW()', function(err, res) {
|
||||
client.query('SELECT NOW()', function (err, res) {
|
||||
release()
|
||||
if (err) return done(err)
|
||||
expect(res.rows).to.have.length(1)
|
||||
@ -22,37 +23,39 @@ describe('pool', function() {
|
||||
})
|
||||
})
|
||||
|
||||
it('passes props to clients', function(done) {
|
||||
it('passes props to clients', function (done) {
|
||||
const pool = new Pool({ binary: true })
|
||||
pool.connect(function(err, client, release) {
|
||||
pool.connect(function (err, client, release) {
|
||||
release()
|
||||
if (err) return done(err)
|
||||
expect(client.binary).to.eql(true)
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
|
||||
it('removes client if it errors in background', function(done) {
|
||||
it('removes client if it errors in background', function (done) {
|
||||
const pool = new Pool()
|
||||
pool.connect(function(err, client, release) {
|
||||
pool.connect(function (err, client, release) {
|
||||
release()
|
||||
if (err) return done(err)
|
||||
client.testString = 'foo'
|
||||
setTimeout(function() {
|
||||
setTimeout(function () {
|
||||
client.emit('error', new Error('on purpose'))
|
||||
}, 10)
|
||||
})
|
||||
pool.on('error', function(err) {
|
||||
pool.on('error', function (err) {
|
||||
expect(err.message).to.be('on purpose')
|
||||
expect(err.client).to.not.be(undefined)
|
||||
expect(err.client.testString).to.be('foo')
|
||||
err.client.connection.stream.on('end', function() {
|
||||
err.client.connection.stream.on('end', function () {
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('with promises', function() {
|
||||
it('connects and disconnects', co.wrap(function*() {
|
||||
describe('with promises', function () {
|
||||
it('connects and disconnects', co.wrap(function * () {
|
||||
var pool = new Pool()
|
||||
var client = yield pool.connect()
|
||||
expect(pool.pool.availableObjectsCount()).to.be(0)
|
||||
@ -64,13 +67,13 @@ describe('pool', function() {
|
||||
return yield pool.end()
|
||||
}))
|
||||
|
||||
it('properly pools clients', co.wrap(function*() {
|
||||
it('properly pools clients', co.wrap(function * () {
|
||||
var pool = new Pool({ poolSize: 9 })
|
||||
var count = 0
|
||||
while (count < 30) {
|
||||
count++
|
||||
pool.connect().then(function(client) {
|
||||
client.queryAsync('select $1::text as name', ['hi']).then(function(res) {
|
||||
pool.connect().then(function (client) {
|
||||
client.queryAsync('select $1::text as name', ['hi']).then(function (res) {
|
||||
client.release()
|
||||
})
|
||||
})
|
||||
@ -80,28 +83,25 @@ describe('pool', function() {
|
||||
return yield pool.end()
|
||||
}))
|
||||
|
||||
it('supports just running queries', co.wrap(function*() {
|
||||
it('supports just running queries', co.wrap(function * () {
|
||||
var pool = new Pool({ poolSize: 9 })
|
||||
var count = 0
|
||||
var queries = _.times(30).map(function() {
|
||||
var queries = _.times(30).map(function () {
|
||||
return pool.query('SELECT $1::text as name', ['hi'])
|
||||
})
|
||||
console.log('executing')
|
||||
yield queries
|
||||
expect(pool.pool.getPoolSize()).to.be(9)
|
||||
expect(pool.pool.availableObjectsCount()).to.be(9)
|
||||
return yield pool.end()
|
||||
}))
|
||||
|
||||
it('recovers from all errors', co.wrap(function*() {
|
||||
it('recovers from all errors', co.wrap(function * () {
|
||||
var pool = new Pool({ poolSize: 9 })
|
||||
var count = 0
|
||||
|
||||
while(count++ < 30) {
|
||||
while (count++ < 30) {
|
||||
try {
|
||||
yield pool.query('SELECT lksjdfd')
|
||||
} catch(e) {
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
var res = yield pool.query('SELECT $1::text as name', ['hi'])
|
||||
expect(res.rows).to.eql([{ name: 'hi' }])
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user