-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·252 lines (214 loc) · 5.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* jshint esversion: 6 */
const mysql2 = require('mysql2/promise');
let instances = {};
function DB() {
this.pool = null;
this.sessionConfig = null;
this.debug = false;
}
function setSessionConfig(connection, config) {
if(!config) {
return;
}
if(this.debug) {
console.log(`Set Session Config ${config}`);
}
return Promise.all(Object.entries(config).map(([key, value]) =>
connection.query(`SET SESSION ${key} = ?`, [value])
));
}
function runQueryWith(query, params) {
let connection;
return this.getConnection().then((conn)=> {
connection = conn;
if (this.debug) {
console.log('getting connection');
console.info('QUERY AND PARAMS', query, params);
}
return connection.execute(query, params);
}).then((results) => {
connection && connection.connection && connection.connection.unprepare(query);
connection && connection.release();
if (this.debug) {
console.log('ok - released connection');
}
return results[0];
}).catch(err => {
if (!connection) {
console.error('could not establish db connection', err.message);
throw err;
}
connection.connection && connection.connection.unprepare(query);
connection.release();
throw err;
});
}
function flatten(params) {
return params.reduce((acc, param) =>
Array.isArray(param) ? [...acc, ...param] : [...acc, param],
[]);
}
/**
* If params contain arrays, the corresponding placeholders are expanded
* with extra placeholders to match arrays' lengths and params are flattened.
*
* @param {String} query
* @param {Array} params
* @return {Array}
*/
DB.prototype.prepareQuery = function(query, params) {
if(Array.isArray(params)){
let i = 0;
query = query.replace(/\?/g, function (match){
let param = params[i++];
return Array.isArray(param) ? Array(param.length).fill('?').join(',') : '?'
});
params = flatten(params);
}
return [query, params];
}
/**
* Format a query as a preapred statement for bulk insert.
*
* @param {String} query
* @param {Array} values
* @return {Array}
*/
DB.prototype.prepareBulk = function(query, values) {
if(!Array.isArray(values) || (values[0] && !Array.isArray(values[0]))) {
throw new Error(`Please provide an array of arrays for bulk insert like [[1,2], [1,3]]`);
}
let placeholders = '';
for (let i = 0; i < values.length; i++) {
placeholders += `(${Array(values[i].length).fill('?')})`;
placeholders += `${(values.length - 1 != i) ? ',' : ''}`;
}
return [
query.replace(`?`, placeholders),
values.reduce((acc, cur) => acc.concat(cur), [])
];
}
/**
* We are overiding the select method so that it returns the rows not the metadata
* that is included in results[1]
* @return {Object} Connection
*/
DB.prototype.getConnection = function () {
return this.pool.getConnection().then((conn)=> {
if (this.debug) {
console.log('getting connection');
}
let exec = conn.execute;
conn.select = function(query, params) {
return exec.call(this, query, params).then((results) => {
return results[0];
});
};
if(this.sessionConfig) {
return setSessionConfig(conn, this.sessionConfig).then(() => conn)
}
return conn;
});
};
/**
* Setup the Database connection pool for this instance
* @param {Object} config
*/
DB.prototype.configure = function (config) {
if ('debug' in config) {
this.debug = config.debug;
delete config.debug;
}
if ('sessionConfig' in config) {
this.sessionConfig = config.sessionConfig;
delete config.sessionConfig;
}
this.pool = mysql2.createPool(config);
};
/**
* Run a DB query using prepared statements. This function does not support batch
* operations.
* @param {String} query
* @param {Object} [params]
* @return {Promise}
*/
DB.prototype.query = function(query, params) {
if(Array.isArray(params)){
[query, params] = this.prepareQuery(query, params);
}
return runQueryWith.call(this, query, params);
}
/**
* Run a DB query using prepared statements. This function supports batch operations
*
* @param {String} query
* @param {Object} [params]
* @return {Promise}
*/
DB.prototype.bulk = function(query, params) {
[query, params] = this.prepareBulk(query, params);
return runQueryWith.call(this, query, params);
}
/**
* Set Session wait timeout parameter to close connection if inactive
* Initiate transaction so all the subsequent queries to the db are not written
* to the database until commit is called
* @param {String} timeout
*
* @return {object} connection
**/
DB.prototype.startTransaction = function (timeout) {
timeout = timeout || 20;
let connection;
return this.getConnection().then(conn => {
connection = conn;
return connection.query('SET SESSION wait_timeout = ?', [timeout]);
}).then(()=> {
return connection.query('START TRANSACTION');
}).then(() => {
return connection;
});
};
/**
* Rollback the current transaction
* @param {Object} connection The connection object from startTransaction
*/
DB.prototype.rollback = function (connection) {
if(connection){
return connection.execute('ROLLBACK').then(()=> {
connection.release();
}).catch(err => {
connection.release();
throw err;
});
}
};
/**
* Commit the current transaction
* @param {Object} connection The connection object from startTransaction
*/
DB.prototype.commit = function (connection) {
return connection.execute('COMMIT').then(()=> {
connection.release();
}).catch(err => {
connection.release();
throw err;
});
};
module.exports = function (opts) {
if (!opts || Object.keys(opts).length <= 0) {
throw new Error('The config object cannot be empty');
}
let config = JSON.parse(JSON.stringify(opts));
let name = '_default_';
if (config.name) {
name = config.name;
delete config.name;
}
if (!instances[name]) {
let instance = new DB();
instance.configure(config);
instances[name] = instance;
}
return instances[name];
};