From 51fb7db8fad8bc83f95121d06af72752513aea61 Mon Sep 17 00:00:00 2001 From: Cody Greene Date: Fri, 5 Aug 2016 14:21:51 -0700 Subject: [PATCH] Support function-like construction (plus test) (#23) * Support function-like construction Remove the necessity of using `new` in front of the `Pool`, i.e. allow it to be used as a regular function. This is following https://github.com/brianc/node-postgres/issues/1077 * add Pool factory test --- index.js | 3 +++ test/index.js | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/index.js b/index.js index f126306e..6f622bac 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,9 @@ var EventEmitter = require('events').EventEmitter var objectAssign = require('object-assign') var Pool = module.exports = function (options, Client) { + if (!(this instanceof Pool)) { + return new Pool(options, Client) + } EventEmitter.call(this) this.options = objectAssign({}, options) this.log = this.options.log || function () { } diff --git a/test/index.js b/test/index.js index a7103af6..e4616a43 100644 --- a/test/index.js +++ b/test/index.js @@ -12,6 +12,12 @@ if (typeof global.Promise === 'undefined') { } 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 () { it('works totally unconfigured', function (done) { var pool = new Pool()