diff --git a/lib/promise/pool_cluster.js b/lib/promise/pool_cluster.js new file mode 100644 index 0000000000..b81d872a1a --- /dev/null +++ b/lib/promise/pool_cluster.js @@ -0,0 +1,55 @@ +'use strict'; + +const PromisePoolConnection = require('./pool_connection'); +const makeDoneCb = require('./make_done_cb'); + +class PromisePoolNamespace { + + constructor(poolNamespace, thePromise) { + this.poolNamespace = poolNamespace; + this.Promise = thePromise || Promise; + } + + getConnection() { + const corePoolNamespace = this.poolNamespace; + return new this.Promise((resolve, reject) => { + corePoolNamespace.getConnection((err, coreConnection) => { + if (err) { + reject(err); + } else { + resolve(new PromisePoolConnection(coreConnection, this.Promise)); + } + }); + }); + } + + query(sql, values) { + const corePoolNamespace = this.poolNamespace; + const localErr = new Error(); + if (typeof values === 'function') { + throw new Error( + 'Callback function is not available with promise clients.', + ); + } + return new this.Promise((resolve, reject) => { + const done = makeDoneCb(resolve, reject, localErr); + corePoolNamespace.query(sql, values, done); + }); + } + + execute(sql, values) { + const corePoolNamespace = this.poolNamespace; + const localErr = new Error(); + if (typeof values === 'function') { + throw new Error( + 'Callback function is not available with promise clients.', + ); + } + return new this.Promise((resolve, reject) => { + const done = makeDoneCb(resolve, reject, localErr); + corePoolNamespace.execute(sql, values, done); + }); + } +} + +module.exports = PromisePoolNamespace; diff --git a/promise.js b/promise.js index 4010437fe9..16c59eb82a 100644 --- a/promise.js +++ b/promise.js @@ -12,6 +12,7 @@ const PromisePool = require('./lib/promise/pool.js'); const makeDoneCb = require('./lib/promise/make_done_cb.js'); const PromisePoolConnection = require('./lib/promise/pool_connection.js'); const inheritEvents = require('./lib/promise/inherit_events.js'); +const PromisePoolNamespace = require('./lib/promise/pool_cluster'); function createConnectionPromise(opts) { const coreConnection = createConnection(opts); @@ -109,7 +110,7 @@ class PromisePoolCluster extends EventEmitter { } of(pattern, selector) { - return new PromisePoolCluster( + return new PromisePoolNamespace( this.poolCluster.of(pattern, selector), this.Promise, ); diff --git a/test/esm/integration/pool-cluster/test-promise-wrapper.test.mjs b/test/esm/integration/pool-cluster/test-promise-wrapper.test.mjs index 00a56edde6..163f11bf7c 100644 --- a/test/esm/integration/pool-cluster/test-promise-wrapper.test.mjs +++ b/test/esm/integration/pool-cluster/test-promise-wrapper.test.mjs @@ -79,4 +79,35 @@ const { createPoolCluster } = require('../../../../promise.js'); poolCluster.poolCluster.emit('online'); }); + + await test(async () => { + const poolCluster = createPoolCluster(); + poolCluster.add('MASTER', common.config); + + const poolNamespace = poolCluster.of('MASTER'); + + assert.equal( + poolNamespace.poolNamespace, + poolCluster.poolCluster.of('MASTER'), + ); + + const connection = await poolNamespace.getConnection(); + + assert.ok(connection, 'should get connection'); + connection.release(); + + const [result] = await poolNamespace.query( + 'SELECT 1 as a from dual where 1 = ?', + [1], + ); + assert.equal(result[0]['a'], 1, 'should query successfully'); + + const [result2] = await poolNamespace.execute( + 'SELECT 1 as a from dual where 1 = ?', + [1], + ); + assert.equal(result2[0]['a'], 1, 'should execute successfully'); + + poolCluster.end(); + }); })();