Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: PromisePoolCluster.of returns PromisePoolCluster instead of PoolNamespace #3261

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,55 @@
return new PromisePool(corePool, thePromise);
}

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);

Check warning on line 70 in promise.js

View check run for this annotation

Codecov / codecov/patch

promise.js#L70

Added line #L70 was not covered by tests
} 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.',
);
}

Check warning on line 85 in promise.js

View check run for this annotation

Codecov / codecov/patch

promise.js#L82-L85

Added lines #L82 - L85 were not covered by tests
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.',
);
}

Check warning on line 99 in promise.js

View check run for this annotation

Codecov / codecov/patch

promise.js#L96-L99

Added lines #L96 - L99 were not covered by tests
return new this.Promise((resolve, reject) => {
const done = makeDoneCb(resolve, reject, localErr);
corePoolNamespace.execute(sql, values, done);
});
}
}

class PromisePoolCluster extends EventEmitter {
constructor(poolCluster, thePromise) {
super();
Expand Down Expand Up @@ -109,7 +158,7 @@
}

of(pattern, selector) {
return new PromisePoolCluster(
return new PromisePoolNamespace(
this.poolCluster.of(pattern, selector),
this.Promise,
);
Expand Down
31 changes: 31 additions & 0 deletions test/esm/integration/pool-cluster/test-promise-wrapper.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
})();
Loading