Skip to content

Commit b5b49eb

Browse files
committed
Add deprecations
This adds deprecations in preparation for `[email protected]` - deprecate using event emitters on automatically created results from client.query. - deprecate query.promise() - it should never have been a public method and it's not documented. I need to do better about using _ prefix on private methods in the future. - deprecate singleton pool on the `pg` object. `pg.connect`, `pg.end`, and `pg.cancel`.
1 parent bbb759f commit b5b49eb

File tree

7 files changed

+98
-23
lines changed

7 files changed

+98
-23
lines changed

lib/client.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var pgPass = require('pgpass');
1313
var TypeOverrides = require('./type-overrides');
1414

1515
var ConnectionParameters = require('./connection-parameters');
16+
var utils = require('./utils');
1617
var Query = require('./query');
1718
var defaults = require('./defaults');
1819
var Connection = require('./connection');
@@ -348,10 +349,12 @@ Client.prototype.copyTo = function (text) {
348349
throw new Error("For PostgreSQL COPY TO/COPY FROM support npm install pg-copy-streams");
349350
};
350351

352+
var DeprecatedEmitterQuery = utils.deprecateEventEmitter(Query);
353+
351354
Client.prototype.query = function(config, values, callback) {
352355
//can take in strings, config object or query object
353356
var query = (typeof config.submit == 'function') ? config :
354-
new Query(config, values, callback);
357+
new DeprecatedEmitterQuery(config, values, callback);
355358
if(this.binary && !query.binary) {
356359
query.binary = true;
357360
}

lib/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var PG = function(clientConstructor) {
2727

2828
util.inherits(PG, EventEmitter);
2929

30-
PG.prototype.end = function() {
30+
PG.prototype.end = util.deprecate(function() {
3131
var self = this;
3232
var keys = Object.keys(this._pools);
3333
var count = keys.length;
@@ -47,9 +47,9 @@ PG.prototype.end = function() {
4747
});
4848
});
4949
}
50-
};
50+
}, 'PG.end is deprecated - please see the upgrade guide at https://node-postgres.com/guides/upgrading');
5151

52-
PG.prototype.connect = function(config, callback) {
52+
PG.prototype.connect = util.deprecate(function(config, callback) {
5353
if(typeof config == "function") {
5454
callback = config;
5555
config = null;
@@ -75,10 +75,10 @@ PG.prototype.connect = function(config, callback) {
7575
}.bind(this));
7676
}
7777
return pool.connect(callback);
78-
};
78+
}, 'PG.connect is deprecated - please see the upgrade guide at https://node-postgres.com/guides/upgrading');
7979

8080
// cancel the query running on the given client
81-
PG.prototype.cancel = function(config, client, query) {
81+
PG.prototype.cancel = util.deprecate(function(config, client, query) {
8282
if(client.native) {
8383
return client.cancel(query);
8484
}
@@ -89,7 +89,7 @@ PG.prototype.cancel = function(config, client, query) {
8989
}
9090
var cancellingClient = new this.Client(c);
9191
cancellingClient.cancel(client, query);
92-
};
92+
}, 'PG.cancel is deprecated - use client.cancel instead');
9393

9494
if(typeof process.env.NODE_PG_FORCE_NATIVE != 'undefined') {
9595
module.exports = new PG(require('./native'));

lib/native/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ var Client = module.exports = function(config) {
4646
this.namedQueries = {};
4747
};
4848

49+
Client.Query = NativeQuery;
50+
4951
util.inherits(Client, EventEmitter);
5052

5153
//connect to the backend
@@ -139,6 +141,35 @@ Client.prototype.query = function(config, values, callback) {
139141
return query;
140142
};
141143

144+
var DeprecatedQuery = require('../utils').deprecateEventEmitter(NativeQuery);
145+
146+
//send a query to the server
147+
//this method is highly overloaded to take
148+
//1) string query, optional array of parameters, optional function callback
149+
//2) object query with {
150+
// string query
151+
// optional array values,
152+
// optional function callback instead of as a separate parameter
153+
// optional string name to name & cache the query plan
154+
// optional string rowMode = 'array' for an array of results
155+
// }
156+
Client.prototype.query = function(config, values, callback) {
157+
if (typeof config.submit == 'function') {
158+
// accept query(new Query(...), (err, res) => { }) style
159+
if (typeof values == 'function') {
160+
config.callback = values;
161+
}
162+
this._queryQueue.push(config);
163+
this._pulseQueryQueue();
164+
return config;
165+
}
166+
167+
var query = new DeprecatedQuery(config, values, callback);
168+
this._queryQueue.push(query);
169+
this._pulseQueryQueue();
170+
return query;
171+
};
172+
142173
//disconnect from the backend server
143174
Client.prototype.end = function(cb) {
144175
var self = this;

lib/native/query.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ var util = require('util');
1111
var utils = require('../utils');
1212
var NativeResult = require('./result');
1313

14-
var NativeQuery = module.exports = function(native) {
14+
var NativeQuery = module.exports = function(config, values, callback) {
1515
EventEmitter.call(this);
16-
this.native = native;
17-
this.text = null;
18-
this.values = null;
19-
this.name = null;
20-
this.callback = null;
16+
config = utils.normalizeQueryConfig(config, values, callback);
17+
this.text = config.text;
18+
this.values = config.values;
19+
this.name = config.name;
20+
this.callback = config.callback;
2121
this.state = 'new';
22-
this._arrayMode = false;
22+
this._arrayMode = config.rowMode == 'array';
2323

2424
//if the 'row' event is listened for
2525
//then emit them as they come in
@@ -34,6 +34,13 @@ var NativeQuery = module.exports = function(native) {
3434

3535
util.inherits(NativeQuery, EventEmitter);
3636

37+
// TODO - remove in 7.0
38+
// this maintains backwards compat so someone could instantiate a query
39+
// manually: `new Query().then()`...
40+
NativeQuery._on = NativeQuery.on;
41+
NativeQuery._once = NativeQuery.once;
42+
43+
3744
NativeQuery.prototype.then = function(onSuccess, onFailure) {
3845
return this.promise().then(onSuccess, onFailure);
3946
};
@@ -42,15 +49,19 @@ NativeQuery.prototype.catch = function(callback) {
4249
return this.promise().catch(callback);
4350
};
4451

45-
NativeQuery.prototype.promise = function() {
52+
NativeQuery.prototype._getPromise = function() {
4653
if (this._promise) return this._promise;
4754
this._promise = new Promise(function(resolve, reject) {
48-
this.once('end', resolve);
49-
this.once('error', reject);
55+
this._once('end', resolve);
56+
this._once('error', reject);
5057
}.bind(this));
5158
return this._promise;
5259
};
5360

61+
NativeQuery.prototype.promise = util.deprecate(function() {
62+
return this._getPromise();
63+
}, 'Query.promise() is deprecated - see the upgrade guide at https://node-postgres.com/guides/upgrading');
64+
5465
NativeQuery.prototype.handleError = function(err) {
5566
var self = this;
5667
//copy pq error fields into the error object
@@ -71,6 +82,7 @@ NativeQuery.prototype.handleError = function(err) {
7182
NativeQuery.prototype.submit = function(client) {
7283
this.state = 'running';
7384
var self = this;
85+
self.native = client.native;
7486
client.native.arrayMode = this._arrayMode;
7587

7688
var after = function(err, rows) {

lib/query.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,33 @@ var Query = function(config, values, callback) {
4040

4141
util.inherits(Query, EventEmitter);
4242

43+
// TODO - remove in 7.0
44+
// this maintains backwards compat so someone could instantiate a query
45+
// manually: `new Query().then()`...
46+
Query._on = Query.on;
47+
Query._once = Query.once;
48+
4349
Query.prototype.then = function(onSuccess, onFailure) {
44-
return this.promise().then(onSuccess, onFailure);
50+
return this._getPromise().then(onSuccess, onFailure);
4551
};
4652

4753
Query.prototype.catch = function(callback) {
48-
return this.promise().catch(callback);
54+
return this._getPromise().catch(callback);
4955
};
5056

51-
Query.prototype.promise = function() {
57+
Query.prototype._getPromise = function () {
5258
if (this._promise) return this._promise;
5359
this._promise = new Promise(function(resolve, reject) {
54-
this.once('end', resolve);
55-
this.once('error', reject);
60+
this._once('end', resolve);
61+
this._once('error', reject);
5662
}.bind(this));
5763
return this._promise;
5864
};
5965

66+
Query.prototype.promise = util.deprecate(function() {
67+
return this._getPromise();
68+
}, 'Query.promise() is deprecated - see the upgrade guide at https://node-postgres.com/guides/upgrading');
69+
6070
Query.prototype.requiresPreparation = function() {
6171
//named queries must always be prepared
6272
if(this.name) { return true; }

lib/utils.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* README.md file in the root directory of this source tree.
77
*/
88

9+
var util = require('util');
10+
911
var defaults = require('./defaults');
1012

1113
function escapeElement(elementRepresentation) {
@@ -137,11 +139,26 @@ function normalizeQueryConfig (config, values, callback) {
137139
return config;
138140
}
139141

142+
var queryEventEmitterOverloadDeprecationMessage = 'Using the automatically created return value from client.query as an event emitter is deprecated. Please see the upgrade guide at https://node-postgres.com/guides/upgrading';
143+
144+
var deprecateEventEmitter = function(Emitter) {
145+
var Result = function () {
146+
Emitter.apply(this, arguments);
147+
};
148+
util.inherits(Result, Emitter);
149+
Result.prototype._on = Result.prototype.on;
150+
Result.prototype._once = Result.prototype.once;
151+
Result.prototype.on = util.deprecate(Result.prototype.on, queryEventEmitterOverloadDeprecationMessage);
152+
Result.prototype.once = util.deprecate(Result.prototype.once, queryEventEmitterOverloadDeprecationMessage);
153+
return Result;
154+
};
155+
140156
module.exports = {
141157
prepareValue: function prepareValueWrapper (value) {
142158
//this ensures that extra arguments do not get passed into prepareValue
143159
//by accident, eg: from calling values.map(utils.prepareValue)
144160
return prepareValue(value);
145161
},
146-
normalizeQueryConfig: normalizeQueryConfig
162+
normalizeQueryConfig: normalizeQueryConfig,
163+
deprecateEventEmitter: deprecateEventEmitter,
147164
};

test/test-helper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//make assert a global...
22
assert = require('assert');
33

4+
process.noDeprecation = true
5+
46
//support for [email protected]
57
if (typeof Promise == 'undefined') {
68
global.Promise = require('promise-polyfill')

0 commit comments

Comments
 (0)