Skip to content

Commit be22202

Browse files
fix: PromisePoolCluster.of returns PromisePoolCluster instead of PoolNamespace (#3261)
1 parent 0a290fe commit be22202

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

lib/promise/pool_cluster.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
const PromisePoolConnection = require('./pool_connection');
4+
const makeDoneCb = require('./make_done_cb');
5+
6+
class PromisePoolNamespace {
7+
8+
constructor(poolNamespace, thePromise) {
9+
this.poolNamespace = poolNamespace;
10+
this.Promise = thePromise || Promise;
11+
}
12+
13+
getConnection() {
14+
const corePoolNamespace = this.poolNamespace;
15+
return new this.Promise((resolve, reject) => {
16+
corePoolNamespace.getConnection((err, coreConnection) => {
17+
if (err) {
18+
reject(err);
19+
} else {
20+
resolve(new PromisePoolConnection(coreConnection, this.Promise));
21+
}
22+
});
23+
});
24+
}
25+
26+
query(sql, values) {
27+
const corePoolNamespace = this.poolNamespace;
28+
const localErr = new Error();
29+
if (typeof values === 'function') {
30+
throw new Error(
31+
'Callback function is not available with promise clients.',
32+
);
33+
}
34+
return new this.Promise((resolve, reject) => {
35+
const done = makeDoneCb(resolve, reject, localErr);
36+
corePoolNamespace.query(sql, values, done);
37+
});
38+
}
39+
40+
execute(sql, values) {
41+
const corePoolNamespace = this.poolNamespace;
42+
const localErr = new Error();
43+
if (typeof values === 'function') {
44+
throw new Error(
45+
'Callback function is not available with promise clients.',
46+
);
47+
}
48+
return new this.Promise((resolve, reject) => {
49+
const done = makeDoneCb(resolve, reject, localErr);
50+
corePoolNamespace.execute(sql, values, done);
51+
});
52+
}
53+
}
54+
55+
module.exports = PromisePoolNamespace;

promise.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const PromisePool = require('./lib/promise/pool.js');
1212
const makeDoneCb = require('./lib/promise/make_done_cb.js');
1313
const PromisePoolConnection = require('./lib/promise/pool_connection.js');
1414
const inheritEvents = require('./lib/promise/inherit_events.js');
15+
const PromisePoolNamespace = require('./lib/promise/pool_cluster');
1516

1617
function createConnectionPromise(opts) {
1718
const coreConnection = createConnection(opts);
@@ -109,7 +110,7 @@ class PromisePoolCluster extends EventEmitter {
109110
}
110111

111112
of(pattern, selector) {
112-
return new PromisePoolCluster(
113+
return new PromisePoolNamespace(
113114
this.poolCluster.of(pattern, selector),
114115
this.Promise,
115116
);

test/esm/integration/pool-cluster/test-promise-wrapper.test.mjs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,35 @@ const { createPoolCluster } = require('../../../../promise.js');
7979

8080
poolCluster.poolCluster.emit('online');
8181
});
82+
83+
await test(async () => {
84+
const poolCluster = createPoolCluster();
85+
poolCluster.add('MASTER', common.config);
86+
87+
const poolNamespace = poolCluster.of('MASTER');
88+
89+
assert.equal(
90+
poolNamespace.poolNamespace,
91+
poolCluster.poolCluster.of('MASTER'),
92+
);
93+
94+
const connection = await poolNamespace.getConnection();
95+
96+
assert.ok(connection, 'should get connection');
97+
connection.release();
98+
99+
const [result] = await poolNamespace.query(
100+
'SELECT 1 as a from dual where 1 = ?',
101+
[1],
102+
);
103+
assert.equal(result[0]['a'], 1, 'should query successfully');
104+
105+
const [result2] = await poolNamespace.execute(
106+
'SELECT 1 as a from dual where 1 = ?',
107+
[1],
108+
);
109+
assert.equal(result2[0]['a'], 1, 'should execute successfully');
110+
111+
poolCluster.end();
112+
});
82113
})();

0 commit comments

Comments
 (0)