Skip to content

Commit cfd9caa

Browse files
committed
Deprecate query.on & query.once
1 parent 76c1000 commit cfd9caa

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

lib/client.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ Client.prototype.copyTo = function (text) {
362362
throw new Error("For PostgreSQL COPY TO/COPY FROM support npm install pg-copy-streams");
363363
};
364364

365+
const DeprecatedQuery = require('./utils').deprecateEventEmitter(Query)
366+
365367
Client.prototype.query = function (config, values, callback) {
366368
//can take in strings, config object or query object
367369
var query;
@@ -373,7 +375,7 @@ Client.prototype.query = function (config, values, callback) {
373375
query.callback = query.callback || values
374376
}
375377
} else {
376-
query = new Query(config, values, callback)
378+
query = new DeprecatedQuery(config, values, callback)
377379
result = query
378380
}
379381

lib/native/client.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ Client.prototype.connect = function(cb) {
109109
return result
110110
};
111111

112+
const DeprecatedQuery = require('../utils').deprecateEventEmitter(NativeQuery)
113+
112114
//send a query to the server
113115
//this method is highly overloaded to take
114116
//1) string query, optional array of parameters, optional function callback
@@ -130,7 +132,7 @@ Client.prototype.query = function(config, values, callback) {
130132
return config;
131133
}
132134

133-
var query = new NativeQuery(config, values, callback);
135+
var query = new DeprecatedQuery(config, values, callback);
134136
this._queryQueue.push(query);
135137
this._pulseQueryQueue();
136138
return query;

lib/native/query.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ NativeQuery.prototype.catch = function(callback) {
7878
NativeQuery.prototype._getPromise = function() {
7979
if (this._promise) return this._promise;
8080
this._promise = new Promise(function(resolve, reject) {
81-
this.once('end', resolve);
82-
this.once('error', reject);
81+
this._once('end', resolve);
82+
this._once('error', reject);
8383
}.bind(this));
8484
return this._promise;
8585
};

lib/query.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ Query.prototype.catch = function(callback) {
6464
Query.prototype._getPromise = function() {
6565
if (this._promise) return this._promise;
6666
this._promise = new Promise(function(resolve, reject) {
67-
this.once('end', resolve);
68-
this.once('error', reject);
67+
this._once('end', resolve);
68+
this._once('error', reject);
6969
}.bind(this));
7070
return this._promise;
7171
};

lib/utils.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* README.md file in the root directory of this source tree.
77
*/
88

9+
const util = require('util')
910
var defaults = require('./defaults');
1011

1112
function escapeElement(elementRepresentation) {
@@ -137,11 +138,29 @@ function normalizeQueryConfig (config, values, callback) {
137138
return config;
138139
}
139140

141+
const queryEventEmitterOverloadDeprecationMessage = `
142+
Using the automatically created return value from client.query as an event emitter is deprecated.
143+
Use either the callback or promise interface.
144+
`
145+
146+
const deprecateEventEmitter = function(Emitter) {
147+
const Result = function () {
148+
Emitter.apply(this, arguments)
149+
}
150+
util.inherits(Result, Emitter)
151+
Result.prototype._on = Result.prototype.on
152+
Result.prototype._once = Result.prototype.once
153+
Result.prototype.on = util.deprecate(Result.prototype.on, queryEventEmitterOverloadDeprecationMessage)
154+
Result.prototype.once = util.deprecate(Result.prototype.once, queryEventEmitterOverloadDeprecationMessage)
155+
return Result
156+
}
157+
140158
module.exports = {
141159
prepareValue: function prepareValueWrapper (value) {
142160
//this ensures that extra arguments do not get passed into prepareValue
143161
//by accident, eg: from calling values.map(utils.prepareValue)
144162
return prepareValue(value);
145163
},
146-
normalizeQueryConfig: normalizeQueryConfig
164+
normalizeQueryConfig: normalizeQueryConfig,
165+
deprecateEventEmitter: deprecateEventEmitter,
147166
};

0 commit comments

Comments
 (0)