Skip to content

fix: circular dependency to promise.js #1

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

Merged
merged 3 commits into from
Oct 22, 2024
Merged
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
2 changes: 1 addition & 1 deletion lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class Connection extends EventEmitter {
}

promise(promiseImpl) {
const PromiseConnection = require('../promise').PromiseConnection;
const PromiseConnection = require('./promise_connection.js');
return new PromiseConnection(this, promiseImpl);
}

Expand Down
27 changes: 27 additions & 0 deletions lib/inherit_events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

function inheritEvents(source, target, events) {
const listeners = {};
target
.on('newListener', eventName => {
if (events.indexOf(eventName) >= 0 && !target.listenerCount(eventName)) {
source.on(
eventName,
(listeners[eventName] = function () {
const args = [].slice.call(arguments);
args.unshift(eventName);

target.emit.apply(target, args);
})
);
}
})
.on('removeListener', eventName => {
if (events.indexOf(eventName) >= 0 && !target.listenerCount(eventName)) {
source.removeListener(eventName, listeners[eventName]);
delete listeners[eventName];
}
});
}

module.exports = inheritEvents
19 changes: 19 additions & 0 deletions lib/make_done_cb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

function makeDoneCb(resolve, reject, localErr) {
return function (err, rows, fields) {
if (err) {
localErr.message = err.message;
localErr.code = err.code;
localErr.errno = err.errno;
localErr.sql = err.sql;
localErr.sqlState = err.sqlState;
localErr.sqlMessage = err.sqlMessage;
reject(localErr);
} else {
resolve([rows, fields]);
}
};
}

module.exports = makeDoneCb
2 changes: 1 addition & 1 deletion lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Pool extends EventEmitter {
}

promise(promiseImpl) {
const PromisePool = require('../promise').PromisePool;
const PromisePool = require('./promise_pool.js');
return new PromisePool(this, promiseImpl);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/pool_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PoolConnection extends Connection {
}

promise(promiseImpl) {
const PromisePoolConnection = require('../promise').PromisePoolConnection;
const PromisePoolConnection = require('./promise_pool_connection.js');
return new PromisePoolConnection(this, promiseImpl);
}

Expand Down
221 changes: 221 additions & 0 deletions lib/promise_connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
'use strict';

const PromisePreparedStatementInfo = require('./promise_prepared_statement_info.js');
const makeDoneCb = require('./make_done_cb.js');
const inheritEvents = require('./inherit_events.js');
const { Connection } = require('../index.js');
const EventEmitter = require('events').EventEmitter;

class PromiseConnection extends EventEmitter {
constructor(connection, promiseImpl) {
super();
this.connection = connection;
this.Promise = promiseImpl || Promise;
inheritEvents(connection, this, [
'error',
'drain',
'connect',
'end',
'enqueue'
]);
}

release() {
this.connection.release();
}

query(query, params) {
const c = this.connection;
const localErr = new Error();
if (typeof params === 'function') {
throw new Error(
'Callback function is not available with promise clients.'
);
}
return new this.Promise((resolve, reject) => {
const done = makeDoneCb(resolve, reject, localErr);
if (params !== undefined) {
c.query(query, params, done);
} else {
c.query(query, done);
}
});
}

execute(query, params) {
const c = this.connection;
const localErr = new Error();
if (typeof params === 'function') {
throw new Error(
'Callback function is not available with promise clients.'
);
}
return new this.Promise((resolve, reject) => {
const done = makeDoneCb(resolve, reject, localErr);
if (params !== undefined) {
c.execute(query, params, done);
} else {
c.execute(query, done);
}
});
}

end() {
return new this.Promise(resolve => {
this.connection.end(resolve);
});
}

beginTransaction() {
const c = this.connection;
const localErr = new Error();
return new this.Promise((resolve, reject) => {
const done = makeDoneCb(resolve, reject, localErr);
c.beginTransaction(done);
});
}

commit() {
const c = this.connection;
const localErr = new Error();
return new this.Promise((resolve, reject) => {
const done = makeDoneCb(resolve, reject, localErr);
c.commit(done);
});
}

rollback() {
const c = this.connection;
const localErr = new Error();
return new this.Promise((resolve, reject) => {
const done = makeDoneCb(resolve, reject, localErr);
c.rollback(done);
});
}

ping() {
const c = this.connection;
const localErr = new Error();
return new this.Promise((resolve, reject) => {
c.ping(err => {
if (err) {
localErr.message = err.message;
localErr.code = err.code;
localErr.errno = err.errno;
localErr.sqlState = err.sqlState;
localErr.sqlMessage = err.sqlMessage;
reject(localErr);
} else {
resolve(true);
}
});
});
}

connect() {
const c = this.connection;
const localErr = new Error();
return new this.Promise((resolve, reject) => {
c.connect((err, param) => {
if (err) {
localErr.message = err.message;
localErr.code = err.code;
localErr.errno = err.errno;
localErr.sqlState = err.sqlState;
localErr.sqlMessage = err.sqlMessage;
reject(localErr);
} else {
resolve(param);
}
});
});
}

prepare(options) {
const c = this.connection;
const promiseImpl = this.Promise;
const localErr = new Error();
return new this.Promise((resolve, reject) => {
c.prepare(options, (err, statement) => {
if (err) {
localErr.message = err.message;
localErr.code = err.code;
localErr.errno = err.errno;
localErr.sqlState = err.sqlState;
localErr.sqlMessage = err.sqlMessage;
reject(localErr);
} else {
const wrappedStatement = new PromisePreparedStatementInfo(
statement,
promiseImpl
);
resolve(wrappedStatement);
}
});
});
}

changeUser(options) {
const c = this.connection;
const localErr = new Error();
return new this.Promise((resolve, reject) => {
c.changeUser(options, err => {
if (err) {
localErr.message = err.message;
localErr.code = err.code;
localErr.errno = err.errno;
localErr.sqlState = err.sqlState;
localErr.sqlMessage = err.sqlMessage;
reject(localErr);
} else {
resolve();
}
});
});
}

get config() {
return this.connection.config;
}

get threadId() {
return this.connection.threadId;
}
}
// patching PromiseConnection
// create facade functions for prototype functions on "Connection" that are not yet
// implemented with PromiseConnection

// proxy synchronous functions only
(function (functionsToWrap) {
for (let i = 0; functionsToWrap && i < functionsToWrap.length; i++) {
const func = functionsToWrap[i];

if (
typeof Connection.prototype[func] === 'function' &&
PromiseConnection.prototype[func] === undefined
) {
PromiseConnection.prototype[func] = (function factory(funcName) {
return function () {
return Connection.prototype[funcName].apply(
this.connection,
arguments
);
};
})(func);
}
}
})([
// synchronous functions
'close',
'createBinlogStream',
'destroy',
'escape',
'escapeId',
'format',
'pause',
'pipe',
'resume',
'unprepare'
]);
module.exports = PromiseConnection
Loading