From c4fe23307e6fee1deca6da5c878dd00bade35957 Mon Sep 17 00:00:00 2001 From: Natalie Wolfe Date: Fri, 15 Dec 2017 14:20:54 -0800 Subject: [PATCH 1/3] Use class-extends to wrap Pool --- lib/index.js | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/lib/index.js b/lib/index.js index 00e3ceed4..9b92d94f7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -7,31 +7,30 @@ * README.md file in the root directory of this source tree. */ -var util = require('util') var Client = require('./client') var defaults = require('./defaults') var Connection = require('./connection') var Pool = require('pg-pool') -const poolFactory = (Client) => { - var BoundPool = function (options) { - var config = Object.assign({ Client: Client }, options) - return new Pool(config) +const poolFactory = (pg) => { + return class BoundPool extends Pool { + constructor (options) { + var config = Object.assign({ Client: pg.Client }, options) + super(config) + } } - - util.inherits(BoundPool, Pool) - - return BoundPool } -var PG = function (clientConstructor) { - this.defaults = defaults - this.Client = clientConstructor - this.Query = this.Client.Query - this.Pool = poolFactory(this.Client) - this._pools = [] - this.Connection = Connection - this.types = require('pg-types') +class PG { + constructor (clientConstructor) { + this.defaults = defaults + this.Client = clientConstructor + this.Query = this.Client.Query + this.Pool = poolFactory(this) + this._pools = [] + this.Connection = Connection + this.types = require('pg-types') + } } if (typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined') { From da590fae514115713b6b11bbdcfae3a6dab1bc1a Mon Sep 17 00:00:00 2001 From: Charmander <~@charmander.me> Date: Wed, 15 Jan 2020 12:18:46 -0800 Subject: [PATCH 2/3] Minimize diff --- packages/pg/lib/index.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/pg/lib/index.js b/packages/pg/lib/index.js index 113bf685e..c3062947d 100644 --- a/packages/pg/lib/index.js +++ b/packages/pg/lib/index.js @@ -12,25 +12,23 @@ var defaults = require('./defaults') var Connection = require('./connection') var Pool = require('pg-pool') -const poolFactory = (pg) => { +const poolFactory = (Client) => { return class BoundPool extends Pool { constructor (options) { - var config = Object.assign({ Client: pg.Client }, options) + var config = Object.assign({ Client: Client }, options) super(config) } } } -class PG { - constructor (clientConstructor) { - this.defaults = defaults - this.Client = clientConstructor - this.Query = this.Client.Query - this.Pool = poolFactory(this) - this._pools = [] - this.Connection = Connection - this.types = require('pg-types') - } +var PG = function (clientConstructor) { + this.defaults = defaults + this.Client = clientConstructor + this.Query = this.Client.Query + this.Pool = poolFactory(this.Client) + this._pools = [] + this.Connection = Connection + this.types = require('pg-types') } if (typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined') { From 5fa8a4937c15f9c51707e49431c29aa4f5e319ec Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Fri, 10 Jan 2020 09:57:21 -0600 Subject: [PATCH 3/3] Test `BoundPool` inheritance --- .../test/integration/gh-issues/1542-tests.js | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 packages/pg/test/integration/gh-issues/1542-tests.js diff --git a/packages/pg/test/integration/gh-issues/1542-tests.js b/packages/pg/test/integration/gh-issues/1542-tests.js new file mode 100644 index 000000000..4d30d6020 --- /dev/null +++ b/packages/pg/test/integration/gh-issues/1542-tests.js @@ -0,0 +1,25 @@ + +"use strict" +const helper = require('./../test-helper') +const assert = require('assert') + +const suite = new helper.Suite() + +suite.testAsync('BoundPool can be subclassed', async () => { + const Pool = helper.pg.Pool; + class SubPool extends Pool { + + } + const subPool = new SubPool() + const client = await subPool.connect() + client.release() + await subPool.end() + assert(subPool instanceof helper.pg.Pool) +}) + +suite.test('calling pg.Pool without new throws', () => { + const Pool = helper.pg.Pool; + assert.throws(() => { + const pool = Pool() + }) +})