mysql2-core 0.3.8 → 0.3.9

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 CHANGED
@@ -106,7 +106,7 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
106
106
  args.push(v2);
107
107
  }
108
108
  else {
109
- const v2 = attr.false ? attr.false : `'0'`;
109
+ const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`;
110
110
  args.push(v2);
111
111
  }
112
112
  }
@@ -154,11 +154,11 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
154
154
  x = buildParam(i++);
155
155
  if (typeof v === "boolean") {
156
156
  if (v === true) {
157
- const v2 = attr.true ? "" + attr.true : `'1'`;
157
+ const v2 = attr.true ? attr.true : `'1'`;
158
158
  args.push(v2);
159
159
  }
160
160
  else {
161
- const v2 = attr.false ? "" + attr.false : `'0'`;
161
+ const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`;
162
162
  args.push(v2);
163
163
  }
164
164
  }
@@ -199,7 +199,7 @@ export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
199
199
  if (v == null) {
200
200
  v = attr.default;
201
201
  }
202
- if (v != null && v !== undefined && !attr.ignored && !attr.noinsert) {
202
+ if (v != null && !attr.ignored && !attr.noinsert) {
203
203
  const field = attr.column ? attr.column : k;
204
204
  cols.push(field);
205
205
  if (k === ver) {
@@ -222,7 +222,7 @@ export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
222
222
  args.push(v2);
223
223
  }
224
224
  else {
225
- const v2 = attr.false ? attr.false : `'0'`;
225
+ const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`;
226
226
  args.push(v2);
227
227
  }
228
228
  }
@@ -260,11 +260,11 @@ export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
260
260
  x = buildParam(i++);
261
261
  if (typeof v === "boolean") {
262
262
  if (v === true) {
263
- const v2 = attr.true ? "" + attr.true : `'1'`;
263
+ const v2 = attr.true ? attr.true : `'1'`;
264
264
  args.push(v2);
265
265
  }
266
266
  else {
267
- const v2 = attr.false ? "" + attr.false : `'0'`;
267
+ const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`;
268
268
  args.push(v2);
269
269
  }
270
270
  }
@@ -301,8 +301,7 @@ export function createPool(conf) {
301
301
  if (conf.max && conf.max > 0 && !conf.connectionLimit) {
302
302
  conf.connectionLimit = conf.max;
303
303
  }
304
- const pool = createPool2(Object.assign(Object.assign({}, conf), { rowsAsArray: true }));
305
- return pool;
304
+ return createPool2(Object.assign(Object.assign({}, conf), { rowsAsArray: true }));
306
305
  }
307
306
  export function getConnection(pool) {
308
307
  return new Promise((resolve, reject) => {
@@ -321,7 +320,7 @@ export function beginTransaction(connection, rollbackIfError) {
321
320
  if (!rollbackIfError) {
322
321
  return reject(err);
323
322
  }
324
- connection.rollback(() => {
323
+ return connection.rollback(() => {
325
324
  return reject(err);
326
325
  });
327
326
  }
@@ -336,11 +335,11 @@ export function commit(connection, rollbackIfError) {
336
335
  if (!rollbackIfError) {
337
336
  return reject(err);
338
337
  }
339
- connection.rollback(() => {
338
+ return connection.rollback(() => {
340
339
  return reject(err);
341
340
  });
342
341
  }
343
- resolve();
342
+ return resolve();
344
343
  });
345
344
  });
346
345
  }
@@ -350,7 +349,7 @@ export function rollback(connection) {
350
349
  if (err) {
351
350
  return reject(err);
352
351
  }
353
- resolve();
352
+ return resolve();
354
353
  });
355
354
  });
356
355
  }
@@ -371,18 +370,18 @@ export class PoolManager {
371
370
  }
372
371
  beginTransaction() {
373
372
  return new Promise((resolve, reject) => {
374
- this.pool.getConnection((er0, connection) => {
375
- if (er0) {
376
- return reject(er0);
373
+ this.pool.getConnection((err, connection) => {
374
+ if (err) {
375
+ return reject(err);
377
376
  }
378
- connection.beginTransaction((er1) => {
379
- if (er1) {
380
- connection.rollback(() => {
381
- return reject(er1);
377
+ connection.beginTransaction((beginError) => {
378
+ if (beginError) {
379
+ return connection.rollback(() => {
380
+ connection.release();
381
+ return reject(beginError);
382
382
  });
383
383
  }
384
- const tx = new PoolConnectionManager(connection);
385
- return resolve(tx);
384
+ return resolve(new PoolConnectionManager(connection));
386
385
  });
387
386
  });
388
387
  });
@@ -415,6 +414,7 @@ export class PoolManager {
415
414
  export class PoolConnectionManager {
416
415
  constructor(connection) {
417
416
  this.connection = connection;
417
+ this.released = false;
418
418
  this.driver = "mysql";
419
419
  this.param = this.param.bind(this);
420
420
  this.execute = this.execute.bind(this);
@@ -429,24 +429,36 @@ export class PoolConnectionManager {
429
429
  }
430
430
  commit() {
431
431
  return new Promise((resolve, reject) => {
432
- this.connection.commit((er3) => {
433
- if (er3) {
434
- return reject(er3);
432
+ this.connection.commit((err) => {
433
+ if (err) {
434
+ return this.connection.rollback(() => {
435
+ this.release();
436
+ return reject(err);
437
+ });
435
438
  }
439
+ this.release();
436
440
  return resolve();
437
441
  });
438
442
  });
439
443
  }
440
444
  rollback() {
441
445
  return new Promise((resolve, reject) => {
442
- this.connection.rollback((er3) => {
443
- if (er3) {
444
- return reject(er3);
446
+ this.connection.rollback((err) => {
447
+ this.release();
448
+ if (err) {
449
+ return reject(err);
445
450
  }
446
451
  return resolve();
447
452
  });
448
453
  });
449
454
  }
455
+ release() {
456
+ if (this.released) {
457
+ return;
458
+ }
459
+ this.released = true;
460
+ this.connection.release();
461
+ }
450
462
  execute(sql, args, ctx) {
451
463
  const p = ctx ? ctx : this.connection;
452
464
  return execute(p, sql, args);
@@ -475,322 +487,152 @@ export class PoolConnectionManager {
475
487
  export function executeBatch(pool, statements, firstSuccess) {
476
488
  return __awaiter(this, void 0, void 0, function* () {
477
489
  if (!statements || statements.length === 0) {
478
- return Promise.resolve(0);
490
+ return 0;
479
491
  }
480
- else if (statements.length === 1) {
492
+ if (statements.length === 1) {
481
493
  return execute(pool, statements[0].query, statements[0].params);
482
494
  }
483
- return new Promise((resolve, reject) => {
484
- pool.getConnection((er0, connection) => {
485
- if (er0) {
486
- return reject(er0);
487
- }
488
- return executeBatchConnectionTx(connection, statements, firstSuccess);
489
- });
490
- });
495
+ const connection = yield getConnection(pool);
496
+ try {
497
+ return yield executeBatchConnectionTx(connection, statements, firstSuccess);
498
+ }
499
+ finally {
500
+ connection.release();
501
+ }
491
502
  });
492
503
  }
493
504
  export function executeBatchConnectionTx(connection, statements, firstSuccess) {
494
505
  return __awaiter(this, void 0, void 0, function* () {
495
506
  if (!statements || statements.length === 0) {
496
- return Promise.resolve(0);
507
+ return 0;
497
508
  }
498
- else if (statements.length === 1) {
509
+ if (statements.length === 1) {
499
510
  return execute(connection, statements[0].query, statements[0].params);
500
511
  }
501
- if (resource.multipleStatements) {
502
- if (firstSuccess) {
503
- return new Promise((resolve, reject) => {
504
- connection.beginTransaction((er1) => {
505
- if (er1) {
506
- connection.rollback(() => {
507
- return reject(er1);
508
- });
509
- }
510
- else {
511
- connection.execute(statements[0].query, toArray(statements[0].params), (er2a, results0) => {
512
- if (er2a) {
513
- connection.rollback(() => {
514
- return reject(er2a);
515
- });
516
- }
517
- else {
518
- if (results0 && results0.affectedRows === 0) {
519
- connection.commit((er3) => {
520
- if (er3) {
521
- connection.rollback(() => {
522
- return reject(er3);
523
- });
524
- }
525
- });
526
- return 0;
527
- }
528
- else {
529
- const queries = [];
530
- const params = [];
531
- const l = statements.length;
532
- for (let j = 1; j < l; j++) {
533
- const item = statements[j];
534
- if (item.query.endsWith(";")) {
535
- queries.push(item.query);
536
- }
537
- else {
538
- queries.push(item.query + ";");
539
- }
540
- if (item.params && item.params.length > 0) {
541
- for (const p of item.params) {
542
- params.push(p);
543
- }
544
- }
545
- }
546
- connection.query(queries.join(""), toArray(params), (er2, results) => {
547
- if (er2) {
548
- connection.rollback(() => {
549
- return reject(er2);
550
- });
551
- }
552
- else {
553
- connection.commit((er3) => {
554
- if (er3) {
555
- connection.rollback(() => {
556
- return reject(er3);
557
- });
558
- }
559
- });
560
- let c = 0;
561
- c += results0.affectedRows + results.affectedRows;
562
- return resolve(c);
563
- }
564
- });
565
- }
566
- }
567
- });
568
- }
569
- });
570
- });
571
- }
572
- else {
573
- return new Promise((resolve, reject) => {
574
- connection.beginTransaction((er1) => {
575
- if (er1) {
576
- connection.rollback(() => {
577
- return reject(er1);
578
- });
579
- }
580
- else {
581
- const queries = [];
582
- const params = [];
583
- statements.forEach((item) => {
584
- if (item.query.endsWith(";")) {
585
- queries.push(item.query);
586
- }
587
- else {
588
- queries.push(item.query + ";");
589
- }
590
- if (item.params && item.params.length > 0) {
591
- for (const p of item.params) {
592
- params.push(p);
593
- }
594
- }
595
- });
596
- connection.query(queries.join(""), toArray(params), (er2, results) => {
597
- if (er2) {
598
- connection.rollback(() => {
599
- buildError(er2);
600
- return reject(er2);
601
- });
602
- }
603
- else {
604
- connection.commit((er3) => {
605
- if (er3) {
606
- connection.rollback(() => {
607
- return reject(er3);
608
- });
609
- }
610
- });
611
- return resolve(results.affectedRows);
612
- }
613
- });
614
- }
615
- });
616
- });
617
- }
618
- }
619
- else {
620
- if (firstSuccess) {
621
- try {
622
- yield beginTransaction(connection, true);
623
- let count = yield execute(connection, statements[0].query, toArray(statements[0].params));
624
- if (count === 0) {
625
- commit(connection, true);
512
+ yield beginTransaction(connection, true);
513
+ try {
514
+ let count = 0;
515
+ if (resource.multipleStatements) {
516
+ if (firstSuccess) {
517
+ const firstCount = yield execute(connection, statements[0].query, statements[0].params);
518
+ count = firstCount;
519
+ if (firstCount === 0) {
520
+ yield commit(connection, true);
626
521
  return 0;
627
522
  }
628
- const l = statements.length;
629
- for (let j = 1; j < l; j++) {
630
- const item = statements[j];
631
- const c = yield execute(connection, item.query, toArray(item.params));
632
- count = count + c;
633
- }
634
- commit(connection, true);
635
- return count;
523
+ const remaining = statements.slice(1);
524
+ count += yield executeMultipleStatements(connection, remaining);
636
525
  }
637
- catch (err) {
638
- rollback(connection);
639
- throw err;
526
+ else {
527
+ count = yield executeMultipleStatements(connection, statements);
640
528
  }
641
529
  }
642
530
  else {
643
- try {
644
- yield beginTransaction(connection, true);
645
- let count = 0;
646
- const l = statements.length;
647
- for (let j = 0; j < l; j++) {
648
- const item = statements[j];
649
- const c = yield execute(connection, item.query, toArray(item.params));
650
- count = count + c;
531
+ const start = firstSuccess ? 1 : 0;
532
+ if (firstSuccess) {
533
+ const firstCount = yield execute(connection, statements[0].query, statements[0].params);
534
+ count = firstCount;
535
+ if (firstCount === 0) {
536
+ yield commit(connection, true);
537
+ return 0;
651
538
  }
652
- commit(connection, true);
653
- return count;
654
539
  }
655
- catch (err) {
656
- rollback(connection);
657
- throw err;
540
+ for (let i = start; i < statements.length; i++) {
541
+ const item = statements[i];
542
+ count += yield execute(connection, item.query, item.params);
658
543
  }
659
544
  }
545
+ yield commit(connection, true);
546
+ return count;
547
+ }
548
+ catch (err) {
549
+ try {
550
+ yield rollback(connection);
551
+ }
552
+ catch (_a) {
553
+ }
554
+ throw buildError(err);
660
555
  }
661
556
  });
662
557
  }
663
558
  export function executeBatchConnection(connection, statements, firstSuccess) {
664
559
  return __awaiter(this, void 0, void 0, function* () {
665
560
  if (!statements || statements.length === 0) {
666
- return Promise.resolve(0);
561
+ return 0;
667
562
  }
668
- else if (statements.length === 1) {
563
+ if (statements.length === 1) {
669
564
  return execute(connection, statements[0].query, statements[0].params);
670
565
  }
671
566
  if (resource.multipleStatements) {
672
567
  if (firstSuccess) {
673
- return new Promise((resolve, reject) => {
674
- connection.execute(statements[0].query, toArray(statements[0].params), (er2a, results0) => {
675
- if (er2a) {
676
- return reject(er2a);
677
- }
678
- else {
679
- if (results0 && results0.affectedRows === 0) {
680
- return 0;
681
- }
682
- else {
683
- const queries = [];
684
- const params = [];
685
- const l = statements.length;
686
- for (let j = 1; j < l; j++) {
687
- const item = statements[j];
688
- if (item.query.endsWith(";")) {
689
- queries.push(item.query);
690
- }
691
- else {
692
- queries.push(item.query + ";");
693
- }
694
- if (item.params && item.params.length > 0) {
695
- for (const p of item.params) {
696
- params.push(p);
697
- }
698
- }
699
- }
700
- connection.query(queries.join(""), toArray(params), (er2, results) => {
701
- if (er2) {
702
- return reject(er2);
703
- }
704
- else {
705
- let c = 0;
706
- c += results0.affectedRows + results.affectedRows;
707
- return resolve(c);
708
- }
709
- });
710
- }
711
- }
712
- });
713
- });
568
+ const firstCount = yield execute(connection, statements[0].query, statements[0].params);
569
+ if (firstCount === 0) {
570
+ return 0;
571
+ }
572
+ return firstCount + (yield executeMultipleStatements(connection, statements.slice(1)));
714
573
  }
715
- else {
716
- return new Promise((resolve, reject) => {
717
- const queries = [];
718
- const params = [];
719
- statements.forEach((item) => {
720
- if (item.query.endsWith(";")) {
721
- queries.push(item.query);
722
- }
723
- else {
724
- queries.push(item.query + ";");
725
- }
726
- if (item.params && item.params.length > 0) {
727
- for (const p of item.params) {
728
- params.push(p);
729
- }
730
- }
731
- });
732
- connection.query(queries.join(""), toArray(params), (er2, results) => {
733
- if (er2) {
734
- connection.rollback(() => {
735
- buildError(er2);
736
- return reject(er2);
737
- });
738
- }
739
- else {
740
- connection.commit((er3) => {
741
- if (er3) {
742
- connection.rollback(() => {
743
- return reject(er3);
744
- });
745
- }
746
- });
747
- return resolve(results.affectedRows);
748
- }
749
- });
750
- });
574
+ return executeMultipleStatements(connection, statements);
575
+ }
576
+ let count = 0;
577
+ const start = firstSuccess ? 1 : 0;
578
+ if (firstSuccess) {
579
+ const firstCount = yield execute(connection, statements[0].query, statements[0].params);
580
+ count = firstCount;
581
+ if (firstCount === 0) {
582
+ return 0;
751
583
  }
752
584
  }
753
- else {
754
- if (firstSuccess) {
755
- try {
756
- let count = yield execute(connection, statements[0].query, toArray(statements[0].params));
757
- if (count === 0) {
758
- return 0;
759
- }
760
- const l = statements.length;
761
- try {
762
- for (let j = 1; j < l; j++) {
763
- const item = statements[j];
764
- const c = yield execute(connection, item.query, toArray(item.params));
765
- count = count + c;
766
- }
767
- return count;
768
- }
769
- catch (er1) {
770
- throw er1;
771
- }
772
- }
773
- catch (er0) {
774
- throw er0;
775
- }
585
+ for (let i = start; i < statements.length; i++) {
586
+ const item = statements[i];
587
+ count += yield execute(connection, item.query, item.params);
588
+ }
589
+ return count;
590
+ });
591
+ }
592
+ function executeMultipleStatements(connection, statements) {
593
+ return __awaiter(this, void 0, void 0, function* () {
594
+ if (!statements || statements.length === 0) {
595
+ return 0;
596
+ }
597
+ const queries = [];
598
+ const params = [];
599
+ for (const item of statements) {
600
+ queries.push(ensureSemicolon(item.query));
601
+ if (item.params && item.params.length > 0) {
602
+ params.push(...item.params);
776
603
  }
777
- else {
778
- let count = 0;
779
- const l = statements.length;
780
- try {
781
- for (let j = 0; j < l; j++) {
782
- const item = statements[j];
783
- const c = yield execute(connection, item.query, toArray(item.params));
784
- count = count + c;
785
- }
786
- return count;
787
- }
788
- catch (err) {
789
- throw err;
604
+ }
605
+ return new Promise((resolve, reject) => {
606
+ connection.query(queries.join(""), toArray(params), (err, results) => {
607
+ if (err) {
608
+ buildError(err);
609
+ return reject(err);
790
610
  }
611
+ return resolve(getAffectedRows(results));
612
+ });
613
+ });
614
+ });
615
+ }
616
+ function ensureSemicolon(sql) {
617
+ return sql.endsWith(";") ? sql : `${sql};`;
618
+ }
619
+ function getAffectedRows(results) {
620
+ if (!results) {
621
+ return 0;
622
+ }
623
+ if (Array.isArray(results)) {
624
+ let count = 0;
625
+ for (const result of results) {
626
+ if (result && typeof result.affectedRows === "number") {
627
+ count += result.affectedRows;
791
628
  }
792
629
  }
793
- });
630
+ return count;
631
+ }
632
+ if (typeof results.affectedRows === "number") {
633
+ return results.affectedRows;
634
+ }
635
+ return 0;
794
636
  }
795
637
  function buildError(err) {
796
638
  if (err.errno === 1062 && err.code === "ER_DUP_ENTRY") {
@@ -802,14 +644,12 @@ export function execute(pool, sql, args) {
802
644
  return __awaiter(this, void 0, void 0, function* () {
803
645
  const p = toArray(args);
804
646
  return new Promise((resolve, reject) => {
805
- return pool.execute(sql, p, (err, res) => {
647
+ pool.execute(sql, p, (err, res) => {
806
648
  if (err) {
807
649
  buildError(err);
808
650
  return reject(err);
809
651
  }
810
- else {
811
- return resolve(res.affectedRows);
812
- }
652
+ return resolve(res.affectedRows);
813
653
  });
814
654
  });
815
655
  });
@@ -818,26 +658,20 @@ export function query(pool, sql, args, m, bools) {
818
658
  return __awaiter(this, void 0, void 0, function* () {
819
659
  const p = toArray(args);
820
660
  return new Promise((resolve, reject) => {
821
- return pool.query(sql, p, (err, results, fields) => {
661
+ pool.query(sql, p, (err, results, fields) => {
822
662
  if (err) {
823
663
  return reject(err);
824
664
  }
825
- else {
826
- if (results.length > 0) {
827
- const arrayResult = results.map((item) => {
828
- if (Array.isArray(item)) {
829
- return formatData(fields, item);
830
- }
831
- else {
832
- return item;
833
- }
834
- });
835
- return resolve(handleResults(arrayResult, m, bools));
836
- }
837
- else {
838
- resolve([]);
839
- }
665
+ if (results.length === 0) {
666
+ return resolve([]);
840
667
  }
668
+ const arrayResult = results.map((item) => {
669
+ if (Array.isArray(item)) {
670
+ return formatData(fields, item);
671
+ }
672
+ return item;
673
+ });
674
+ return resolve(handleResults(arrayResult, m, bools));
841
675
  });
842
676
  });
843
677
  });
@@ -852,14 +686,12 @@ export function executeScalar(pool, sql, args) {
852
686
  if (!r) {
853
687
  return null;
854
688
  }
855
- else {
856
- const keys = Object.keys(r);
857
- return r[keys[0]];
858
- }
689
+ const keys = Object.keys(r);
690
+ return r[keys[0]];
859
691
  });
860
692
  }
861
693
  export function count(pool, sql, args) {
862
- return executeScalar(pool, sql, args);
694
+ return executeScalar(pool, sql, args).then((res) => (res !== null ? res : 0));
863
695
  }
864
696
  export function save(pool, obj, table, attrs, ver, buildParam) {
865
697
  const s = buildToSave(obj, table, attrs, ver, buildParam);
@@ -887,30 +719,25 @@ export function toArray(arr) {
887
719
  return [];
888
720
  }
889
721
  const p = [];
890
- const l = arr.length;
891
- for (let i = 0; i < l; i++) {
892
- if (arr[i] === undefined || arr[i] == null) {
722
+ for (const value of arr) {
723
+ if (value === undefined || value === null) {
893
724
  p.push(null);
725
+ continue;
894
726
  }
895
- else {
896
- if (typeof arr[i] === "object") {
897
- if (arr[i] instanceof Date) {
898
- p.push(arr[i]);
899
- }
900
- else {
901
- if (resource.string) {
902
- const s = JSON.stringify(arr[i]);
903
- p.push(s);
904
- }
905
- else {
906
- p.push(arr[i]);
907
- }
908
- }
727
+ if (typeof value === "object") {
728
+ if (value instanceof Date) {
729
+ p.push(value);
730
+ continue;
731
+ }
732
+ if (resource.string) {
733
+ p.push(JSON.stringify(value));
909
734
  }
910
735
  else {
911
- p.push(arr[i]);
736
+ p.push(value);
912
737
  }
738
+ continue;
913
739
  }
740
+ p.push(value);
914
741
  }
915
742
  return p;
916
743
  }
@@ -920,18 +747,12 @@ export function handleResults(r, m, bools) {
920
747
  if (bools && bools.length > 0) {
921
748
  return handleBool(res, bools);
922
749
  }
923
- else {
924
- return res;
925
- }
750
+ return res;
926
751
  }
927
- else {
928
- if (bools && bools.length > 0) {
929
- return handleBool(r, bools);
930
- }
931
- else {
932
- return r;
933
- }
752
+ if (bools && bools.length > 0) {
753
+ return handleBool(r, bools);
934
754
  }
755
+ return r;
935
756
  }
936
757
  export function handleBool(objs, bools) {
937
758
  if (!bools || bools.length === 0 || !objs) {
@@ -940,17 +761,19 @@ export function handleBool(objs, bools) {
940
761
  for (const obj of objs) {
941
762
  const o = obj;
942
763
  for (const field of bools) {
943
- if (field.name) {
944
- const v = o[field.name];
945
- if (typeof v !== "boolean" && v != null && v !== undefined) {
946
- const b = field.true;
947
- if (b == null || b === undefined) {
948
- o[field.name] = "1" == v || "T" == v || "Y" == v || "true" == v || "on" == v;
949
- }
950
- else {
951
- o[field.name] = v == b ? true : false;
952
- }
953
- }
764
+ if (!field.name) {
765
+ continue;
766
+ }
767
+ const v = o[field.name];
768
+ if (typeof v === "boolean" || v == null) {
769
+ continue;
770
+ }
771
+ const b = field.true;
772
+ if (b == null) {
773
+ o[field.name] = "1" == v || "T" == v || "Y" == v || "true" == v || "on" == v;
774
+ }
775
+ else {
776
+ o[field.name] = v == b;
954
777
  }
955
778
  }
956
779
  }
@@ -960,7 +783,7 @@ export function formatData(nameColumn, data, m) {
960
783
  const result = {};
961
784
  nameColumn.forEach((item, index) => {
962
785
  let key = item.name;
963
- if (m) {
786
+ if (m && m[item.name]) {
964
787
  key = m[item.name];
965
788
  }
966
789
  result[key] = data[index];
@@ -995,9 +818,7 @@ export function mapArray(results, m) {
995
818
  return results;
996
819
  }
997
820
  const objs = [];
998
- const length = results.length;
999
- for (let i = 0; i < length; i++) {
1000
- const obj = results[i];
821
+ for (const obj of results) {
1001
822
  const obj2 = {};
1002
823
  const keys = Object.keys(obj);
1003
824
  for (const key of keys) {
@@ -1058,7 +879,7 @@ export function getMapField(name, mp) {
1058
879
  export function isEmpty(s) {
1059
880
  return !(s && s.length > 0);
1060
881
  }
1061
- export class StringService {
882
+ export class StringAdapter {
1062
883
  constructor(pool, table, column) {
1063
884
  this.pool = pool;
1064
885
  this.table = table;
@@ -1096,19 +917,14 @@ export function version(attrs) {
1096
917
  return undefined;
1097
918
  }
1098
919
  export class MySQLWriter {
1099
- constructor(pool, table, attributes, oneIfSuccess, toDB, buildParam) {
920
+ constructor(pool, table, attributes, oneIfSuccess, map, buildParam) {
921
+ this.pool = pool;
1100
922
  this.table = table;
1101
923
  this.attributes = attributes;
1102
924
  this.oneIfSuccess = oneIfSuccess;
925
+ this.map = map;
1103
926
  this.write = this.write.bind(this);
1104
- if (typeof pool === "function") {
1105
- this.execute = pool;
1106
- }
1107
- else {
1108
- this.pool = pool;
1109
- }
1110
- this.param = buildParam;
1111
- this.map = toDB;
927
+ this.param = buildParam ? buildParam : param;
1112
928
  const x = version(attributes);
1113
929
  if (x) {
1114
930
  this.version = x.name;
@@ -1124,21 +940,11 @@ export class MySQLWriter {
1124
940
  }
1125
941
  const stmt = buildToSave(obj2, this.table, this.attributes, this.version, this.param);
1126
942
  if (stmt.query) {
1127
- if (this.execute) {
1128
- if (this.oneIfSuccess) {
1129
- return this.execute(stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0));
1130
- }
1131
- else {
1132
- return this.execute(stmt.query, stmt.params);
1133
- }
943
+ if (this.oneIfSuccess) {
944
+ return execute(this.pool, stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0));
1134
945
  }
1135
946
  else {
1136
- if (this.oneIfSuccess) {
1137
- return execute(this.pool, stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0));
1138
- }
1139
- else {
1140
- return execute(this.pool, stmt.query, stmt.params);
1141
- }
947
+ return execute(this.pool, stmt.query, stmt.params);
1142
948
  }
1143
949
  }
1144
950
  else {
@@ -1147,28 +953,20 @@ export class MySQLWriter {
1147
953
  }
1148
954
  }
1149
955
  export class MySQLStreamWriter {
1150
- constructor(pool, table, attributes, size, toDB, buildParam) {
956
+ constructor(pool, table, attributes, size = 1000, map, buildParam) {
957
+ this.pool = pool;
1151
958
  this.table = table;
1152
959
  this.attributes = attributes;
960
+ this.size = size;
961
+ this.map = map;
1153
962
  this.list = [];
1154
- this.size = 0;
1155
963
  this.write = this.write.bind(this);
1156
964
  this.flush = this.flush.bind(this);
1157
- if (typeof pool === "function") {
1158
- this.executeBatch = pool;
1159
- }
1160
- else {
1161
- this.pool = pool;
1162
- }
1163
- this.param = buildParam;
1164
- this.map = toDB;
965
+ this.param = buildParam ? buildParam : param;
1165
966
  const x = version(attributes);
1166
967
  if (x) {
1167
968
  this.version = x.name;
1168
969
  }
1169
- if (size) {
1170
- this.size = size;
1171
- }
1172
970
  }
1173
971
  write(obj) {
1174
972
  if (!obj) {
@@ -1197,18 +995,10 @@ export class MySQLStreamWriter {
1197
995
  const total = this.list.length;
1198
996
  const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.version, this.param);
1199
997
  if (stmt.length > 0) {
1200
- if (this.executeBatch) {
1201
- return this.executeBatch(stmt).then((r) => {
1202
- this.list = [];
1203
- return total;
1204
- });
1205
- }
1206
- else {
1207
- return executeBatch(this.pool, stmt).then((r) => {
1208
- this.list = [];
1209
- return total;
1210
- });
1211
- }
998
+ return executeBatch(this.pool, stmt).then((r) => {
999
+ this.list = [];
1000
+ return total;
1001
+ });
1212
1002
  }
1213
1003
  else {
1214
1004
  return Promise.resolve(0);
@@ -1217,19 +1007,14 @@ export class MySQLStreamWriter {
1217
1007
  }
1218
1008
  }
1219
1009
  export class MySQLBatchWriter {
1220
- constructor(pool, table, attributes, oneIfSuccess, toDB, buildParam) {
1010
+ constructor(pool, table, attributes, oneIfSuccess, map, buildParam) {
1011
+ this.pool = pool;
1221
1012
  this.table = table;
1222
1013
  this.attributes = attributes;
1223
1014
  this.oneIfSuccess = oneIfSuccess;
1015
+ this.map = map;
1224
1016
  this.write = this.write.bind(this);
1225
- if (typeof pool === "function") {
1226
- this.execute = pool;
1227
- }
1228
- else {
1229
- this.pool = pool;
1230
- }
1231
- this.param = buildParam;
1232
- this.map = toDB;
1017
+ this.param = buildParam ? buildParam : param;
1233
1018
  const x = version(attributes);
1234
1019
  if (x) {
1235
1020
  this.version = x.name;
@@ -1249,21 +1034,11 @@ export class MySQLBatchWriter {
1249
1034
  }
1250
1035
  const stmts = buildToSaveBatch(list, this.table, this.attributes, this.version, this.param);
1251
1036
  if (stmts.length > 0) {
1252
- if (this.execute) {
1253
- if (this.oneIfSuccess) {
1254
- return this.execute(stmts).then((ct) => stmts.length);
1255
- }
1256
- else {
1257
- return this.execute(stmts);
1258
- }
1037
+ if (this.oneIfSuccess) {
1038
+ return executeBatch(this.pool, stmts).then((ct) => stmts.length);
1259
1039
  }
1260
1040
  else {
1261
- if (this.oneIfSuccess) {
1262
- return executeBatch(this.pool, stmts).then((ct) => stmts.length);
1263
- }
1264
- else {
1265
- return executeBatch(this.pool, stmts);
1266
- }
1041
+ return executeBatch(this.pool, stmts);
1267
1042
  }
1268
1043
  }
1269
1044
  else {
@@ -1313,13 +1088,16 @@ export class MySQLChecker {
1313
1088
  }
1314
1089
  }
1315
1090
  export class Exporter {
1316
- constructor(connection, buildQuery, ft, write, end, attributes) {
1091
+ constructor(connection, filename, buildQuery, format, write, end, attributes, logInfo, progressSize = 10000) {
1317
1092
  this.connection = connection;
1093
+ this.filename = filename;
1318
1094
  this.buildQuery = buildQuery;
1095
+ this.format = format;
1319
1096
  this.write = write;
1320
1097
  this.end = end;
1321
1098
  this.attributes = attributes;
1322
- this.format = ft;
1099
+ this.logInfo = logInfo;
1100
+ this.progressSize = progressSize;
1323
1101
  if (attributes) {
1324
1102
  this.map = buildMap(attributes);
1325
1103
  }
@@ -1327,28 +1105,43 @@ export class Exporter {
1327
1105
  }
1328
1106
  export(ctx) {
1329
1107
  return __awaiter(this, void 0, void 0, function* () {
1330
- let idx = 0;
1331
1108
  const stmt = yield this.buildQuery(ctx);
1332
1109
  const reader = this.connection.query(stmt.query, stmt.params);
1333
1110
  let er;
1111
+ let i = 0;
1112
+ let k = 0;
1334
1113
  reader.on("error", (err) => (er = err));
1335
1114
  if (this.map) {
1336
1115
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1337
- ++idx;
1116
+ ++i;
1117
+ k++;
1338
1118
  this.connection.pause();
1339
1119
  const obj = mapOne(row, this.map);
1340
1120
  const data = this.format(obj);
1341
1121
  this.write(data);
1342
1122
  this.connection.resume();
1123
+ if (k >= this.progressSize) {
1124
+ if (this.logInfo) {
1125
+ this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1126
+ }
1127
+ k = 0;
1128
+ }
1343
1129
  }));
1344
1130
  }
1345
1131
  else {
1346
1132
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1347
- ++idx;
1133
+ ++i;
1134
+ k++;
1348
1135
  this.connection.pause();
1349
1136
  const data = this.format(row);
1350
1137
  this.write(data);
1351
1138
  this.connection.resume();
1139
+ if (k >= this.progressSize) {
1140
+ if (this.logInfo) {
1141
+ this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1142
+ }
1143
+ k = 0;
1144
+ }
1352
1145
  }));
1353
1146
  }
1354
1147
  return new Promise((resolve, reject) => {
@@ -1363,7 +1156,7 @@ export class Exporter {
1363
1156
  reject(err);
1364
1157
  }
1365
1158
  else {
1366
- resolve(idx);
1159
+ resolve(i);
1367
1160
  }
1368
1161
  });
1369
1162
  }
@@ -1373,12 +1166,15 @@ export class Exporter {
1373
1166
  }
1374
1167
  }
1375
1168
  export class ExportService {
1376
- constructor(connection, queryBuilder, formatter, writer, attributes) {
1169
+ constructor(connection, filename, queryBuilder, formatter, writer, attributes, logInfo, progressSize = 10000) {
1377
1170
  this.connection = connection;
1171
+ this.filename = filename;
1378
1172
  this.queryBuilder = queryBuilder;
1379
1173
  this.formatter = formatter;
1380
1174
  this.writer = writer;
1381
1175
  this.attributes = attributes;
1176
+ this.logInfo = logInfo;
1177
+ this.progressSize = progressSize;
1382
1178
  if (attributes) {
1383
1179
  this.map = buildMap(attributes);
1384
1180
  }
@@ -1386,28 +1182,43 @@ export class ExportService {
1386
1182
  }
1387
1183
  export(ctx) {
1388
1184
  return __awaiter(this, void 0, void 0, function* () {
1389
- let idx = 0;
1390
1185
  const stmt = yield this.queryBuilder.build(ctx);
1391
1186
  const reader = this.connection.query(stmt.query, stmt.params);
1392
1187
  let er;
1393
1188
  reader.on("error", (err) => (er = err));
1189
+ let i = 0;
1190
+ let k = 0;
1394
1191
  if (this.map) {
1395
1192
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1396
- ++idx;
1193
+ ++i;
1194
+ k++;
1397
1195
  this.connection.pause();
1398
1196
  const obj = mapOne(row, this.map);
1399
1197
  const data = this.formatter.format(obj);
1400
1198
  this.writer.write(data);
1401
1199
  this.connection.resume();
1200
+ if (k >= this.progressSize) {
1201
+ if (this.logInfo) {
1202
+ this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1203
+ }
1204
+ k = 0;
1205
+ }
1402
1206
  }));
1403
1207
  }
1404
1208
  else {
1405
1209
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1406
- ++idx;
1210
+ ++i;
1211
+ k++;
1407
1212
  this.connection.pause();
1408
1213
  const data = this.formatter.format(row);
1409
1214
  this.writer.write(data);
1410
1215
  this.connection.resume();
1216
+ if (k >= this.progressSize) {
1217
+ if (this.logInfo) {
1218
+ this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1219
+ }
1220
+ k = 0;
1221
+ }
1411
1222
  }));
1412
1223
  }
1413
1224
  return new Promise((resolve, reject) => {
@@ -1427,7 +1238,7 @@ export class ExportService {
1427
1238
  reject(err);
1428
1239
  }
1429
1240
  else {
1430
- resolve(idx);
1241
+ resolve(i);
1431
1242
  }
1432
1243
  });
1433
1244
  }