Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/pg-cursor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class Cursor extends EventEmitter {
this._queue = []
this.state = 'initialized'
this._result = new Result(this._conf.rowMode, this._conf.types)
this._Promise = this._conf.Promise || global.Promise
this._cb = null
this._rows = null
this._portal = null
Expand Down Expand Up @@ -212,7 +211,7 @@ class Cursor extends EventEmitter {
let promise

if (!cb) {
promise = new this._Promise((resolve, reject) => {
promise = new Promise((resolve, reject) => {
cb = (err) => (err ? reject(err) : resolve())
})
}
Expand All @@ -235,7 +234,7 @@ class Cursor extends EventEmitter {
let promise

if (!cb) {
promise = new this._Promise((resolve, reject) => {
promise = new Promise((resolve, reject) => {
cb = (err, rows) => (err ? reject(err) : resolve(rows))
})
}
Expand Down
16 changes: 7 additions & 9 deletions packages/pg-pool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function throwOnDoubleRelease() {
throw new Error('Release called on client which has already been released to the pool.')
}

function promisify(Promise, callback) {
function promisify(callback) {
if (callback) {
return { callback: callback, result: undefined }
}
Expand Down Expand Up @@ -93,7 +93,6 @@ class Pool extends EventEmitter {
this.options.maxLifetimeSeconds = this.options.maxLifetimeSeconds || 0
this.log = this.options.log || function () {}
this.Client = this.options.Client || Client || require('pg').Client
this.Promise = this.options.Promise || global.Promise

if (typeof this.options.idleTimeoutMillis === 'undefined') {
this.options.idleTimeoutMillis = 10000
Expand All @@ -109,7 +108,6 @@ class Pool extends EventEmitter {
}

_promiseTry(f) {
const Promise = this.Promise
if (typeof Promise.try === 'function') {
return Promise.try(f)
}
Expand Down Expand Up @@ -190,10 +188,10 @@ class Pool extends EventEmitter {
connect(cb) {
if (this.ending) {
const err = new Error('Cannot use a pool after calling end on the pool')
return cb ? cb(err) : this.Promise.reject(err)
return cb ? cb(err) : Promise.reject(err)
}

const response = promisify(this.Promise, cb)
const response = promisify(cb)
const result = response.result

// if we don't have to connect a new client, don't do so
Expand Down Expand Up @@ -431,7 +429,7 @@ class Pool extends EventEmitter {
query(text, values, cb) {
// guard clause against passing a function as the first parameter
if (typeof text === 'function') {
const response = promisify(this.Promise, text)
const response = promisify(text)
setImmediate(function () {
return response.callback(new Error('Passing a function as the first parameter to pool.query is not supported'))
})
Expand All @@ -443,7 +441,7 @@ class Pool extends EventEmitter {
cb = values
values = undefined
}
const response = promisify(this.Promise, cb)
const response = promisify(cb)
cb = response.callback

this.connect((err, client) => {
Expand Down Expand Up @@ -489,10 +487,10 @@ class Pool extends EventEmitter {
this.log('ending')
if (this.ending) {
const err = new Error('Called end on pool more than once')
return cb ? cb(err) : this.Promise.reject(err)
return cb ? cb(err) : Promise.reject(err)
}
this.ending = true
const promised = promisify(this.Promise, cb)
const promised = promisify(cb)
this._endCallback = promised.callback
this._pulseQueue()
return promised.result
Expand Down
20 changes: 5 additions & 15 deletions packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ const queryQueueDeprecationNotice = nodeUtils.deprecate(
'Client.queryQueue is deprecated and will be removed in pg@9.0.'
)

const byoPromiseDeprecationNotice = nodeUtils.deprecate(
() => {},
'Passing a custom Promise implementation to the Client/Pool constructor is deprecated and will be removed in pg@9.0.'
)

const queryQueueLengthDeprecationNotice = nodeUtils.deprecate(
() => {},
'Calling client.query() when the client is already executing a query is deprecated and will be removed in pg@9.0. Use async/await or an external async flow control mechanism instead.'
Expand Down Expand Up @@ -53,10 +48,6 @@ class Client extends EventEmitter {

const c = config || {}

if (c.Promise) {
byoPromiseDeprecationNotice()
}
this._Promise = c.Promise || global.Promise
this._types = new TypeOverrides(c.types)
this._ending = false
this._ended = false
Expand Down Expand Up @@ -205,7 +196,7 @@ class Client extends EventEmitter {
return
}

return new this._Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
this._connect((error) => {
if (error) {
reject(error)
Expand Down Expand Up @@ -247,8 +238,7 @@ class Client extends EventEmitter {
return
}
const con = this.connection
this._Promise
.resolve()
Promise.resolve()
.then(() => this.password(this.connectionParameters))
.then((pass) => {
if (pass !== undefined) {
Expand Down Expand Up @@ -604,7 +594,7 @@ class Client extends EventEmitter {
readTimeout = config.query_timeout || this.connectionParameters.query_timeout
query = new Query(config, values, callback)
if (!query.callback) {
result = new this._Promise((resolve, reject) => {
result = new Promise((resolve, reject) => {
query.callback = (err, res) => (err ? reject(err) : resolve(res))
}).catch((err) => {
// replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the
Expand Down Expand Up @@ -692,7 +682,7 @@ class Client extends EventEmitter {
if (cb) {
cb()
} else {
return this._Promise.resolve()
return Promise.resolve()
}
}

Expand All @@ -707,7 +697,7 @@ class Client extends EventEmitter {
if (cb) {
this.connection.once('end', cb)
} else {
return new this._Promise((resolve) => {
return new Promise((resolve) => {
this.connection.once('end', resolve)
})
}
Expand Down
7 changes: 3 additions & 4 deletions packages/pg/lib/native/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const Client = (module.exports = function (config) {
EventEmitter.call(this)
config = config || {}

this._Promise = config.Promise || global.Promise
this._types = new TypeOverrides(config.types)

this.native = new Native({
Expand Down Expand Up @@ -134,7 +133,7 @@ Client.prototype.connect = function (callback) {
return
}

return new this._Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
this._connect((error) => {
if (error) {
reject(error)
Expand Down Expand Up @@ -176,7 +175,7 @@ Client.prototype.query = function (config, values, callback) {
query = new NativeQuery(config, values, callback)
if (!query.callback) {
let resolveOut, rejectOut
result = new this._Promise((resolve, reject) => {
result = new Promise((resolve, reject) => {
resolveOut = resolve
rejectOut = reject
}).catch((err) => {
Expand Down Expand Up @@ -254,7 +253,7 @@ Client.prototype.end = function (cb) {
}
let result
if (!cb) {
result = new this._Promise(function (resolve, reject) {
result = new Promise(function (resolve, reject) {
cb = (err) => (err ? reject(err) : resolve())
})
}
Expand Down
15 changes: 10 additions & 5 deletions packages/pg/test/integration/client/query-as-promise-tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict'
const bluebird = require('bluebird')
const helper = require('../test-helper')
const pg = helper.pg
const assert = require('assert')
Expand Down Expand Up @@ -33,15 +32,21 @@ suite.test('promise API', (cb) => {
})
})

suite.test('promise API with configurable promise type', (cb) => {
const client = new pg.Client({ Promise: bluebird })
// The `Promise` constructor option was removed in pg@9.0; a client always uses the global Promise
// now, and passing one is ignored rather than honoured.
suite.test('a supplied promise type is ignored', (cb) => {
class NotAPromise extends Promise {}

const client = new pg.Client({ Promise: NotAPromise })
const connectPromise = client.connect()
assert(connectPromise instanceof bluebird, 'Client connect() returns configured promise')
assert(connectPromise instanceof Promise, 'Client connect() returns a promise')
assert(!(connectPromise instanceof NotAPromise), 'Client connect() ignores a supplied promise type')

connectPromise
.then(() => {
const queryPromise = client.query('SELECT 1')
assert(queryPromise instanceof bluebird, 'Client query() returns configured promise')
assert(queryPromise instanceof Promise, 'Client query() returns a promise')
assert(!(queryPromise instanceof NotAPromise), 'Client query() ignores a supplied promise type')

return queryPromise.then(() => {
client.end(cb)
Expand Down
Loading