diff --git a/lib/index.js b/lib/index.js index ac2991e..8575c7e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -47,27 +47,19 @@ PG.prototype.connect = function(config, callback) { name: poolName, create: function(callback) { var client = new self.Client(c); - client.connect(); - - var connectError = function(err) { - client.removeListener('connect', connectSuccess); - callback(err, null); - }; - - var connectSuccess = function() { - client.removeListener('error', connectError); - + client.connect(function(err) { + if(err) return callback(err); + //handle connected client background errors by emitting event //via the pg object and then removing errored client from the pool client.on('error', function(e) { self.emit('error', e, client); pool.destroy(client); }); - callback(null, client); - }; - client.once('connect', connectSuccess); - client.once('error', connectError); + callback(null, client); + }); + client.on('drain', function() { pool.release(client); });