mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
Actual promisification of client.query
This commit is contained in:
parent
25a98f5099
commit
6d98b0847c
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user