From ac77d7854c9655c1427972a428464d72f2c37848 Mon Sep 17 00:00:00 2001 From: bramkoot Date: Thu, 26 Jan 2017 17:14:02 +0100 Subject: [PATCH 1/4] Add connectionSetup as configuration option to prepare a client --- index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d355186..9d779ae 100644 --- a/index.js +++ b/index.js @@ -72,7 +72,12 @@ Pool.prototype._create = function (cb) { } else { this.log('client connected') this.emit('connect', client) - cb(null, client) + + if (this.options.connectionSetup && typeof this.options.connectionSetup == 'function') { + this.options.connectionSetup(client, cb) + } else { + cb(null, client) + } } }.bind(this)) } From c6f62699ebf92d618424ace74a55eaaf36adb2c6 Mon Sep 17 00:00:00 2001 From: bramkoot Date: Thu, 26 Jan 2017 17:48:43 +0100 Subject: [PATCH 2/4] Change == into === --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 9d779ae..b201803 100644 --- a/index.js +++ b/index.js @@ -73,7 +73,7 @@ Pool.prototype._create = function (cb) { this.log('client connected') this.emit('connect', client) - if (this.options.connectionSetup && typeof this.options.connectionSetup == 'function') { + if (this.options.connectionSetup && typeof this.options.connectionSetup === 'function') { this.options.connectionSetup(client, cb) } else { cb(null, client) From 7aece055583010e28533ad6bebd64b810f9b277c Mon Sep 17 00:00:00 2001 From: Bram Koot Date: Thu, 4 May 2017 17:06:32 +0200 Subject: [PATCH 3/4] Added tests, changed function truthy checking after code review --- index.js | 2 +- test/index.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index b201803..322cb75 100644 --- a/index.js +++ b/index.js @@ -73,7 +73,7 @@ Pool.prototype._create = function (cb) { this.log('client connected') this.emit('connect', client) - if (this.options.connectionSetup && typeof this.options.connectionSetup === 'function') { + if (this.options.connectionSetup !== undefined) { this.options.connectionSetup(client, cb) } else { cb(null, client) diff --git a/test/index.js b/test/index.js index 5f6d0ad..30b31d0 100644 --- a/test/index.js +++ b/test/index.js @@ -32,6 +32,25 @@ describe('pool', function () { }) }) + it('calls the connectionSetup function when successfully connected', function (done) { + var called = false + var pool = new Pool({ + connectionSetup: function (client, cb) { + called = true + cb(null, client) + } + }) + pool.connect(function (err, client, release) { + if (err) return done(err) + client.query('SELECT NOW()', function (err, res) { + release() + if (err) return done(err) + expect(called).to.be(true) + pool.end(done) + }) + }) + }) + it('passes props to clients', function (done) { var pool = new Pool({ binary: true }) pool.connect(function (err, client, release) { @@ -73,6 +92,23 @@ describe('pool', function () { }) }) + it('does not call setupConnection when an error occurs', function (done) { + var called = false + var pool = new Pool({ + host: 'no-postgres-server-here.com', + connectionSetup: function () { + called = true + } + }) + pool.query('SELECT $1::text as name', ['brianc'], function (err, res) { + expect(err).to.be.an(Error) + expect(called).to.be(false) + pool.end(function (err) { + done(err) + }) + }) + }) + it('removes client if it errors in background', function (done) { var pool = new Pool() pool.connect(function (err, client, release) { From 8dae10fa56923dd8e0316c9e32d1b42aaeaaa42f Mon Sep 17 00:00:00 2001 From: Bram Koot Date: Thu, 4 May 2017 17:26:44 +0200 Subject: [PATCH 4/4] Simplify test --- test/index.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/test/index.js b/test/index.js index 30b31d0..76a16f6 100644 --- a/test/index.js +++ b/test/index.js @@ -42,12 +42,9 @@ describe('pool', function () { }) pool.connect(function (err, client, release) { if (err) return done(err) - client.query('SELECT NOW()', function (err, res) { - release() - if (err) return done(err) - expect(called).to.be(true) - pool.end(done) - }) + expect(called).to.be(true) + release() + pool.end(done) }) })