Skip to content

Commit c5640c9

Browse files
committed
Make BoundPool an es6 subclass of Pool
This fixes subtle subclassing bugs using es6 classes. It is a semver breaking change targeting 8.0 release...though it's not likely to cause issues in the general case.
1 parent 5cf8f5f commit c5640c9

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

packages/pg/lib/index.js

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,30 @@
77
* README.md file in the root directory of this source tree.
88
*/
99

10-
var util = require('util')
11-
var Client = require('./client')
12-
var defaults = require('./defaults')
13-
var Connection = require('./connection')
14-
var Pool = require('pg-pool')
15-
const checkConstructor = require('./compat/check-constructor')
10+
const Client = require('./client')
11+
const defaults = require('./defaults')
12+
const Connection = require('./connection')
13+
const Pool = require('pg-pool')
1614

1715
const poolFactory = (Client) => {
18-
var BoundPool = function (options) {
19-
// eslint-disable-next-line no-eval
20-
checkConstructor('pg.Pool', 'PG-POOL-NEW', () => eval('new.target'))
21-
22-
var config = Object.assign({ Client: Client }, options)
23-
return new Pool(config)
16+
return class BoundPool extends Pool {
17+
constructor(options) {
18+
const config = Object.assign({ Client: Client }, options)
19+
super(config)
20+
}
2421
}
25-
26-
util.inherits(BoundPool, Pool)
27-
28-
return BoundPool
2922
}
3023

31-
var PG = function (clientConstructor) {
32-
this.defaults = defaults
33-
this.Client = clientConstructor
34-
this.Query = this.Client.Query
35-
this.Pool = poolFactory(this.Client)
36-
this._pools = []
37-
this.Connection = Connection
38-
this.types = require('pg-types')
24+
class PG {
25+
constructor(clientConstructor) {
26+
this.defaults = defaults
27+
this.Client = clientConstructor
28+
this.Query = this.Client.Query
29+
this.Pool = poolFactory(this.Client)
30+
this._pools = []
31+
this.Connection = Connection
32+
this.types = require('pg-types')
33+
}
3934
}
4035

4136
if (typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined') {
@@ -46,7 +41,7 @@ if (typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined') {
4641
// lazy require native module...the native module may not have installed
4742
module.exports.__defineGetter__('native', function () {
4843
delete module.exports.native
49-
var native = null
44+
let native = null
5045
try {
5146
native = new PG(require('./native'))
5247
} catch (err) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
"use strict"
3+
const helper = require('./../test-helper')
4+
const assert = require('assert')
5+
6+
const suite = new helper.Suite()
7+
8+
suite.testAsync('BoundPool can be subclassed', async () => {
9+
const Pool = helper.pg.Pool;
10+
class SubPool extends Pool {
11+
12+
}
13+
const subPool = new SubPool()
14+
const client = await subPool.connect()
15+
client.release()
16+
await subPool.end()
17+
assert(subPool instanceof helper.pg.Pool)
18+
})
19+
20+
suite.test('calling pg.Pool without new throws', () => {
21+
const Pool = helper.pg.Pool;
22+
assert.throws(() => {
23+
const pool = Pool()
24+
})
25+
})

0 commit comments

Comments
 (0)