diff --git a/promise.js b/promise.js index 61b2ed656e..0e714ddf63 100644 --- a/promise.js +++ b/promise.js @@ -74,6 +74,34 @@ class PromisePoolNamespace { }); }); } + + 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); + }); + } } class PromisePoolCluster extends EventEmitter { 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..1b9bfea730 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,22 @@ 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'); + + 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'); + }); })();