Actual promisification of client.query

This commit is contained in:
Brian M. Carlson 2017-06-09 16:35:01 -05:00 committed by Brian C
parent 25a98f5099
commit 6d98b0847c

View File

@ -349,19 +349,37 @@ Client.prototype.copyTo = function (text) {
};
Client.prototype.query = function(config, values, callback) {
//can take in strings, config object or query object
var query = (typeof config.submit == 'function') ? config :
new Query(config, values, callback);
var promise;
var isQueryable = typeof config.submit == 'function';
var query;
// if we receive an object with a 'submit' function we delegate
// processing to the passed object - this is how pg.Query, QueryStream, and Cursor work
if (isQueryable) {
query = config;
} else {
query = new Query(config, values, callback);
if (!query.callback) {
promise = new global.Promise(function (resolve, reject) {
query.on('error', reject);
query.on('end', resolve);
});
}
}
if(this.binary && !query.binary) {
query.binary = true;
}
// TODO - this is a smell
if(query._result) {
query._result._getTypeParser = this._types.getTypeParser.bind(this._types);
}
this.queryQueue.push(query);
this._pulseQueryQueue();
return query;
// if we were passed a queryable, return it
// otherwise return callback/promise result
return isQueryable ? query : promise;
};
Client.prototype.end = function(cb) {