Skip to content

Commit 9c7f5f6

Browse files
authored
Merge pull request #15 from rodrigogs/allow_connection_pool
Allowing connection pools
2 parents c95de42 + e5632c8 commit 9c7f5f6

7 files changed

Lines changed: 2058 additions & 3281 deletions

File tree

‎.npmignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/.vscode/
12
/.idea/
23
/.circleci/
34
/example/

‎lib/MySQLEvents.js‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ const connectionHandler = require('./connectionHandler');
77
const EVENTS = require('./EVENTS.enum');
88
const STATEMENTS = require('./STATEMENTS.enum');
99

10+
/**
11+
* @param {Object|Connection|String} connection
12+
* @param {Object} options
13+
*/
1014
class MySQLEvents extends EventEmitter {
11-
/**
12-
* @param {Object|Connection|String} connection
13-
* @param {Object} options
14-
*/
1515
constructor(connection, options = {}) {
1616
super();
1717

@@ -88,7 +88,7 @@ class MySQLEvents extends EventEmitter {
8888
this.connection = await connectionHandler(this.connection);
8989

9090
debug('initializing zongji');
91-
this.zongJi = new ZongJi(this.connection, Object.assign(this.options, options));
91+
this.zongJi = new ZongJi(this.connection, Object.assign({}, this.options, options));
9292

9393
debug('connected');
9494
this.emit('connected');
@@ -107,7 +107,7 @@ class MySQLEvents extends EventEmitter {
107107
debug('disconnecting from mysql');
108108

109109
this.zongJi.stop();
110-
this.zongJi = null;
110+
delete this.zongJi;
111111

112112
await new Promise((resolve, reject) => {
113113
this.connection.end((err) => {
@@ -129,7 +129,6 @@ class MySQLEvents extends EventEmitter {
129129
if (!this.isStarted || this.isPaused) return;
130130
debug('pausing connection');
131131

132-
this.zongJi.ctrlConnection.pause();
133132
this.zongJi.connection.pause();
134133
this.isPaused = true;
135134
this.emit(EVENTS.PAUSED);
@@ -142,7 +141,6 @@ class MySQLEvents extends EventEmitter {
142141
if (!this.isStarted || !this.isPaused) return;
143142
debug('resuming connection');
144143

145-
this.zongJi.ctrlConnection.resume();
146144
this.zongJi.connection.resume();
147145
this.isPaused = false;
148146
this.emit(EVENTS.RESUMED);

‎lib/connectionHandler.js‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
const debug = require('debuggler')();
22
const mysql = require('mysql');
33
const Connection = require('mysql/lib/Connection');
4+
const Pool = require('mysql/lib/Pool');
45

56
const connect = connection => new Promise((resolve, reject) => connection.connect((err) => {
67
if (err) return reject(err);
78
resolve();
89
}));
910

1011
const connectionHandler = async (connection) => {
12+
if (connection instanceof Pool) {
13+
debug('reusing pool:', connection);
14+
if (connection._closed) {
15+
connection = mysql.createPool(connection.config.connectionConfig);
16+
}
17+
}
18+
1119
if (connection instanceof Connection) {
12-
debug('creating connection from previous connection object:', connection);
20+
debug('reusing connection:', connection);
1321
if (connection.state !== 'connected') {
1422
connection = mysql.createConnection(connection.config);
1523
}
@@ -20,12 +28,16 @@ const connectionHandler = async (connection) => {
2028
connection = mysql.createConnection(connection);
2129
}
2230

23-
if ((typeof connection === 'object') && !(connection instanceof Connection)) {
31+
if ((typeof connection === 'object') && (!(connection instanceof Connection) && !(connection instanceof Pool))) {
2432
debug('creating connection from object:', connection);
25-
connection = mysql.createConnection(connection);
33+
if (connection.isPool) {
34+
connection = mysql.createPool(connection);
35+
} else {
36+
connection = mysql.createConnection(connection);
37+
}
2638
}
2739

28-
if (connection.state !== 'connected') {
40+
if ((connection instanceof Connection) && (connection.state !== 'connected')) {
2941
debug('initializing connection');
3042
await connect(connection);
3143
}

0 commit comments

Comments
 (0)