Skip to content

Commit

Permalink
Warn when pg.Pool() isn’t called as a constructor
Browse files Browse the repository at this point in the history
in preparation for #1541. `eval` is a bit awkward, but it’s more accurate than an `instanceof` check and will work on platforms new enough to support pg 8 (i.e. only not Node 4).
  • Loading branch information
charmander committed Dec 6, 2019
1 parent 8f56b8c commit aaf3372
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/index.js
Expand Up @@ -13,8 +13,19 @@ var defaults = require('./defaults')
var Connection = require('./connection')
var Pool = require('pg-pool')

let hasNewTarget = false

try {
eval('(function () { new.target })')
hasNewTarget = true
} catch (error) {}

const poolFactory = (Client) => {
var BoundPool = function (options) {
if (hasNewTarget && eval('new.target') === undefined) {
process.emitWarning('Constructing a pg.Pool without new is deprecated and will stop working in pg 8.', 'DeprecationWarning', 'PG-POOL-NEW')
}

var config = Object.assign({ Client: Client }, options)
return new Pool(config)
}
Expand Down

0 comments on commit aaf3372

Please sign in to comment.