@@ -349,19 +349,37 @@ Client.prototype.copyTo = function (text) {
349
349
} ;
350
350
351
351
Client . prototype . query = function ( config , values , callback ) {
352
- //can take in strings, config object or query object
353
- var query = ( typeof config . submit == 'function' ) ? config :
354
- new Query ( config , values , callback ) ;
352
+ var promise ;
353
+ var isQueryable = typeof config . submit == 'function' ;
354
+ var query ;
355
+ // if we receive an object with a 'submit' function we delegate
356
+ // processing to the passed object - this is how pg.Query, QueryStream, and Cursor work
357
+ if ( isQueryable ) {
358
+ query = config ;
359
+ } else {
360
+ query = new Query ( config , values , callback ) ;
361
+ if ( ! query . callback ) {
362
+ promise = new global . Promise ( function ( resolve , reject ) {
363
+ query . on ( 'error' , reject ) ;
364
+ query . on ( 'end' , resolve ) ;
365
+ } ) ;
366
+ }
367
+ }
355
368
if ( this . binary && ! query . binary ) {
356
369
query . binary = true ;
357
370
}
371
+
372
+ // TODO - this is a smell
358
373
if ( query . _result ) {
359
374
query . _result . _getTypeParser = this . _types . getTypeParser . bind ( this . _types ) ;
360
375
}
361
376
362
377
this . queryQueue . push ( query ) ;
363
378
this . _pulseQueryQueue ( ) ;
364
- return query ;
379
+
380
+ // if we were passed a queryable, return it
381
+ // otherwise return callback/promise result
382
+ return isQueryable ? query : promise ;
365
383
} ;
366
384
367
385
Client . prototype . end = function ( cb ) {
0 commit comments