Deprecate query.on & query.once

This commit is contained in:
Brian M. Carlson 2017-06-16 16:41:31 -05:00 committed by Brian C
parent 76c1000567
commit cfd9caa925
5 changed files with 30 additions and 7 deletions

View File

@ -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
}

View File

@ -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;

View File

@ -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;
};

View File

@ -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;
};

View File

@ -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,
};