Skip to content

Commit

Permalink
feat(poolCluster): add execute method
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxizhe committed Aug 4, 2021
1 parent 1b542f8 commit 703a9e3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/pool_cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,32 @@ class PoolNamespace {
return query;
}

/**
* pool cluster execute
* @param {*} sql
* @param {*} values
* @param {*} cb
*/
execute(sql, values, cb) {
if (typeof values === 'function') {
cb = values;
values = [];
}
this.getConnection((err, conn) => {
if (err) {
return cb(err);
}
try {
conn.execute(sql, values, cb).once('end', () => {
conn.release();
});
} catch (e) {
conn.release();
throw e;
}
});
}

_getClusterNode() {
const foundNodeIds = this._cluster._findNodeIds(this._pattern);
if (foundNodeIds.length === 0) {
Expand Down
14 changes: 14 additions & 0 deletions promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,20 @@ class PromisePoolCluster extends EventEmitter {
});
}

execute(sql, args) {
const corePoolCluster = this.poolCluster;
const localErr = new Error();
if (typeof args === 'function') {
throw new Error(
'Callback function is not available with promise clients.'
);
}
return new this.Promise((resolve, reject) => {
const done = makeDoneCb(resolve, reject, localErr);
corePoolCluster.execute(sql, args, done);
});
}

of(pattern, selector) {
return new PromisePoolCluster(
this.poolCluster.of(pattern, selector),
Expand Down

0 comments on commit 703a9e3

Please sign in to comment.