mysql2-core 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js ADDED
@@ -0,0 +1,990 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function (t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function () { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function () { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ function __export(m) {
50
+ for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
51
+ }
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ var mysql2_1 = require("mysql2");
54
+ var build_1 = require("./build");
55
+ __export(require("./build"));
56
+ var resource = (function () {
57
+ function resource() {
58
+ }
59
+ return resource;
60
+ }());
61
+ exports.resource = resource;
62
+ function createPool(conf) {
63
+ if (conf.max && conf.max > 0 && !conf.connectionLimit) {
64
+ conf.connectionLimit = conf.max;
65
+ }
66
+ var pool = mysql2_1.createPool(__assign(__assign({}, conf), { rowsAsArray: true, multipleStatements: true }));
67
+ return pool;
68
+ }
69
+ exports.createPool = createPool;
70
+ function execBatch(pool, statements, firstSuccess) {
71
+ if (!statements || statements.length === 0) {
72
+ return Promise.resolve(0);
73
+ }
74
+ else if (statements.length === 1) {
75
+ return exec(pool, statements[0].query, statements[0].params);
76
+ }
77
+ if (firstSuccess) {
78
+ return new Promise(function (resolve, reject) {
79
+ pool.getConnection(function (er0, connection) {
80
+ if (er0) {
81
+ return reject(er0);
82
+ }
83
+ connection.beginTransaction(function (er1) {
84
+ if (er1) {
85
+ connection.rollback(function () {
86
+ return reject(er1);
87
+ });
88
+ }
89
+ else {
90
+ var query0 = '';
91
+ var queries_1 = [];
92
+ var l = statements.length;
93
+ for (var j = 0; j < l; j++) {
94
+ var item = statements[j];
95
+ if (j === 0) {
96
+ query0 = mysql2_1.format(item.query, toArray(item.params));
97
+ }
98
+ else {
99
+ if (item.query.endsWith(';')) {
100
+ queries_1.push(mysql2_1.format(item.query, toArray(item.params)));
101
+ }
102
+ else {
103
+ queries_1.push(mysql2_1.format(item.query + ';', toArray(item.params)));
104
+ }
105
+ }
106
+ }
107
+ connection.query(query0, function (er2a, results0) {
108
+ if (er2a) {
109
+ connection.rollback(function () {
110
+ return reject(er2a);
111
+ });
112
+ }
113
+ else {
114
+ if (results0 && results0.affectedRows === 0) {
115
+ return 0;
116
+ }
117
+ else {
118
+ connection.query(queries_1.join(''), function (er2, results) {
119
+ if (er2) {
120
+ connection.rollback(function () {
121
+ return reject(er2);
122
+ });
123
+ }
124
+ else {
125
+ connection.commit(function (er3) {
126
+ if (er3) {
127
+ connection.rollback(function () {
128
+ return reject(er3);
129
+ });
130
+ }
131
+ });
132
+ var c = 0;
133
+ c += results0.affectedRows + results.reduce(function (prev, item) { return prev + item.affectedRows; }, 0);
134
+ return resolve(c);
135
+ }
136
+ });
137
+ }
138
+ }
139
+ });
140
+ }
141
+ });
142
+ });
143
+ });
144
+ }
145
+ else {
146
+ return new Promise(function (resolve, reject) {
147
+ pool.getConnection(function (er0, connection) {
148
+ if (er0) {
149
+ return reject(er0);
150
+ }
151
+ connection.beginTransaction(function (er1) {
152
+ if (er1) {
153
+ connection.rollback(function () {
154
+ return reject(er1);
155
+ });
156
+ }
157
+ else {
158
+ var queries_2 = [];
159
+ statements.forEach(function (item) {
160
+ if (item.query.endsWith(';')) {
161
+ queries_2.push(mysql2_1.format(item.query, toArray(item.params)));
162
+ }
163
+ else {
164
+ queries_2.push(mysql2_1.format(item.query + ';', toArray(item.params)));
165
+ }
166
+ });
167
+ connection.query(queries_2.join(''), function (er2, results) {
168
+ if (er2) {
169
+ connection.rollback(function () {
170
+ buildError(er2);
171
+ return reject(er2);
172
+ });
173
+ }
174
+ else {
175
+ connection.commit(function (er3) {
176
+ if (er3) {
177
+ connection.rollback(function () {
178
+ return reject(er3);
179
+ });
180
+ }
181
+ });
182
+ return resolve(results.reduce(function (prev, item) { return prev + item.affectedRows; }, 0));
183
+ }
184
+ });
185
+ }
186
+ });
187
+ });
188
+ });
189
+ }
190
+ }
191
+ exports.execBatch = execBatch;
192
+ function buildError(err) {
193
+ if (err.errno === 1062 && err.code === 'ER_DUP_ENTRY') {
194
+ err.error = 'duplicate';
195
+ }
196
+ return err;
197
+ }
198
+ function exec(pool, sql, args) {
199
+ return __awaiter(this, void 0, void 0, function () {
200
+ var p;
201
+ return __generator(this, function (_a) {
202
+ p = toArray(args);
203
+ return [2, new Promise(function (resolve, reject) {
204
+ return pool.execute(sql, p, function (err, res) {
205
+ if (err) {
206
+ buildError(err);
207
+ return reject(err);
208
+ }
209
+ else {
210
+ return resolve(res.affectedRows);
211
+ }
212
+ });
213
+ })];
214
+ });
215
+ });
216
+ }
217
+ exports.exec = exec;
218
+ function query(pool, sql, args, m, bools) {
219
+ return __awaiter(this, void 0, void 0, function () {
220
+ var p;
221
+ return __generator(this, function (_a) {
222
+ p = toArray(args);
223
+ return [2, new Promise(function (resolve, reject) {
224
+ return pool.query(sql, p, function (err, results, fields) {
225
+ if (err) {
226
+ return reject(err);
227
+ }
228
+ else {
229
+ if (results.length > 0) {
230
+ var arrayResult = results.map(function (item) {
231
+ return formatData(fields, item);
232
+ });
233
+ return resolve(handleResults(arrayResult, m, bools));
234
+ }
235
+ else {
236
+ resolve([]);
237
+ }
238
+ }
239
+ });
240
+ })];
241
+ });
242
+ });
243
+ }
244
+ exports.query = query;
245
+ function queryOne(pool, sql, args, m, bools) {
246
+ return query(pool, sql, args, m, bools).then(function (r) {
247
+ return (r && r.length > 0 ? r[0] : null);
248
+ });
249
+ }
250
+ exports.queryOne = queryOne;
251
+ function execScalar(pool, sql, args) {
252
+ return queryOne(pool, sql, args).then(function (r) {
253
+ if (!r) {
254
+ return null;
255
+ }
256
+ else {
257
+ var keys = Object.keys(r);
258
+ return r[keys[0]];
259
+ }
260
+ });
261
+ }
262
+ exports.execScalar = execScalar;
263
+ function count(pool, sql, args) {
264
+ return execScalar(pool, sql, args);
265
+ }
266
+ exports.count = count;
267
+ function save(pool, obj, table, attrs, ver, buildParam, i) {
268
+ var s = build_1.buildToSave(obj, table, attrs, ver, buildParam);
269
+ if (!s) {
270
+ return Promise.resolve(-1);
271
+ }
272
+ if (typeof pool === 'function') {
273
+ return pool(s.query, s.params);
274
+ }
275
+ else {
276
+ return exec(pool, s.query, s.params);
277
+ }
278
+ }
279
+ exports.save = save;
280
+ function saveBatch(pool, objs, table, attrs, ver, buildParam) {
281
+ var s = build_1.buildToSaveBatch(objs, table, attrs, ver, buildParam);
282
+ if (typeof pool === 'function') {
283
+ return pool(s);
284
+ }
285
+ else {
286
+ return execBatch(pool, s);
287
+ }
288
+ }
289
+ exports.saveBatch = saveBatch;
290
+ function toArray(arr) {
291
+ if (!arr || arr.length === 0) {
292
+ return [];
293
+ }
294
+ var p = [];
295
+ var l = arr.length;
296
+ for (var i = 0; i < l; i++) {
297
+ if (arr[i] === undefined || arr[i] == null) {
298
+ p.push(null);
299
+ }
300
+ else {
301
+ if (typeof arr[i] === 'object') {
302
+ if (arr[i] instanceof Date) {
303
+ p.push(arr[i]);
304
+ }
305
+ else {
306
+ if (resource.string) {
307
+ var s = JSON.stringify(arr[i]);
308
+ p.push(s);
309
+ }
310
+ else {
311
+ p.push(arr[i]);
312
+ }
313
+ }
314
+ }
315
+ else {
316
+ p.push(arr[i]);
317
+ }
318
+ }
319
+ }
320
+ return p;
321
+ }
322
+ exports.toArray = toArray;
323
+ function handleResults(r, m, bools) {
324
+ if (m) {
325
+ var res = mapArray(r, m);
326
+ if (bools && bools.length > 0) {
327
+ return handleBool(res, bools);
328
+ }
329
+ else {
330
+ return res;
331
+ }
332
+ }
333
+ else {
334
+ if (bools && bools.length > 0) {
335
+ return handleBool(r, bools);
336
+ }
337
+ else {
338
+ return r;
339
+ }
340
+ }
341
+ }
342
+ exports.handleResults = handleResults;
343
+ function handleBool(objs, bools) {
344
+ if (!bools || bools.length === 0 || !objs) {
345
+ return objs;
346
+ }
347
+ for (var _i = 0, objs_1 = objs; _i < objs_1.length; _i++) {
348
+ var obj = objs_1[_i];
349
+ var o = obj;
350
+ for (var _a = 0, bools_1 = bools; _a < bools_1.length; _a++) {
351
+ var field = bools_1[_a];
352
+ if (field.name) {
353
+ var v = o[field.name];
354
+ if (typeof v !== 'boolean' && v != null && v !== undefined) {
355
+ var b = field.true;
356
+ if (b == null || b === undefined) {
357
+ o[field.name] = ('1' == v || 'T' == v || 'Y' == v || 'true' == v || 'on' == v);
358
+ }
359
+ else {
360
+ o[field.name] = (v == b ? true : false);
361
+ }
362
+ }
363
+ }
364
+ }
365
+ }
366
+ return objs;
367
+ }
368
+ exports.handleBool = handleBool;
369
+ function formatData(nameColumn, data, m) {
370
+ var result = {};
371
+ nameColumn.forEach(function (item, index) {
372
+ var key = item.name;
373
+ if (m) {
374
+ key = m[item.name];
375
+ }
376
+ result[key] = data[index];
377
+ });
378
+ return result;
379
+ }
380
+ exports.formatData = formatData;
381
+ function map(obj, m) {
382
+ if (!m) {
383
+ return obj;
384
+ }
385
+ var mkeys = Object.keys(m);
386
+ if (mkeys.length === 0) {
387
+ return obj;
388
+ }
389
+ var o = {};
390
+ var keys = Object.keys(obj);
391
+ for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
392
+ var key = keys_1[_i];
393
+ var k0 = m[key];
394
+ if (!k0) {
395
+ k0 = key;
396
+ }
397
+ o[k0] = obj[key];
398
+ }
399
+ return o;
400
+ }
401
+ exports.map = map;
402
+ function mapArray(results, m) {
403
+ if (!m) {
404
+ return results;
405
+ }
406
+ var mkeys = Object.keys(m);
407
+ if (mkeys.length === 0) {
408
+ return results;
409
+ }
410
+ var objs = [];
411
+ var length = results.length;
412
+ for (var i = 0; i < length; i++) {
413
+ var obj = results[i];
414
+ var obj2 = {};
415
+ var keys = Object.keys(obj);
416
+ for (var _i = 0, keys_2 = keys; _i < keys_2.length; _i++) {
417
+ var key = keys_2[_i];
418
+ var k0 = m[key];
419
+ if (!k0) {
420
+ k0 = key;
421
+ }
422
+ obj2[k0] = obj[key];
423
+ }
424
+ objs.push(obj2);
425
+ }
426
+ return objs;
427
+ }
428
+ exports.mapArray = mapArray;
429
+ function getFields(fields, all) {
430
+ if (!fields || fields.length === 0) {
431
+ return undefined;
432
+ }
433
+ var ext = [];
434
+ if (all) {
435
+ for (var _i = 0, fields_1 = fields; _i < fields_1.length; _i++) {
436
+ var s = fields_1[_i];
437
+ if (all.includes(s)) {
438
+ ext.push(s);
439
+ }
440
+ }
441
+ if (ext.length === 0) {
442
+ return undefined;
443
+ }
444
+ else {
445
+ return ext;
446
+ }
447
+ }
448
+ else {
449
+ return fields;
450
+ }
451
+ }
452
+ exports.getFields = getFields;
453
+ function buildFields(fields, all) {
454
+ var s = getFields(fields, all);
455
+ if (!s || s.length === 0) {
456
+ return '*';
457
+ }
458
+ else {
459
+ return s.join(',');
460
+ }
461
+ }
462
+ exports.buildFields = buildFields;
463
+ function getMapField(name, mp) {
464
+ if (!mp) {
465
+ return name;
466
+ }
467
+ var x = mp[name];
468
+ if (!x) {
469
+ return name;
470
+ }
471
+ if (typeof x === 'string') {
472
+ return x;
473
+ }
474
+ return name;
475
+ }
476
+ exports.getMapField = getMapField;
477
+ function isEmpty(s) {
478
+ return !(s && s.length > 0);
479
+ }
480
+ exports.isEmpty = isEmpty;
481
+ var StringService = (function () {
482
+ function StringService(pool, table, column) {
483
+ this.pool = pool;
484
+ this.table = table;
485
+ this.column = column;
486
+ this.load = this.load.bind(this);
487
+ this.save = this.save.bind(this);
488
+ }
489
+ StringService.prototype.load = function (key, max) {
490
+ var _this = this;
491
+ var s = "select " + this.column + " from " + this.table + " where " + this.column + " like ? order by " + this.column + " limit " + max;
492
+ return query(this.pool, s, ['' + key + '%']).then(function (arr) {
493
+ return arr.map(function (i) { return i[_this.column]; });
494
+ });
495
+ };
496
+ StringService.prototype.save = function (values) {
497
+ if (!values || values.length === 0) {
498
+ return Promise.resolve(0);
499
+ }
500
+ var arr = [];
501
+ for (var i = 1; i <= values.length; i++) {
502
+ arr.push('(?)');
503
+ }
504
+ var s = "insert ignore into " + this.table + "(" + this.column + ")values" + arr.join(',');
505
+ return exec(this.pool, s, values);
506
+ };
507
+ return StringService;
508
+ }());
509
+ exports.StringService = StringService;
510
+ function version(attrs) {
511
+ var ks = Object.keys(attrs);
512
+ for (var _i = 0, ks_1 = ks; _i < ks_1.length; _i++) {
513
+ var k = ks_1[_i];
514
+ var attr = attrs[k];
515
+ if (attr.version) {
516
+ attr.name = k;
517
+ return attr;
518
+ }
519
+ }
520
+ return undefined;
521
+ }
522
+ exports.version = version;
523
+ var MySQLWriter = (function () {
524
+ function MySQLWriter(pool, table, attributes, oneIfSuccess, toDB, buildParam) {
525
+ this.table = table;
526
+ this.attributes = attributes;
527
+ this.oneIfSuccess = oneIfSuccess;
528
+ this.write = this.write.bind(this);
529
+ if (typeof pool === 'function') {
530
+ this.exec = pool;
531
+ }
532
+ else {
533
+ this.pool = pool;
534
+ }
535
+ this.param = buildParam;
536
+ this.map = toDB;
537
+ var x = version(attributes);
538
+ if (x) {
539
+ this.version = x.name;
540
+ }
541
+ }
542
+ MySQLWriter.prototype.write = function (obj) {
543
+ if (!obj) {
544
+ return Promise.resolve(0);
545
+ }
546
+ var obj2 = obj;
547
+ if (this.map) {
548
+ obj2 = this.map(obj);
549
+ }
550
+ var stmt = build_1.buildToSave(obj2, this.table, this.attributes, this.version, this.param);
551
+ if (stmt) {
552
+ if (this.exec) {
553
+ if (this.oneIfSuccess) {
554
+ return this.exec(stmt.query, stmt.params).then(function (ct) { return ct > 0 ? 1 : 0; });
555
+ }
556
+ else {
557
+ return this.exec(stmt.query, stmt.params);
558
+ }
559
+ }
560
+ else {
561
+ if (this.oneIfSuccess) {
562
+ return exec(this.pool, stmt.query, stmt.params).then(function (ct) { return ct > 0 ? 1 : 0; });
563
+ }
564
+ else {
565
+ return exec(this.pool, stmt.query, stmt.params);
566
+ }
567
+ }
568
+ }
569
+ else {
570
+ return Promise.resolve(0);
571
+ }
572
+ };
573
+ return MySQLWriter;
574
+ }());
575
+ exports.MySQLWriter = MySQLWriter;
576
+ var MySQLStreamWriter = (function () {
577
+ function MySQLStreamWriter(pool, table, attributes, size, toDB, buildParam) {
578
+ this.table = table;
579
+ this.attributes = attributes;
580
+ this.list = [];
581
+ this.size = 0;
582
+ this.write = this.write.bind(this);
583
+ this.flush = this.flush.bind(this);
584
+ if (typeof pool === 'function') {
585
+ this.execBatch = pool;
586
+ }
587
+ else {
588
+ this.pool = pool;
589
+ }
590
+ this.param = buildParam;
591
+ this.map = toDB;
592
+ var x = version(attributes);
593
+ if (x) {
594
+ this.version = x.name;
595
+ }
596
+ if (size) {
597
+ this.size = size;
598
+ }
599
+ }
600
+ MySQLStreamWriter.prototype.write = function (obj) {
601
+ if (!obj) {
602
+ return Promise.resolve(0);
603
+ }
604
+ var obj2 = obj;
605
+ if (this.map) {
606
+ obj2 = this.map(obj);
607
+ this.list.push(obj2);
608
+ }
609
+ else {
610
+ this.list.push(obj);
611
+ }
612
+ if (this.list.length < this.size) {
613
+ return Promise.resolve(0);
614
+ }
615
+ else {
616
+ return this.flush();
617
+ }
618
+ };
619
+ MySQLStreamWriter.prototype.flush = function () {
620
+ var _this = this;
621
+ if (!this.list || this.list.length === 0) {
622
+ return Promise.resolve(0);
623
+ }
624
+ else {
625
+ var total_1 = this.list.length;
626
+ var stmt = build_1.buildToSaveBatch(this.list, this.table, this.attributes, this.version, this.param);
627
+ if (stmt) {
628
+ if (this.execBatch) {
629
+ return this.execBatch(stmt).then(function (r) {
630
+ _this.list = [];
631
+ return total_1;
632
+ });
633
+ }
634
+ else {
635
+ return execBatch(this.pool, stmt).then(function (r) {
636
+ _this.list = [];
637
+ return total_1;
638
+ });
639
+ }
640
+ }
641
+ else {
642
+ return Promise.resolve(0);
643
+ }
644
+ }
645
+ };
646
+ return MySQLStreamWriter;
647
+ }());
648
+ exports.MySQLStreamWriter = MySQLStreamWriter;
649
+ var MySQLBatchWriter = (function () {
650
+ function MySQLBatchWriter(pool, table, attributes, oneIfSuccess, toDB, buildParam) {
651
+ this.table = table;
652
+ this.attributes = attributes;
653
+ this.oneIfSuccess = oneIfSuccess;
654
+ this.write = this.write.bind(this);
655
+ if (typeof pool === 'function') {
656
+ this.execute = pool;
657
+ }
658
+ else {
659
+ this.pool = pool;
660
+ }
661
+ this.param = buildParam;
662
+ this.map = toDB;
663
+ var x = version(attributes);
664
+ if (x) {
665
+ this.version = x.name;
666
+ }
667
+ }
668
+ MySQLBatchWriter.prototype.write = function (objs) {
669
+ if (!objs || objs.length === 0) {
670
+ return Promise.resolve(0);
671
+ }
672
+ var list = objs;
673
+ if (this.map) {
674
+ list = [];
675
+ for (var _i = 0, objs_2 = objs; _i < objs_2.length; _i++) {
676
+ var obj = objs_2[_i];
677
+ var obj2 = this.map(obj);
678
+ list.push(obj2);
679
+ }
680
+ }
681
+ var stmts = build_1.buildToSaveBatch(list, this.table, this.attributes, this.version, this.param);
682
+ if (stmts && stmts.length > 0) {
683
+ if (this.execute) {
684
+ if (this.oneIfSuccess) {
685
+ return this.execute(stmts).then(function (ct) { return stmts.length; });
686
+ }
687
+ else {
688
+ return this.execute(stmts);
689
+ }
690
+ }
691
+ else {
692
+ if (this.oneIfSuccess) {
693
+ return execBatch(this.pool, stmts).then(function (ct) { return stmts.length; });
694
+ }
695
+ else {
696
+ return execBatch(this.pool, stmts);
697
+ }
698
+ }
699
+ }
700
+ else {
701
+ return Promise.resolve(0);
702
+ }
703
+ };
704
+ return MySQLBatchWriter;
705
+ }());
706
+ exports.MySQLBatchWriter = MySQLBatchWriter;
707
+ var MySQLChecker = (function () {
708
+ function MySQLChecker(pool, service, timeout) {
709
+ this.pool = pool;
710
+ this.timeout = (timeout ? timeout : 4200);
711
+ this.service = (service ? service : 'mysql');
712
+ this.check = this.check.bind(this);
713
+ this.name = this.name.bind(this);
714
+ this.build = this.build.bind(this);
715
+ }
716
+ MySQLChecker.prototype.check = function () {
717
+ return __awaiter(this, void 0, void 0, function () {
718
+ var obj, promise;
719
+ var _this = this;
720
+ return __generator(this, function (_a) {
721
+ obj = {};
722
+ promise = new Promise(function (resolve, reject) {
723
+ _this.pool.query('select current_time', function (err, result) {
724
+ if (err) {
725
+ return reject(err);
726
+ }
727
+ else {
728
+ resolve(obj);
729
+ }
730
+ });
731
+ });
732
+ if (this.timeout > 0) {
733
+ return [2, promiseTimeOut(this.timeout, promise)];
734
+ }
735
+ else {
736
+ return [2, promise];
737
+ }
738
+ return [2];
739
+ });
740
+ });
741
+ };
742
+ MySQLChecker.prototype.name = function () {
743
+ return this.service;
744
+ };
745
+ MySQLChecker.prototype.build = function (data, err) {
746
+ if (err) {
747
+ if (!data) {
748
+ data = {};
749
+ }
750
+ data['error'] = err;
751
+ }
752
+ return data;
753
+ };
754
+ return MySQLChecker;
755
+ }());
756
+ exports.MySQLChecker = MySQLChecker;
757
+ function promiseTimeOut(timeoutInMilliseconds, promise) {
758
+ return Promise.race([
759
+ promise,
760
+ new Promise(function (resolve, reject) {
761
+ setTimeout(function () {
762
+ reject("Timed out in: " + timeoutInMilliseconds + " milliseconds!");
763
+ }, timeoutInMilliseconds);
764
+ })
765
+ ]);
766
+ }
767
+ var Exporter = (function () {
768
+ function Exporter(connection, buildQuery, ft, write, end, attributes) {
769
+ this.connection = connection;
770
+ this.buildQuery = buildQuery;
771
+ this.write = write;
772
+ this.end = end;
773
+ this.attributes = attributes;
774
+ this.format = ft;
775
+ if (attributes) {
776
+ this.map = buildMap(attributes);
777
+ }
778
+ this.export = this.export.bind(this);
779
+ }
780
+ Exporter.prototype.export = function (ctx) {
781
+ return __awaiter(this, void 0, void 0, function () {
782
+ var idx, stmt, reader, er;
783
+ var _this = this;
784
+ return __generator(this, function (_a) {
785
+ switch (_a.label) {
786
+ case 0:
787
+ idx = 0;
788
+ return [4, this.buildQuery(ctx)];
789
+ case 1:
790
+ stmt = _a.sent();
791
+ reader = this.connection.query(stmt.query, stmt.params);
792
+ reader.on('error', function (err) { return er = err; });
793
+ if (this.map) {
794
+ reader.on('result', function (row) {
795
+ return __awaiter(_this, void 0, void 0, function () {
796
+ var obj, data;
797
+ return __generator(this, function (_a) {
798
+ ++idx;
799
+ this.connection.pause();
800
+ obj = mapOne(row, this.map);
801
+ data = this.format(obj);
802
+ this.write(data);
803
+ this.connection.resume();
804
+ return [2];
805
+ });
806
+ });
807
+ });
808
+ }
809
+ else {
810
+ reader.on('result', function (row) {
811
+ return __awaiter(_this, void 0, void 0, function () {
812
+ var data;
813
+ return __generator(this, function (_a) {
814
+ ++idx;
815
+ this.connection.pause();
816
+ data = this.format(row);
817
+ this.write(data);
818
+ this.connection.resume();
819
+ return [2];
820
+ });
821
+ });
822
+ });
823
+ }
824
+ return [2, new Promise(function (resolve, reject) {
825
+ reader.on('end', function () {
826
+ _this.end();
827
+ if (er) {
828
+ reject(er);
829
+ }
830
+ else {
831
+ _this.connection.end(function (err) {
832
+ if (err) {
833
+ reject(err);
834
+ }
835
+ else {
836
+ resolve(idx);
837
+ }
838
+ });
839
+ }
840
+ });
841
+ })];
842
+ }
843
+ });
844
+ });
845
+ };
846
+ return Exporter;
847
+ }());
848
+ exports.Exporter = Exporter;
849
+ var ExportService = (function () {
850
+ function ExportService(connection, queryBuilder, formatter, writer, attributes) {
851
+ this.connection = connection;
852
+ this.queryBuilder = queryBuilder;
853
+ this.formatter = formatter;
854
+ this.writer = writer;
855
+ this.attributes = attributes;
856
+ if (attributes) {
857
+ this.map = buildMap(attributes);
858
+ }
859
+ this.export = this.export.bind(this);
860
+ }
861
+ ExportService.prototype.export = function (ctx) {
862
+ return __awaiter(this, void 0, void 0, function () {
863
+ var idx, stmt, reader, er;
864
+ var _this = this;
865
+ return __generator(this, function (_a) {
866
+ switch (_a.label) {
867
+ case 0:
868
+ idx = 0;
869
+ return [4, this.queryBuilder.build(ctx)];
870
+ case 1:
871
+ stmt = _a.sent();
872
+ reader = this.connection.query(stmt.query, stmt.params);
873
+ reader.on('error', function (err) { return er = err; });
874
+ if (this.map) {
875
+ reader.on('result', function (row) {
876
+ return __awaiter(_this, void 0, void 0, function () {
877
+ var obj, data;
878
+ return __generator(this, function (_a) {
879
+ ++idx;
880
+ this.connection.pause();
881
+ obj = mapOne(row, this.map);
882
+ data = this.formatter.format(obj);
883
+ this.writer.write(data);
884
+ this.connection.resume();
885
+ return [2];
886
+ });
887
+ });
888
+ });
889
+ }
890
+ else {
891
+ reader.on('result', function (row) {
892
+ return __awaiter(_this, void 0, void 0, function () {
893
+ var data;
894
+ return __generator(this, function (_a) {
895
+ ++idx;
896
+ this.connection.pause();
897
+ data = this.formatter.format(row);
898
+ this.writer.write(data);
899
+ this.connection.resume();
900
+ return [2];
901
+ });
902
+ });
903
+ });
904
+ }
905
+ return [2, new Promise(function (resolve, reject) {
906
+ reader.on('end', function () {
907
+ if (_this.writer.end) {
908
+ _this.writer.end();
909
+ }
910
+ else if (_this.writer.flush) {
911
+ _this.writer.flush();
912
+ }
913
+ if (er) {
914
+ reject(er);
915
+ }
916
+ else {
917
+ _this.connection.end(function (err) {
918
+ if (err) {
919
+ reject(err);
920
+ }
921
+ else {
922
+ resolve(idx);
923
+ }
924
+ });
925
+ }
926
+ });
927
+ })];
928
+ }
929
+ });
930
+ });
931
+ };
932
+ return ExportService;
933
+ }());
934
+ exports.ExportService = ExportService;
935
+ function mapOne(results, m) {
936
+ var obj = results;
937
+ if (!m) {
938
+ return obj;
939
+ }
940
+ var mkeys = Object.keys(m);
941
+ if (mkeys.length === 0) {
942
+ return obj;
943
+ }
944
+ var obj2 = {};
945
+ var keys = Object.keys(obj);
946
+ for (var _i = 0, keys_3 = keys; _i < keys_3.length; _i++) {
947
+ var key = keys_3[_i];
948
+ var k0 = m[key];
949
+ if (!k0) {
950
+ k0 = key;
951
+ }
952
+ obj2[k0] = (obj)[key];
953
+ }
954
+ return obj2;
955
+ }
956
+ exports.mapOne = mapOne;
957
+ function buildMap(attrs) {
958
+ var mp = {};
959
+ var ks = Object.keys(attrs);
960
+ var isMap = false;
961
+ for (var _i = 0, ks_2 = ks; _i < ks_2.length; _i++) {
962
+ var k = ks_2[_i];
963
+ var attr = attrs[k];
964
+ attr.name = k;
965
+ var field = (attr.column ? attr.column : k);
966
+ var s = field.toLowerCase();
967
+ if (s !== k) {
968
+ mp[s] = k;
969
+ isMap = true;
970
+ }
971
+ }
972
+ if (isMap) {
973
+ return mp;
974
+ }
975
+ return undefined;
976
+ }
977
+ exports.buildMap = buildMap;
978
+ function select(table, attrs) {
979
+ var cols = [];
980
+ var ks = Object.keys(attrs);
981
+ for (var _i = 0, ks_3 = ks; _i < ks_3.length; _i++) {
982
+ var k = ks_3[_i];
983
+ var attr = attrs[k];
984
+ attr.name = k;
985
+ var field = (attr.column ? attr.column : k);
986
+ cols.push(field);
987
+ }
988
+ return "select " + cols.join(',') + " from " + table;
989
+ }
990
+ exports.select = select;