mysql2-core 0.3.9 → 0.3.10

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/README.md CHANGED
@@ -54,7 +54,7 @@ interface Executor {
54
54
  driver: string
55
55
  param(i: number): string
56
56
  execute(sql: string, args?: any[], ctx?: any): Promise<number>
57
- executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>
57
+ executeBatch(statements: Statement[], firstAffected?: boolean, ctx?: any): Promise<number>
58
58
  query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
59
59
  queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T | null>
60
60
  executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T | null>
@@ -283,15 +283,15 @@ For multiple statements, the pool-level `executeBatch()` obtains a connection an
283
283
 
284
284
  The connection is released after completion.
285
285
 
286
- ## `firstSuccess`
286
+ ## `firstAffected`
287
287
 
288
- `executeBatch()` supports an optional `firstSuccess` flag:
288
+ `executeBatch()` supports an optional `firstAffected` flag:
289
289
 
290
290
  ```ts
291
291
  await db.executeBatch(statements, true)
292
292
  ```
293
293
 
294
- When `firstSuccess` is `true`, the first statement determines whether the remaining statements are executed.
294
+ When `firstAffected` is `true`, the first statement determines whether the remaining statements are executed.
295
295
 
296
296
  If the first statement affects at least one row:
297
297
 
package/lib/index.js CHANGED
@@ -59,7 +59,7 @@ export function metadata(attrs) {
59
59
  }
60
60
  return m;
61
61
  }
62
- export function buildToSave(obj, table, attrs, ver, buildParam, i) {
62
+ export function buildToSave(obj, table, attrs, buildParam, i) {
63
63
  if (!i) {
64
64
  i = 1;
65
65
  }
@@ -86,7 +86,7 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
86
86
  if (v != null && !attr.ignored && !attr.noinsert) {
87
87
  const field = attr.column ? attr.column : k;
88
88
  cols.push(field);
89
- if (k === ver) {
89
+ if (attr.version) {
90
90
  isVersion = true;
91
91
  values.push(`${1}`);
92
92
  }
@@ -102,11 +102,11 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
102
102
  values.push(p);
103
103
  if (typeof v === "boolean") {
104
104
  if (v === true) {
105
- const v2 = attr.true ? attr.true : `'1'`;
105
+ const v2 = attr.true !== undefined ? attr.true : `'1'`;
106
106
  args.push(v2);
107
107
  }
108
108
  else {
109
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`;
109
+ const v2 = attr.false !== undefined ? attr.false : `'0'`;
110
110
  args.push(v2);
111
111
  }
112
112
  }
@@ -117,12 +117,6 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
117
117
  }
118
118
  }
119
119
  }
120
- if (!isVersion && ver && ver.length > 0) {
121
- const attr = attrs[ver];
122
- const field = attr.column ? attr.column : ver;
123
- cols.push(field);
124
- values.push(`${1}`);
125
- }
126
120
  if (pks.length === 0) {
127
121
  if (cols.length === 0) {
128
122
  return { query: "", params: args };
@@ -154,11 +148,11 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
154
148
  x = buildParam(i++);
155
149
  if (typeof v === "boolean") {
156
150
  if (v === true) {
157
- const v2 = attr.true ? attr.true : `'1'`;
151
+ const v2 = attr.true !== undefined ? attr.true : `'1'`;
158
152
  args.push(v2);
159
153
  }
160
154
  else {
161
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`;
155
+ const v2 = attr.false !== undefined ? attr.false : `'0'`;
162
156
  args.push(v2);
163
157
  }
164
158
  }
@@ -180,7 +174,7 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
180
174
  }
181
175
  }
182
176
  }
183
- export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
177
+ export function buildToSaveBatch(objs, table, attrs, buildParam) {
184
178
  if (!buildParam) {
185
179
  buildParam = param;
186
180
  }
@@ -202,7 +196,7 @@ export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
202
196
  if (v != null && !attr.ignored && !attr.noinsert) {
203
197
  const field = attr.column ? attr.column : k;
204
198
  cols.push(field);
205
- if (k === ver) {
199
+ if (attr.version) {
206
200
  isVersion = true;
207
201
  values.push(`${1}`);
208
202
  }
@@ -218,11 +212,11 @@ export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
218
212
  values.push(p);
219
213
  if (typeof v === "boolean") {
220
214
  if (v === true) {
221
- const v2 = attr.true ? attr.true : `'1'`;
215
+ const v2 = attr.true !== undefined ? attr.true : `'1'`;
222
216
  args.push(v2);
223
217
  }
224
218
  else {
225
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`;
219
+ const v2 = attr.false !== undefined ? attr.false : `'0'`;
226
220
  args.push(v2);
227
221
  }
228
222
  }
@@ -233,18 +227,12 @@ export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
233
227
  }
234
228
  }
235
229
  }
236
- if (!isVersion && ver && ver.length > 0) {
237
- const attr = attrs[ver];
238
- const field = attr.column ? attr.column : ver;
239
- cols.push(field);
240
- values.push(`${1}`);
241
- }
242
230
  const colSet = [];
243
231
  for (const k of ks) {
244
232
  const v = o[k];
245
233
  if (v !== undefined) {
246
234
  const attr = attrs[k];
247
- if (attr && !attr.key && !attr.ignored && k !== ver && !attr.noupdate) {
235
+ if (attr && !attr.key && !attr.ignored && !attr.noupdate) {
248
236
  const field = attr.column ? attr.column : k;
249
237
  let x;
250
238
  if (v === null) {
@@ -260,11 +248,11 @@ export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
260
248
  x = buildParam(i++);
261
249
  if (typeof v === "boolean") {
262
250
  if (v === true) {
263
- const v2 = attr.true ? attr.true : `'1'`;
251
+ const v2 = attr.true !== undefined ? attr.true : `'1'`;
264
252
  args.push(v2);
265
253
  }
266
254
  else {
267
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`;
255
+ const v2 = attr.false !== undefined ? attr.false : `'0'`;
268
256
  args.push(v2);
269
257
  }
270
258
  }
@@ -386,29 +374,23 @@ export class PoolManager {
386
374
  });
387
375
  });
388
376
  }
389
- execute(sql, args, ctx) {
390
- const p = ctx ? ctx : this.pool;
391
- return execute(p, sql, args);
377
+ execute(sql, args) {
378
+ return execute(this.pool, sql, args);
392
379
  }
393
- executeBatch(statements, firstSuccess, ctx) {
394
- const p = ctx ? ctx : this.pool;
395
- return executeBatch(p, statements, firstSuccess);
380
+ executeBatch(statements, firstAffected) {
381
+ return executeBatch(this.pool, statements, firstAffected);
396
382
  }
397
- query(sql, args, m, bools, ctx) {
398
- const p = ctx ? ctx : this.pool;
399
- return query(p, sql, args, m, bools);
383
+ query(sql, args, m, bools) {
384
+ return query(this.pool, sql, args, m, bools);
400
385
  }
401
- queryOne(sql, args, m, bools, ctx) {
402
- const p = ctx ? ctx : this.pool;
403
- return queryOne(p, sql, args, m, bools);
386
+ queryOne(sql, args, m, bools) {
387
+ return queryOne(this.pool, sql, args, m, bools);
404
388
  }
405
- executeScalar(sql, args, ctx) {
406
- const p = ctx ? ctx : this.pool;
407
- return executeScalar(p, sql, args);
389
+ executeScalar(sql, args) {
390
+ return executeScalar(this.pool, sql, args);
408
391
  }
409
- count(sql, args, ctx) {
410
- const p = ctx ? ctx : this.pool;
411
- return count(p, sql, args);
392
+ count(sql, args) {
393
+ return count(this.pool, sql, args);
412
394
  }
413
395
  }
414
396
  export class PoolConnectionManager {
@@ -459,32 +441,26 @@ export class PoolConnectionManager {
459
441
  this.released = true;
460
442
  this.connection.release();
461
443
  }
462
- execute(sql, args, ctx) {
463
- const p = ctx ? ctx : this.connection;
464
- return execute(p, sql, args);
444
+ execute(sql, args) {
445
+ return execute(this.connection, sql, args);
465
446
  }
466
- executeBatch(statements, firstSuccess, ctx) {
467
- const p = ctx ? ctx : this.connection;
468
- return executeBatchConnection(p, statements, firstSuccess);
447
+ executeBatch(statements, firstAffected) {
448
+ return executeBatchConnection(this.connection, statements, firstAffected);
469
449
  }
470
450
  query(sql, args, m, bools, ctx) {
471
- const p = ctx ? ctx : this.connection;
472
- return query(p, sql, args, m, bools);
451
+ return query(this.connection, sql, args, m, bools);
473
452
  }
474
- queryOne(sql, args, m, bools, ctx) {
475
- const p = ctx ? ctx : this.connection;
476
- return queryOne(p, sql, args, m, bools);
453
+ queryOne(sql, args, m, bools) {
454
+ return queryOne(this.connection, sql, args, m, bools);
477
455
  }
478
- executeScalar(sql, args, ctx) {
479
- const p = ctx ? ctx : this.connection;
480
- return executeScalar(p, sql, args);
456
+ executeScalar(sql, args) {
457
+ return executeScalar(this.connection, sql, args);
481
458
  }
482
- count(sql, args, ctx) {
483
- const p = ctx ? ctx : this.connection;
484
- return count(p, sql, args);
459
+ count(sql, args) {
460
+ return count(this.connection, sql, args);
485
461
  }
486
462
  }
487
- export function executeBatch(pool, statements, firstSuccess) {
463
+ export function executeBatch(pool, statements, firstAffected) {
488
464
  return __awaiter(this, void 0, void 0, function* () {
489
465
  if (!statements || statements.length === 0) {
490
466
  return 0;
@@ -494,14 +470,14 @@ export function executeBatch(pool, statements, firstSuccess) {
494
470
  }
495
471
  const connection = yield getConnection(pool);
496
472
  try {
497
- return yield executeBatchConnectionTx(connection, statements, firstSuccess);
473
+ return yield executeBatchConnectionTx(connection, statements, firstAffected);
498
474
  }
499
475
  finally {
500
476
  connection.release();
501
477
  }
502
478
  });
503
479
  }
504
- export function executeBatchConnectionTx(connection, statements, firstSuccess) {
480
+ export function executeBatchConnectionTx(connection, statements, firstAffected) {
505
481
  return __awaiter(this, void 0, void 0, function* () {
506
482
  if (!statements || statements.length === 0) {
507
483
  return 0;
@@ -513,7 +489,7 @@ export function executeBatchConnectionTx(connection, statements, firstSuccess) {
513
489
  try {
514
490
  let count = 0;
515
491
  if (resource.multipleStatements) {
516
- if (firstSuccess) {
492
+ if (firstAffected) {
517
493
  const firstCount = yield execute(connection, statements[0].query, statements[0].params);
518
494
  count = firstCount;
519
495
  if (firstCount === 0) {
@@ -528,8 +504,8 @@ export function executeBatchConnectionTx(connection, statements, firstSuccess) {
528
504
  }
529
505
  }
530
506
  else {
531
- const start = firstSuccess ? 1 : 0;
532
- if (firstSuccess) {
507
+ const start = firstAffected ? 1 : 0;
508
+ if (firstAffected) {
533
509
  const firstCount = yield execute(connection, statements[0].query, statements[0].params);
534
510
  count = firstCount;
535
511
  if (firstCount === 0) {
@@ -555,7 +531,7 @@ export function executeBatchConnectionTx(connection, statements, firstSuccess) {
555
531
  }
556
532
  });
557
533
  }
558
- export function executeBatchConnection(connection, statements, firstSuccess) {
534
+ export function executeBatchConnection(connection, statements, firstAffected) {
559
535
  return __awaiter(this, void 0, void 0, function* () {
560
536
  if (!statements || statements.length === 0) {
561
537
  return 0;
@@ -564,7 +540,7 @@ export function executeBatchConnection(connection, statements, firstSuccess) {
564
540
  return execute(connection, statements[0].query, statements[0].params);
565
541
  }
566
542
  if (resource.multipleStatements) {
567
- if (firstSuccess) {
543
+ if (firstAffected) {
568
544
  const firstCount = yield execute(connection, statements[0].query, statements[0].params);
569
545
  if (firstCount === 0) {
570
546
  return 0;
@@ -574,8 +550,8 @@ export function executeBatchConnection(connection, statements, firstSuccess) {
574
550
  return executeMultipleStatements(connection, statements);
575
551
  }
576
552
  let count = 0;
577
- const start = firstSuccess ? 1 : 0;
578
- if (firstSuccess) {
553
+ const start = firstAffected ? 1 : 0;
554
+ if (firstAffected) {
579
555
  const firstCount = yield execute(connection, statements[0].query, statements[0].params);
580
556
  count = firstCount;
581
557
  if (firstCount === 0) {
@@ -693,8 +669,8 @@ export function executeScalar(pool, sql, args) {
693
669
  export function count(pool, sql, args) {
694
670
  return executeScalar(pool, sql, args).then((res) => (res !== null ? res : 0));
695
671
  }
696
- export function save(pool, obj, table, attrs, ver, buildParam) {
697
- const s = buildToSave(obj, table, attrs, ver, buildParam);
672
+ export function save(pool, obj, table, attrs, buildParam) {
673
+ const s = buildToSave(obj, table, attrs, buildParam);
698
674
  if (!s.query) {
699
675
  return Promise.resolve(-1);
700
676
  }
@@ -705,8 +681,8 @@ export function save(pool, obj, table, attrs, ver, buildParam) {
705
681
  return execute(pool, s.query, s.params);
706
682
  }
707
683
  }
708
- export function saveBatch(pool, objs, table, attrs, ver, buildParam) {
709
- const s = buildToSaveBatch(objs, table, attrs, ver, buildParam);
684
+ export function saveBatch(pool, objs, table, attrs, buildParam) {
685
+ const s = buildToSaveBatch(objs, table, attrs, buildParam);
710
686
  if (typeof pool === "function") {
711
687
  return pool(s);
712
688
  }
@@ -905,17 +881,6 @@ export class StringAdapter {
905
881
  return execute(this.pool, s, values);
906
882
  }
907
883
  }
908
- export function version(attrs) {
909
- const ks = Object.keys(attrs);
910
- for (const k of ks) {
911
- const attr = attrs[k];
912
- if (attr.version) {
913
- attr.name = k;
914
- return attr;
915
- }
916
- }
917
- return undefined;
918
- }
919
884
  export class MySQLWriter {
920
885
  constructor(pool, table, attributes, oneIfSuccess, map, buildParam) {
921
886
  this.pool = pool;
@@ -925,10 +890,6 @@ export class MySQLWriter {
925
890
  this.map = map;
926
891
  this.write = this.write.bind(this);
927
892
  this.param = buildParam ? buildParam : param;
928
- const x = version(attributes);
929
- if (x) {
930
- this.version = x.name;
931
- }
932
893
  }
933
894
  write(obj) {
934
895
  if (!obj) {
@@ -938,7 +899,7 @@ export class MySQLWriter {
938
899
  if (this.map) {
939
900
  obj2 = this.map(obj);
940
901
  }
941
- const stmt = buildToSave(obj2, this.table, this.attributes, this.version, this.param);
902
+ const stmt = buildToSave(obj2, this.table, this.attributes, this.param);
942
903
  if (stmt.query) {
943
904
  if (this.oneIfSuccess) {
944
905
  return execute(this.pool, stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0));
@@ -963,10 +924,6 @@ export class MySQLStreamWriter {
963
924
  this.write = this.write.bind(this);
964
925
  this.flush = this.flush.bind(this);
965
926
  this.param = buildParam ? buildParam : param;
966
- const x = version(attributes);
967
- if (x) {
968
- this.version = x.name;
969
- }
970
927
  }
971
928
  write(obj) {
972
929
  if (!obj) {
@@ -992,15 +949,15 @@ export class MySQLStreamWriter {
992
949
  return Promise.resolve(0);
993
950
  }
994
951
  else {
995
- const total = this.list.length;
996
- const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.version, this.param);
952
+ const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.param);
997
953
  if (stmt.length > 0) {
998
954
  return executeBatch(this.pool, stmt).then((r) => {
999
955
  this.list = [];
1000
- return total;
956
+ return stmt.length;
1001
957
  });
1002
958
  }
1003
959
  else {
960
+ this.list = [];
1004
961
  return Promise.resolve(0);
1005
962
  }
1006
963
  }
@@ -1015,10 +972,6 @@ export class MySQLBatchWriter {
1015
972
  this.map = map;
1016
973
  this.write = this.write.bind(this);
1017
974
  this.param = buildParam ? buildParam : param;
1018
- const x = version(attributes);
1019
- if (x) {
1020
- this.version = x.name;
1021
- }
1022
975
  }
1023
976
  write(objs) {
1024
977
  if (!objs || objs.length === 0) {
@@ -1032,7 +985,7 @@ export class MySQLBatchWriter {
1032
985
  list.push(obj2);
1033
986
  }
1034
987
  }
1035
- const stmts = buildToSaveBatch(list, this.table, this.attributes, this.version, this.param);
988
+ const stmts = buildToSaveBatch(list, this.table, this.attributes, this.param);
1036
989
  if (stmts.length > 0) {
1037
990
  if (this.oneIfSuccess) {
1038
991
  return executeBatch(this.pool, stmts).then((ct) => stmts.length);
@@ -1109,38 +1062,38 @@ export class Exporter {
1109
1062
  const reader = this.connection.query(stmt.query, stmt.params);
1110
1063
  let er;
1111
1064
  let i = 0;
1112
- let k = 0;
1065
+ let j = 0;
1113
1066
  reader.on("error", (err) => (er = err));
1114
1067
  if (this.map) {
1115
1068
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1116
1069
  ++i;
1117
- k++;
1070
+ j++;
1118
1071
  this.connection.pause();
1119
1072
  const obj = mapOne(row, this.map);
1120
1073
  const data = this.format(obj);
1121
1074
  this.write(data);
1122
1075
  this.connection.resume();
1123
- if (k >= this.progressSize) {
1076
+ if (j >= this.progressSize) {
1124
1077
  if (this.logInfo) {
1125
1078
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1126
1079
  }
1127
- k = 0;
1080
+ j = 0;
1128
1081
  }
1129
1082
  }));
1130
1083
  }
1131
1084
  else {
1132
1085
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1133
1086
  ++i;
1134
- k++;
1087
+ j++;
1135
1088
  this.connection.pause();
1136
1089
  const data = this.format(row);
1137
1090
  this.write(data);
1138
1091
  this.connection.resume();
1139
- if (k >= this.progressSize) {
1092
+ if (j >= this.progressSize) {
1140
1093
  if (this.logInfo) {
1141
1094
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1142
1095
  }
1143
- k = 0;
1096
+ j = 0;
1144
1097
  }
1145
1098
  }));
1146
1099
  }
@@ -1187,37 +1140,37 @@ export class ExportService {
1187
1140
  let er;
1188
1141
  reader.on("error", (err) => (er = err));
1189
1142
  let i = 0;
1190
- let k = 0;
1143
+ let j = 0;
1191
1144
  if (this.map) {
1192
1145
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1193
1146
  ++i;
1194
- k++;
1147
+ j++;
1195
1148
  this.connection.pause();
1196
1149
  const obj = mapOne(row, this.map);
1197
1150
  const data = this.formatter.format(obj);
1198
1151
  this.writer.write(data);
1199
1152
  this.connection.resume();
1200
- if (k >= this.progressSize) {
1153
+ if (j >= this.progressSize) {
1201
1154
  if (this.logInfo) {
1202
1155
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1203
1156
  }
1204
- k = 0;
1157
+ j = 0;
1205
1158
  }
1206
1159
  }));
1207
1160
  }
1208
1161
  else {
1209
1162
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1210
1163
  ++i;
1211
- k++;
1164
+ j++;
1212
1165
  this.connection.pause();
1213
1166
  const data = this.formatter.format(row);
1214
1167
  this.writer.write(data);
1215
1168
  this.connection.resume();
1216
- if (k >= this.progressSize) {
1169
+ if (j >= this.progressSize) {
1217
1170
  if (this.logInfo) {
1218
1171
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1219
1172
  }
1220
- k = 0;
1173
+ j = 0;
1221
1174
  }
1222
1175
  }));
1223
1176
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mysql2-core",
3
- "version": "0.3.9",
3
+ "version": "0.3.10",
4
4
  "description": "mysql2-core",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/build.ts CHANGED
@@ -59,7 +59,7 @@ export function metadata(attrs: Attributes): Metadata {
59
59
  }
60
60
  return m
61
61
  }
62
- export function buildToSave<T>(obj: T, table: string, attrs: Attributes, ver?: string, buildParam?: (i: number) => string, i?: number): Statement {
62
+ export function buildToSave<T>(obj: T, table: string, attrs: Attributes, buildParam?: (i: number) => string, i?: number): Statement {
63
63
  if (!i) {
64
64
  i = 1
65
65
  }
@@ -86,7 +86,7 @@ export function buildToSave<T>(obj: T, table: string, attrs: Attributes, ver?: s
86
86
  if (v != null && !attr.ignored && !attr.noinsert) {
87
87
  const field = attr.column ? attr.column : k
88
88
  cols.push(field)
89
- if (k === ver) {
89
+ if (attr.version) {
90
90
  isVersion = true
91
91
  values.push(`${1}`)
92
92
  } else {
@@ -99,10 +99,10 @@ export function buildToSave<T>(obj: T, table: string, attrs: Attributes, ver?: s
99
99
  values.push(p)
100
100
  if (typeof v === "boolean") {
101
101
  if (v === true) {
102
- const v2 = attr.true ? attr.true : `'1'`
102
+ const v2 = attr.true !== undefined ? attr.true : `'1'`
103
103
  args.push(v2)
104
104
  } else {
105
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`
105
+ const v2 = attr.false !== undefined ? attr.false : `'0'`
106
106
  args.push(v2)
107
107
  }
108
108
  } else {
@@ -112,12 +112,6 @@ export function buildToSave<T>(obj: T, table: string, attrs: Attributes, ver?: s
112
112
  }
113
113
  }
114
114
  }
115
- if (!isVersion && ver && ver.length > 0) {
116
- const attr = attrs[ver]
117
- const field = attr.column ? attr.column : ver
118
- cols.push(field)
119
- values.push(`${1}`)
120
- }
121
115
  if (pks.length === 0) {
122
116
  if (cols.length === 0) {
123
117
  return { query: "", params: args }
@@ -144,10 +138,10 @@ export function buildToSave<T>(obj: T, table: string, attrs: Attributes, ver?: s
144
138
  x = buildParam(i++)
145
139
  if (typeof v === "boolean") {
146
140
  if (v === true) {
147
- const v2 = attr.true ? attr.true : `'1'`
141
+ const v2 = attr.true !== undefined ? attr.true : `'1'`
148
142
  args.push(v2)
149
143
  } else {
150
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`
144
+ const v2 = attr.false !== undefined ? attr.false : `'0'`
151
145
  args.push(v2)
152
146
  }
153
147
  } else {
@@ -167,7 +161,7 @@ export function buildToSave<T>(obj: T, table: string, attrs: Attributes, ver?: s
167
161
  }
168
162
  }
169
163
  }
170
- export function buildToSaveBatch<T>(objs: T[], table: string, attrs: Attributes, ver?: string, buildParam?: (i: number) => string): Statement[] {
164
+ export function buildToSaveBatch<T>(objs: T[], table: string, attrs: Attributes, buildParam?: (i: number) => string): Statement[] {
171
165
  if (!buildParam) {
172
166
  buildParam = param
173
167
  }
@@ -189,7 +183,7 @@ export function buildToSaveBatch<T>(objs: T[], table: string, attrs: Attributes,
189
183
  if (v != null && !attr.ignored && !attr.noinsert) {
190
184
  const field = attr.column ? attr.column : k
191
185
  cols.push(field)
192
- if (k === ver) {
186
+ if (attr.version) {
193
187
  isVersion = true
194
188
  values.push(`${1}`)
195
189
  } else {
@@ -202,10 +196,10 @@ export function buildToSaveBatch<T>(objs: T[], table: string, attrs: Attributes,
202
196
  values.push(p)
203
197
  if (typeof v === "boolean") {
204
198
  if (v === true) {
205
- const v2 = attr.true ? attr.true : `'1'`
199
+ const v2 = attr.true !== undefined ? attr.true : `'1'`
206
200
  args.push(v2)
207
201
  } else {
208
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`
202
+ const v2 = attr.false !== undefined ? attr.false : `'0'`
209
203
  args.push(v2)
210
204
  }
211
205
  } else {
@@ -215,18 +209,12 @@ export function buildToSaveBatch<T>(objs: T[], table: string, attrs: Attributes,
215
209
  }
216
210
  }
217
211
  }
218
- if (!isVersion && ver && ver.length > 0) {
219
- const attr = attrs[ver]
220
- const field = attr.column ? attr.column : ver
221
- cols.push(field)
222
- values.push(`${1}`)
223
- }
224
212
  const colSet: string[] = []
225
213
  for (const k of ks) {
226
214
  const v = o[k]
227
215
  if (v !== undefined) {
228
216
  const attr = attrs[k]
229
- if (attr && !attr.key && !attr.ignored && k !== ver && !attr.noupdate) {
217
+ if (attr && !attr.key && !attr.ignored && !attr.noupdate) {
230
218
  const field = attr.column ? attr.column : k
231
219
  let x: string
232
220
  if (v === null) {
@@ -239,10 +227,10 @@ export function buildToSaveBatch<T>(objs: T[], table: string, attrs: Attributes,
239
227
  x = buildParam(i++)
240
228
  if (typeof v === "boolean") {
241
229
  if (v === true) {
242
- const v2 = attr.true ? attr.true : `'1'`
230
+ const v2 = attr.true !== undefined ? attr.true : `'1'`
243
231
  args.push(v2)
244
232
  } else {
245
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`
233
+ const v2 = attr.false !== undefined ? attr.false : `'0'`
246
234
  args.push(v2)
247
235
  }
248
236
  } else {
package/src/index.ts CHANGED
@@ -113,29 +113,23 @@ export class PoolManager implements DB {
113
113
  })
114
114
  })
115
115
  }
116
- execute(sql: string, args?: any[], ctx?: any): Promise<number> {
117
- const p = ctx ? ctx : this.pool
118
- return execute(p, sql, args)
116
+ execute(sql: string, args?: any[]): Promise<number> {
117
+ return execute(this.pool, sql, args)
119
118
  }
120
- executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
121
- const p = ctx ? ctx : this.pool
122
- return executeBatch(p, statements, firstSuccess)
119
+ executeBatch(statements: Statement[], firstAffected?: boolean): Promise<number> {
120
+ return executeBatch(this.pool, statements, firstAffected)
123
121
  }
124
- query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]> {
125
- const p = ctx ? ctx : this.pool
126
- return query(p, sql, args, m, bools)
122
+ query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T[]> {
123
+ return query(this.pool, sql, args, m, bools)
127
124
  }
128
- queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T | null> {
129
- const p = ctx ? ctx : this.pool
130
- return queryOne(p, sql, args, m, bools)
125
+ queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T | null> {
126
+ return queryOne(this.pool, sql, args, m, bools)
131
127
  }
132
- executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T | null> {
133
- const p = ctx ? ctx : this.pool
134
- return executeScalar<T>(p, sql, args)
128
+ executeScalar<T>(sql: string, args?: any[]): Promise<T | null> {
129
+ return executeScalar<T>(this.pool, sql, args)
135
130
  }
136
- count(sql: string, args?: any[], ctx?: any): Promise<number> {
137
- const p = ctx ? ctx : this.pool
138
- return count(p, sql, args)
131
+ count(sql: string, args?: any[]): Promise<number> {
132
+ return count(this.pool, sql, args)
139
133
  }
140
134
  }
141
135
  // tslint:disable-next-line:max-classes-per-file
@@ -186,32 +180,26 @@ export class PoolConnectionManager implements Transaction {
186
180
  this.released = true
187
181
  this.connection.release()
188
182
  }
189
- execute(sql: string, args?: any[], ctx?: any): Promise<number> {
190
- const p = ctx ? ctx : this.connection
191
- return execute(p, sql, args)
183
+ execute(sql: string, args?: any[]): Promise<number> {
184
+ return execute(this.connection, sql, args)
192
185
  }
193
- executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
194
- const p = ctx ? ctx : this.connection
195
- return executeBatchConnection(p, statements, firstSuccess)
186
+ executeBatch(statements: Statement[], firstAffected?: boolean): Promise<number> {
187
+ return executeBatchConnection(this.connection, statements, firstAffected)
196
188
  }
197
189
  query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]> {
198
- const p = ctx ? ctx : this.connection
199
- return query(p, sql, args, m, bools)
190
+ return query(this.connection, sql, args, m, bools)
200
191
  }
201
- queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T | null> {
202
- const p = ctx ? ctx : this.connection
203
- return queryOne(p, sql, args, m, bools)
192
+ queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T | null> {
193
+ return queryOne(this.connection, sql, args, m, bools)
204
194
  }
205
- executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T | null> {
206
- const p = ctx ? ctx : this.connection
207
- return executeScalar<T>(p, sql, args)
195
+ executeScalar<T>(sql: string, args?: any[]): Promise<T | null> {
196
+ return executeScalar<T>(this.connection, sql, args)
208
197
  }
209
- count(sql: string, args?: any[], ctx?: any): Promise<number> {
210
- const p = ctx ? ctx : this.connection
211
- return count(p, sql, args)
198
+ count(sql: string, args?: any[]): Promise<number> {
199
+ return count(this.connection, sql, args)
212
200
  }
213
201
  }
214
- export async function executeBatch(pool: Pool, statements: Statement[], firstSuccess?: boolean): Promise<number> {
202
+ export async function executeBatch(pool: Pool, statements: Statement[], firstAffected?: boolean): Promise<number> {
215
203
  if (!statements || statements.length === 0) {
216
204
  return 0
217
205
  }
@@ -220,12 +208,12 @@ export async function executeBatch(pool: Pool, statements: Statement[], firstSuc
220
208
  }
221
209
  const connection = await getConnection(pool)
222
210
  try {
223
- return await executeBatchConnectionTx(connection, statements, firstSuccess)
211
+ return await executeBatchConnectionTx(connection, statements, firstAffected)
224
212
  } finally {
225
213
  connection.release()
226
214
  }
227
215
  }
228
- export async function executeBatchConnectionTx(connection: PoolConnection, statements: Statement[], firstSuccess?: boolean): Promise<number> {
216
+ export async function executeBatchConnectionTx(connection: PoolConnection, statements: Statement[], firstAffected?: boolean): Promise<number> {
229
217
  if (!statements || statements.length === 0) {
230
218
  return 0
231
219
  }
@@ -237,7 +225,7 @@ export async function executeBatchConnectionTx(connection: PoolConnection, state
237
225
  let count = 0
238
226
 
239
227
  if (resource.multipleStatements) {
240
- if (firstSuccess) {
228
+ if (firstAffected) {
241
229
  const firstCount = await execute(connection, statements[0].query, statements[0].params)
242
230
 
243
231
  count = firstCount
@@ -252,8 +240,8 @@ export async function executeBatchConnectionTx(connection: PoolConnection, state
252
240
  count = await executeMultipleStatements(connection, statements)
253
241
  }
254
242
  } else {
255
- const start = firstSuccess ? 1 : 0
256
- if (firstSuccess) {
243
+ const start = firstAffected ? 1 : 0
244
+ if (firstAffected) {
257
245
  const firstCount = await execute(connection, statements[0].query, statements[0].params)
258
246
  count = firstCount
259
247
  if (firstCount === 0) {
@@ -276,12 +264,11 @@ export async function executeBatchConnectionTx(connection: PoolConnection, state
276
264
  } catch {
277
265
  // Preserve the original error.
278
266
  }
279
-
280
267
  throw buildError(err)
281
268
  }
282
269
  }
283
270
 
284
- export async function executeBatchConnection(connection: PoolConnection, statements: Statement[], firstSuccess?: boolean): Promise<number> {
271
+ export async function executeBatchConnection(connection: PoolConnection, statements: Statement[], firstAffected?: boolean): Promise<number> {
285
272
  if (!statements || statements.length === 0) {
286
273
  return 0
287
274
  }
@@ -291,7 +278,7 @@ export async function executeBatchConnection(connection: PoolConnection, stateme
291
278
  }
292
279
 
293
280
  if (resource.multipleStatements) {
294
- if (firstSuccess) {
281
+ if (firstAffected) {
295
282
  const firstCount = await execute(connection, statements[0].query, statements[0].params)
296
283
 
297
284
  if (firstCount === 0) {
@@ -305,9 +292,9 @@ export async function executeBatchConnection(connection: PoolConnection, stateme
305
292
  }
306
293
 
307
294
  let count = 0
308
- const start = firstSuccess ? 1 : 0
295
+ const start = firstAffected ? 1 : 0
309
296
 
310
- if (firstSuccess) {
297
+ if (firstAffected) {
311
298
  const firstCount = await execute(connection, statements[0].query, statements[0].params)
312
299
 
313
300
  count = firstCount
@@ -437,8 +424,8 @@ export function count(pool: Pool | PoolConnection, sql: string, args?: any[]): P
437
424
  return executeScalar<number>(pool, sql, args).then((res) => (res !== null ? res : 0))
438
425
  }
439
426
 
440
- export function save<T>(pool: Pool | PoolConnection | ((sql: string, args?: any[]) => Promise<number>), obj: T, table: string, attrs: Attributes, ver?: string, buildParam?: (i: number) => string): Promise<number> {
441
- const s = buildToSave(obj, table, attrs, ver, buildParam)
427
+ export function save<T>(pool: Pool | PoolConnection | ((sql: string, args?: any[]) => Promise<number>), obj: T, table: string, attrs: Attributes, buildParam?: (i: number) => string): Promise<number> {
428
+ const s = buildToSave(obj, table, attrs, buildParam)
442
429
  if (!s.query) {
443
430
  return Promise.resolve(-1)
444
431
  }
@@ -448,8 +435,8 @@ export function save<T>(pool: Pool | PoolConnection | ((sql: string, args?: any[
448
435
  return execute(pool, s.query, s.params)
449
436
  }
450
437
  }
451
- export function saveBatch<T>(pool: Pool | ((statements: Statement[]) => Promise<number>), objs: T[], table: string, attrs: Attributes, ver?: string, buildParam?: (i: number) => string): Promise<number> {
452
- const s = buildToSaveBatch(objs, table, attrs, ver, buildParam)
438
+ export function saveBatch<T>(pool: Pool | ((statements: Statement[]) => Promise<number>), objs: T[], table: string, attrs: Attributes, buildParam?: (i: number) => string): Promise<number> {
439
+ const s = buildToSaveBatch(objs, table, attrs, buildParam)
453
440
  if (typeof pool === "function") {
454
441
  return pool(s)
455
442
  } else {
@@ -647,20 +634,8 @@ export class StringAdapter {
647
634
  }
648
635
  }
649
636
 
650
- export function version(attrs: Attributes): Attribute | undefined {
651
- const ks = Object.keys(attrs)
652
- for (const k of ks) {
653
- const attr = attrs[k]
654
- if (attr.version) {
655
- attr.name = k
656
- return attr
657
- }
658
- }
659
- return undefined
660
- }
661
637
  // tslint:disable-next-line:max-classes-per-file
662
638
  export class MySQLWriter<T> {
663
- protected version?: string
664
639
  protected param?: (i: number) => string
665
640
  constructor(
666
641
  protected pool: Pool | PoolConnection,
@@ -672,10 +647,6 @@ export class MySQLWriter<T> {
672
647
  ) {
673
648
  this.write = this.write.bind(this)
674
649
  this.param = buildParam ? buildParam : param
675
- const x = version(attributes)
676
- if (x) {
677
- this.version = x.name
678
- }
679
650
  }
680
651
  write(obj: T): Promise<number> {
681
652
  if (!obj) {
@@ -685,7 +656,7 @@ export class MySQLWriter<T> {
685
656
  if (this.map) {
686
657
  obj2 = this.map(obj)
687
658
  }
688
- const stmt = buildToSave(obj2, this.table, this.attributes, this.version, this.param)
659
+ const stmt = buildToSave(obj2, this.table, this.attributes, this.param)
689
660
  if (stmt.query) {
690
661
  if (this.oneIfSuccess) {
691
662
  return execute(this.pool, stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0))
@@ -700,7 +671,6 @@ export class MySQLWriter<T> {
700
671
  // tslint:disable-next-line:max-classes-per-file
701
672
  export class MySQLStreamWriter<T> {
702
673
  protected list: T[] = []
703
- protected version?: string
704
674
  protected param?: (i: number) => string
705
675
  constructor(
706
676
  protected pool: Pool,
@@ -713,10 +683,6 @@ export class MySQLStreamWriter<T> {
713
683
  this.write = this.write.bind(this)
714
684
  this.flush = this.flush.bind(this)
715
685
  this.param = buildParam ? buildParam : param
716
- const x = version(attributes)
717
- if (x) {
718
- this.version = x.name
719
- }
720
686
  }
721
687
  write(obj: T): Promise<number> {
722
688
  if (!obj) {
@@ -739,14 +705,14 @@ export class MySQLStreamWriter<T> {
739
705
  if (!this.list || this.list.length === 0) {
740
706
  return Promise.resolve(0)
741
707
  } else {
742
- const total = this.list.length
743
- const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.version, this.param)
708
+ const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.param)
744
709
  if (stmt.length > 0) {
745
710
  return executeBatch(this.pool as any, stmt).then((r) => {
746
711
  this.list = []
747
- return total
712
+ return stmt.length
748
713
  })
749
714
  } else {
715
+ this.list = []
750
716
  return Promise.resolve(0)
751
717
  }
752
718
  }
@@ -754,7 +720,6 @@ export class MySQLStreamWriter<T> {
754
720
  }
755
721
  // tslint:disable-next-line:max-classes-per-file
756
722
  export class MySQLBatchWriter<T> {
757
- protected version?: string
758
723
  protected param?: (i: number) => string
759
724
  constructor(
760
725
  protected pool: Pool,
@@ -766,10 +731,6 @@ export class MySQLBatchWriter<T> {
766
731
  ) {
767
732
  this.write = this.write.bind(this)
768
733
  this.param = buildParam ? buildParam : param
769
- const x = version(attributes)
770
- if (x) {
771
- this.version = x.name
772
- }
773
734
  }
774
735
  write(objs: T[]): Promise<number> {
775
736
  if (!objs || objs.length === 0) {
@@ -783,7 +744,7 @@ export class MySQLBatchWriter<T> {
783
744
  list.push(obj2)
784
745
  }
785
746
  }
786
- const stmts = buildToSaveBatch(list, this.table, this.attributes, this.version, this.param)
747
+ const stmts = buildToSaveBatch(list, this.table, this.attributes, this.param)
787
748
  if (stmts.length > 0) {
788
749
  if (this.oneIfSuccess) {
789
750
  return executeBatch(this.pool, stmts).then((ct) => stmts.length)
@@ -903,38 +864,38 @@ export class Exporter<T> {
903
864
  const reader = this.connection.query(stmt.query, stmt.params)
904
865
  let er: any
905
866
  let i = 0
906
- let k = 0
867
+ let j = 0
907
868
  reader.on("error", (err) => (er = err))
908
869
  // (D2) WRITE ROW-BY-ROW
909
870
  if (this.map) {
910
871
  reader.on("result", async (row: any) => {
911
872
  ++i
912
- k++
873
+ j++
913
874
  this.connection.pause()
914
875
  const obj = mapOne<T>(row, this.map)
915
876
  const data = this.format(obj)
916
877
  this.write(data)
917
878
  this.connection.resume()
918
- if (k >= this.progressSize) {
879
+ if (j >= this.progressSize) {
919
880
  if (this.logInfo) {
920
881
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`)
921
882
  }
922
- k = 0
883
+ j = 0
923
884
  }
924
885
  })
925
886
  } else {
926
887
  reader.on("result", async (row: any) => {
927
888
  ++i
928
- k++
889
+ j++
929
890
  this.connection.pause()
930
891
  const data = this.format(row as T)
931
892
  this.write(data)
932
893
  this.connection.resume()
933
- if (k >= this.progressSize) {
894
+ if (j >= this.progressSize) {
934
895
  if (this.logInfo) {
935
896
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`)
936
897
  }
937
- k = 0
898
+ j = 0
938
899
  }
939
900
  })
940
901
  }
@@ -981,37 +942,37 @@ export class ExportService<T> {
981
942
  let er: any
982
943
  reader.on("error", (err) => (er = err))
983
944
  let i = 0
984
- let k = 0
945
+ let j = 0
985
946
  // (D2) WRITE ROW-BY-ROW
986
947
  if (this.map) {
987
948
  reader.on("result", async (row: any) => {
988
949
  ++i
989
- k++
950
+ j++
990
951
  this.connection.pause()
991
952
  const obj = mapOne<T>(row, this.map)
992
953
  const data = this.formatter.format(obj)
993
954
  this.writer.write(data)
994
955
  this.connection.resume()
995
- if (k >= this.progressSize) {
956
+ if (j >= this.progressSize) {
996
957
  if (this.logInfo) {
997
958
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`)
998
959
  }
999
- k = 0
960
+ j = 0
1000
961
  }
1001
962
  })
1002
963
  } else {
1003
964
  reader.on("result", async (row: any) => {
1004
965
  ++i
1005
- k++
966
+ j++
1006
967
  this.connection.pause()
1007
968
  const data = this.formatter.format(row as T)
1008
969
  this.writer.write(data)
1009
970
  this.connection.resume()
1010
- if (k >= this.progressSize) {
971
+ if (j >= this.progressSize) {
1011
972
  if (this.logInfo) {
1012
973
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`)
1013
974
  }
1014
- k = 0
975
+ j = 0
1015
976
  }
1016
977
  })
1017
978
  }
package/src/metadata.ts CHANGED
@@ -9,12 +9,12 @@ export interface Statement {
9
9
  export interface Executor {
10
10
  driver: string
11
11
  param(i: number): string
12
- execute(sql: string, args?: any[], ctx?: any): Promise<number>
13
- executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>
14
- query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
15
- queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T | null>
16
- executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T | null>
17
- count(sql: string, args?: any[], ctx?: any): Promise<number>
12
+ execute(sql: string, args?: any[]): Promise<number>
13
+ executeBatch(statements: Statement[], firstAffected?: boolean): Promise<number>
14
+ query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T[]>
15
+ queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[]): Promise<T | null>
16
+ executeScalar<T>(sql: string, args?: any[]): Promise<T | null>
17
+ count(sql: string, args?: any[]): Promise<number>
18
18
  }
19
19
  export interface Transaction extends Executor {
20
20
  commit(): Promise<void>