From cfd9caa925b68a951ab0a0976636b3db99bf72f0 Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Fri, 16 Jun 2017 16:41:31 -0500 Subject: [PATCH] Deprecate query.on & query.once --- lib/client.js | 4 +++- lib/native/client.js | 4 +++- lib/native/query.js | 4 ++-- lib/query.js | 4 ++-- lib/utils.js | 21 ++++++++++++++++++++- 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/client.js b/lib/client.js index 8ca944b1..3eee5402 100644 --- a/lib/client.js +++ b/lib/client.js @@ -362,6 +362,8 @@ Client.prototype.copyTo = function (text) { throw new Error("For PostgreSQL COPY TO/COPY FROM support npm install pg-copy-streams"); }; +const DeprecatedQuery = require('./utils').deprecateEventEmitter(Query) + Client.prototype.query = function (config, values, callback) { //can take in strings, config object or query object var query; @@ -373,7 +375,7 @@ Client.prototype.query = function (config, values, callback) { query.callback = query.callback || values } } else { - query = new Query(config, values, callback) + query = new DeprecatedQuery(config, values, callback) result = query } diff --git a/lib/native/client.js b/lib/native/client.js index 7950bc3d..331f9206 100644 --- a/lib/native/client.js +++ b/lib/native/client.js @@ -109,6 +109,8 @@ Client.prototype.connect = function(cb) { return result }; +const DeprecatedQuery = require('../utils').deprecateEventEmitter(NativeQuery) + //send a query to the server //this method is highly overloaded to take //1) string query, optional array of parameters, optional function callback @@ -130,7 +132,7 @@ Client.prototype.query = function(config, values, callback) { return config; } - var query = new NativeQuery(config, values, callback); + var query = new DeprecatedQuery(config, values, callback); this._queryQueue.push(query); this._pulseQueryQueue(); return query; diff --git a/lib/native/query.js b/lib/native/query.js index a1f7dd42..4fb501f1 100644 --- a/lib/native/query.js +++ b/lib/native/query.js @@ -78,8 +78,8 @@ NativeQuery.prototype.catch = function(callback) { NativeQuery.prototype._getPromise = function() { if (this._promise) return this._promise; this._promise = new Promise(function(resolve, reject) { - this.once('end', resolve); - this.once('error', reject); + this._once('end', resolve); + this._once('error', reject); }.bind(this)); return this._promise; }; diff --git a/lib/query.js b/lib/query.js index d3656748..b997091c 100644 --- a/lib/query.js +++ b/lib/query.js @@ -64,8 +64,8 @@ Query.prototype.catch = function(callback) { Query.prototype._getPromise = function() { if (this._promise) return this._promise; this._promise = new Promise(function(resolve, reject) { - this.once('end', resolve); - this.once('error', reject); + this._once('end', resolve); + this._once('error', reject); }.bind(this)); return this._promise; }; diff --git a/lib/utils.js b/lib/utils.js index 82d1aefd..381775ff 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -6,6 +6,7 @@ * README.md file in the root directory of this source tree. */ +const util = require('util') var defaults = require('./defaults'); function escapeElement(elementRepresentation) { @@ -137,11 +138,29 @@ function normalizeQueryConfig (config, values, callback) { return config; } +const queryEventEmitterOverloadDeprecationMessage = ` +Using the automatically created return value from client.query as an event emitter is deprecated. +Use either the callback or promise interface. +` + +const deprecateEventEmitter = function(Emitter) { + const Result = function () { + Emitter.apply(this, arguments) + } + util.inherits(Result, Emitter) + Result.prototype._on = Result.prototype.on + Result.prototype._once = Result.prototype.once + Result.prototype.on = util.deprecate(Result.prototype.on, queryEventEmitterOverloadDeprecationMessage) + Result.prototype.once = util.deprecate(Result.prototype.once, queryEventEmitterOverloadDeprecationMessage) + return Result +} + module.exports = { prepareValue: function prepareValueWrapper (value) { //this ensures that extra arguments do not get passed into prepareValue //by accident, eg: from calling values.map(utils.prepareValue) return prepareValue(value); }, - normalizeQueryConfig: normalizeQueryConfig + normalizeQueryConfig: normalizeQueryConfig, + deprecateEventEmitter: deprecateEventEmitter, };