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: circular dependencies #3081

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
24 changes: 9 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,24 @@

const SqlString = require('sqlstring');

const Connection = require('./lib/connection.js');
const ConnectionConfig = require('./lib/connection_config.js');
const parserCache = require('./lib/parsers/parser_cache');
const parserCache = require('./lib/parsers/parser_cache.js');

exports.createConnection = function(opts) {
return new Connection({ config: new ConnectionConfig(opts) });
};
const Connection = require('./lib/connection.js');

exports.createConnection = require('./lib/create_connection.js');
exports.connect = exports.createConnection;
exports.Connection = Connection;
exports.Connection = Connection
tpraxl marked this conversation as resolved.
Show resolved Hide resolved
exports.ConnectionConfig = ConnectionConfig;

const Pool = require('./lib/pool.js');
const PoolCluster = require('./lib/pool_cluster.js');
const createPool = require('./lib/create_pool.js');
const createPoolCluster = require('./lib/create_pool_cluster.js');

exports.createPool = function(config) {
const PoolConfig = require('./lib/pool_config.js');
return new Pool({ config: new PoolConfig(config) });
};
exports.createPool = createPool
tpraxl marked this conversation as resolved.
Show resolved Hide resolved

exports.createPoolCluster = function(config) {
const PoolCluster = require('./lib/pool_cluster.js');
return new PoolCluster(config);
};
exports.createPoolCluster = createPoolCluster;

exports.createQuery = Connection.createQuery;

Expand All @@ -42,7 +36,7 @@ exports.createServer = function(handler) {
return s;
};

exports.PoolConnection = require('./lib/pool_connection');
exports.PoolConnection = require('./lib/pool_connection.js');
exports.authPlugins = require('./lib/auth_plugins');
exports.escape = SqlString.escape;
exports.escapeId = SqlString.escapeId;
Expand Down
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
10 changes: 10 additions & 0 deletions lib/create_connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const Connection = require('./connection.js');
const ConnectionConfig = require('./connection_config.js');

function createConnection(opts) {
return new Connection({ config: new ConnectionConfig(opts) });
}

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

const Pool = require('./pool.js');
const PoolConfig = require('./pool_config.js');

function createPool(config) {
return new Pool({ config: new PoolConfig(config) });
}

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

const PoolCluster = require('./pool_cluster.js');

function createPoolCluster(config) {
return new PoolCluster(config);
}

module.exports = createPoolCluster;
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
tpraxl marked this conversation as resolved.
Show resolved Hide resolved
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
tpraxl marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 5 additions & 6 deletions lib/pool.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';

const process = require('process');
const mysql = require('../index.js');

const SqlString = require('sqlstring');
const EventEmitter = require('events').EventEmitter;
const PoolConnection = require('./pool_connection.js');
const Queue = require('denque');
Expand Down Expand Up @@ -34,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 Expand Up @@ -214,7 +213,7 @@ class Pool extends EventEmitter {
}

format(sql, values) {
return mysql.format(
return SqlString.format(
sql,
values,
this.config.connectionConfig.stringifyObjects,
Expand All @@ -223,15 +222,15 @@ class Pool extends EventEmitter {
}

escape(value) {
return mysql.escape(
return SqlString.escape(
value,
this.config.connectionConfig.stringifyObjects,
this.config.connectionConfig.timezone
);
}

escapeId(value) {
return mysql.escapeId(value, false);
return SqlString.escapeId(value, false);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/pool_connection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Connection = require('../index.js').Connection;
const Connection = require('./connection.js');

class PoolConnection extends Connection {
constructor(pool, options) {
Expand Down Expand Up @@ -30,7 +30,7 @@
}

promise(promiseImpl) {
const PromisePoolConnection = require('../promise').PromisePoolConnection;
const PromisePoolConnection = require('./promise_pool_connection.js');

Check warning on line 33 in lib/pool_connection.js

View check run for this annotation

Codecov / codecov/patch

lib/pool_connection.js#L33

Added line #L33 was not covered by tests
return new PromisePoolConnection(this, promiseImpl);
}

Expand Down
Loading
Loading