Skip to content

Commit

Permalink
refactor: split common.js into lib modules
Browse files Browse the repository at this point in the history
The proposal to put common.js into lib was good, but it felt
weird to arbitrarily stuff just some exported functions in
lib/common.js. This made sense when common.js was meant
to provide commons for index.js and promise.js. But
in the lib, this felt like a weird scope.

I therefore split common.js into

- lib/create_connection.js
- lib/create_pool.js
- lib/create_pool_cluster.js

Also made `require` more consistent in all affected files: all
`require` files now have a js suffix when they refer to a
single local file.
  • Loading branch information
tpraxl committed Sep 28, 2024
1 parent 40053ad commit fbb8ab1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 40 deletions.
19 changes: 0 additions & 19 deletions common.js

This file was deleted.

18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@
const SqlString = require('sqlstring');

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

const common = require('./common');
exports.createConnection = common.createConnection
const Connection = require('./lib/connection.js');

exports.createConnection = require('./lib/create_connection.js');
exports.connect = exports.createConnection;
exports.Connection = common.Connection;
exports.Connection = Connection
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 = common.createPool
exports.createPool = createPool

exports.createPoolCluster = common.createPoolCluster;
exports.createPoolCluster = createPoolCluster;

exports.createQuery = common.Connection.createQuery;
exports.createQuery = Connection.createQuery;

exports.Pool = Pool;

Expand All @@ -34,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
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;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"typings/mysql",
"index.js",
"index.d.ts",
"common.js",
"promise.js",
"promise.d.ts"
],
Expand Down
27 changes: 15 additions & 12 deletions promise.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use strict';

const SqlString = require('sqlstring');
const common = require('./common.js');
const EventEmitter = require('events').EventEmitter;
const parserCache = require('./lib/parsers/parser_cache.js');
const PoolConnection = require('./lib/pool_connection.js');
const Pool = require('./lib/pool.js');
const PoolCluster = require('./lib/pool_cluster.js');
const createConnection = require('./lib/create_connection.js');
const Connection = require('./lib/connection.js');
const createPool = require('./lib/create_pool.js');
const createPoolCluster = require('./lib/create_pool_cluster.js');

function makeDoneCb(resolve, reject, localErr) {
return function (err, rows, fields) {
Expand Down Expand Up @@ -252,8 +255,8 @@ class PromiseConnection extends EventEmitter {
}
}

function createConnection(opts) {
const coreConnection = common.createConnection(opts);
function createConnectionPromise(opts) {
const coreConnection = createConnection(opts);
const createConnectionErr = new Error();
const thePromise = opts.Promise || Promise;
if (!thePromise) {
Expand Down Expand Up @@ -290,12 +293,12 @@ function createConnection(opts) {
const func = functionsToWrap[i];

if (
typeof common.Connection.prototype[func] === 'function' &&
typeof Connection.prototype[func] === 'function' &&
PromiseConnection.prototype[func] === undefined
) {
PromiseConnection.prototype[func] = (function factory(funcName) {
return function () {
return common.Connection.prototype[funcName].apply(
return Connection.prototype[funcName].apply(

Check warning on line 301 in promise.js

View check run for this annotation

Codecov / codecov/patch

promise.js#L301

Added line #L301 was not covered by tests
this.connection,
arguments
);
Expand Down Expand Up @@ -411,8 +414,8 @@ class PromisePool extends EventEmitter {
}
}

function createPool(opts) {
const corePool = common.createPool(opts);
function createPromisePool(opts) {
const corePool = createPool(opts);
const thePromise = opts.Promise || Promise;
if (!thePromise) {
throw new Error(
Expand Down Expand Up @@ -545,8 +548,8 @@ class PromisePoolCluster extends EventEmitter {
'add'
]);

function createPoolCluster(opts) {
const corePoolCluster = common.createPoolCluster(opts);
function createPromisePoolCluster(opts) {
const corePoolCluster = createPoolCluster(opts);
const thePromise = (opts && opts.Promise) || Promise;
if (!thePromise) {
throw new Error(
Expand All @@ -558,9 +561,9 @@ function createPoolCluster(opts) {
return new PromisePoolCluster(corePoolCluster, thePromise);
}

exports.createConnection = createConnection;
exports.createPool = createPool;
exports.createPoolCluster = createPoolCluster;
exports.createConnection = createConnectionPromise;
exports.createPool = createPromisePool;
exports.createPoolCluster = createPromisePoolCluster;
exports.escape = SqlString.escape;
exports.escapeId = SqlString.escapeId;
exports.format = SqlString.format;
Expand Down

0 comments on commit fbb8ab1

Please sign in to comment.