Comparing version 1.3.1 to 1.4.0
10
index.js
@@ -7,2 +7,5 @@ var genericPool = require('generic-pool') | ||
var Pool = module.exports = function (options, Client) { | ||
if (!(this instanceof Pool)) { | ||
return new Pool(options, Client) | ||
} | ||
EventEmitter.call(this) | ||
@@ -41,9 +44,10 @@ this.options = objectAssign({}, options) | ||
client.connect(function (err) { | ||
this.log('client connected') | ||
this.emit('connect', client) | ||
if (err) { | ||
this.log('client connection error:', err) | ||
cb(err) | ||
} else { | ||
this.log('client connected') | ||
this.emit('connect', client) | ||
cb(null, client) | ||
} | ||
cb(err, err ? null : client) | ||
}.bind(this)) | ||
@@ -50,0 +54,0 @@ } |
{ | ||
"name": "pg-pool", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "Connection pool for node-postgres", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -49,2 +49,36 @@ # pg-pool | ||
##### Note: | ||
The Pool constructor does not support passing a Database URL as the parameter. To use pg-pool on heroku, for example, you need to parse the URL into a config object. Here is an example of how to parse a Database URL. | ||
```js | ||
const Pool = require('pg-pool'); | ||
const url = require('url') | ||
const params = url.parse(process.env.DATABASE_URL); | ||
const auth = params.auth.split(':'); | ||
const config = { | ||
user: auth[0], | ||
password: auth[1], | ||
host: params.hostname, | ||
port: params.port, | ||
database: params.pathname.split('/')[1], | ||
ssl: true | ||
}; | ||
const pool = new Pool(config); | ||
/* | ||
Transforms, 'progres://DBuser:secret@DBHost:#####/myDB', into | ||
config = { | ||
user: 'DBuser', | ||
password: 'secret', | ||
host: 'DBHost', | ||
port: '#####', | ||
database: 'myDB', | ||
ssl: true | ||
} | ||
*/ | ||
``` | ||
### acquire clients with a promise | ||
@@ -51,0 +85,0 @@ |
var expect = require('expect.js') | ||
var EventEmitter = require('events').EventEmitter | ||
var describe = require('mocha').describe | ||
var it = require('mocha').it | ||
var objectAssign = require('object-assign') | ||
var Pool = require('../') | ||
@@ -25,2 +25,20 @@ | ||
it('emits "connect" only with a successful connection', function (done) { | ||
var pool = new Pool({ | ||
// This client will always fail to connect | ||
Client: mockClient({ | ||
connect: function (cb) { | ||
process.nextTick(function () { cb(new Error('bad news')) }) | ||
} | ||
}) | ||
}) | ||
pool.on('connect', function () { | ||
throw new Error('should never get here') | ||
}) | ||
pool._create(function (err) { | ||
if (err) done() | ||
else done(new Error('expected failure')) | ||
}) | ||
}) | ||
it('emits acquire every time a client is acquired', function (done) { | ||
@@ -47,1 +65,9 @@ var pool = new Pool() | ||
}) | ||
function mockClient (methods) { | ||
return function () { | ||
var client = new EventEmitter() | ||
objectAssign(client, methods) | ||
return client | ||
} | ||
} |
@@ -15,2 +15,8 @@ var expect = require('expect.js') | ||
describe('pool', function () { | ||
it('can be used as a factory function', function () { | ||
var pool = Pool() | ||
expect(pool instanceof Pool).to.be.ok() | ||
expect(typeof pool.connect).to.be('function') | ||
}) | ||
describe('with callbacks', function () { | ||
@@ -17,0 +23,0 @@ it('works totally unconfigured', function (done) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23444
340
351