@@ -428,8 +428,106 @@ function createPool(opts) {
428
428
'format'
429
429
] ) ;
430
430
431
+ class PromisePoolCluster extends EventEmitter {
432
+ constructor ( poolCluster , thePromise ) {
433
+ super ( ) ;
434
+ this . poolCluster = poolCluster ;
435
+ this . Promise = thePromise || Promise ;
436
+ inheritEvents ( poolCluster , this , [ 'acquire' , 'connection' , 'enqueue' , 'release' ] ) ;
437
+ }
438
+
439
+ getConnection ( ) {
440
+ const corePoolCluster = this . poolCluster ;
441
+ return new this . Promise ( ( resolve , reject ) => {
442
+ corePoolCluster . getConnection ( ( err , coreConnection ) => {
443
+ if ( err ) {
444
+ reject ( err ) ;
445
+ } else {
446
+ resolve ( new PromisePoolConnection ( coreConnection , this . Promise ) ) ;
447
+ }
448
+ } ) ;
449
+ } ) ;
450
+ }
451
+
452
+ query ( sql , args ) {
453
+ const corePoolCluster = this . poolCluster ;
454
+ const localErr = new Error ( ) ;
455
+ if ( typeof args === 'function' ) {
456
+ throw new Error (
457
+ 'Callback function is not available with promise clients.'
458
+ ) ;
459
+ }
460
+ return new this . Promise ( ( resolve , reject ) => {
461
+ const done = makeDoneCb ( resolve , reject , localErr ) ;
462
+ corePoolCluster . query ( sql , args , done ) ;
463
+ } ) ;
464
+ }
465
+
466
+ of ( pattern , selector ) {
467
+ return new PromisePoolCluster (
468
+ this . poolCluster . of ( pattern , selector ) ,
469
+ this . Promise
470
+ ) ;
471
+ }
472
+
473
+ end ( ) {
474
+ const corePoolCluster = this . poolCluster ;
475
+ const localErr = new Error ( ) ;
476
+ return new this . Promise ( ( resolve , reject ) => {
477
+ corePoolCluster . end ( err => {
478
+ if ( err ) {
479
+ localErr . message = err . message ;
480
+ localErr . code = err . code ;
481
+ localErr . errno = err . errno ;
482
+ localErr . sqlState = err . sqlState ;
483
+ localErr . sqlMessage = err . sqlMessage ;
484
+ reject ( localErr ) ;
485
+ } else {
486
+ resolve ( ) ;
487
+ }
488
+ } ) ;
489
+ } ) ;
490
+ }
491
+ }
492
+
493
+ /**
494
+ * proxy poolCluster synchronous functions
495
+ */
496
+ ( function ( functionsToWrap ) {
497
+ for ( let i = 0 ; functionsToWrap && i < functionsToWrap . length ; i ++ ) {
498
+ const func = functionsToWrap [ i ] ;
499
+
500
+ if (
501
+ typeof core . PoolCluster . prototype [ func ] === 'function' &&
502
+ PromisePoolCluster . prototype [ func ] === undefined
503
+ ) {
504
+ PromisePoolCluster . prototype [ func ] = ( function factory ( funcName ) {
505
+ return function ( ) {
506
+ return core . PoolCluster . prototype [ funcName ] . apply ( this . poolCluster , arguments ) ;
507
+ } ;
508
+ } ) ( func ) ;
509
+ }
510
+ }
511
+ } ) ( [
512
+ 'add'
513
+ ] ) ;
514
+
515
+ function createPoolCluster ( opts ) {
516
+ const corePoolCluster = core . createPoolCluster ( opts ) ;
517
+ const thePromise = ( opts && opts . Promise ) || Promise ;
518
+ if ( ! thePromise ) {
519
+ throw new Error (
520
+ 'no Promise implementation available.' +
521
+ 'Use promise-enabled node version or pass userland Promise' +
522
+ " implementation as parameter, for example: { Promise: require('bluebird') }"
523
+ ) ;
524
+ }
525
+ return new PromisePoolCluster ( corePoolCluster , thePromise ) ;
526
+ }
527
+
431
528
exports . createConnection = createConnection ;
432
529
exports . createPool = createPool ;
530
+ exports . createPoolCluster = createPoolCluster ;
433
531
exports . escape = core . escape ;
434
532
exports . escapeId = core . escapeId ;
435
533
exports . format = core . format ;
0 commit comments