mysql2-core 0.3.9 → 0.5.1

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
@@ -59,28 +59,46 @@ export function metadata(attrs) {
59
59
  }
60
60
  return m;
61
61
  }
62
- export function buildToSave(obj, table, attrs, ver, buildParam, i) {
63
- if (!i) {
64
- i = 1;
65
- }
62
+ export function buildToSave(obj, table, attrs, pks, ver, buildParam) {
66
63
  if (!buildParam) {
67
64
  buildParam = param;
68
65
  }
69
66
  const ks = Object.keys(attrs);
70
- const pks = [];
67
+ if (!pks) {
68
+ pks = [];
69
+ for (const k of ks) {
70
+ const attr = attrs[k];
71
+ attr.name = k;
72
+ if (attr.key) {
73
+ pks.push(attr);
74
+ }
75
+ if (attr.version) {
76
+ ver = k;
77
+ }
78
+ }
79
+ }
71
80
  const cols = [];
72
81
  const values = [];
73
82
  const args = [];
74
- let isVersion = false;
75
83
  const o = obj;
84
+ let isUpdate = true;
85
+ let isVersion = false;
86
+ for (const k of pks) {
87
+ if (k.name) {
88
+ let v = o[k.name];
89
+ if (v == null) {
90
+ isUpdate = false;
91
+ }
92
+ }
93
+ }
94
+ let i = 1;
76
95
  for (const k of ks) {
77
96
  const attr = attrs[k];
78
- attr.name = k;
79
- if (attr.key) {
80
- pks.push(attr);
97
+ if (!attr) {
98
+ continue;
81
99
  }
82
100
  let v = o[k];
83
- if (v == null) {
101
+ if (v == null && attr.default !== undefined) {
84
102
  v = attr.default;
85
103
  }
86
104
  if (v != null && !attr.ignored && !attr.noinsert) {
@@ -88,6 +106,7 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
88
106
  cols.push(field);
89
107
  if (k === ver) {
90
108
  isVersion = true;
109
+ ver = k;
91
110
  values.push(`${1}`);
92
111
  }
93
112
  else {
@@ -102,11 +121,11 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
102
121
  values.push(p);
103
122
  if (typeof v === "boolean") {
104
123
  if (v === true) {
105
- const v2 = attr.true ? attr.true : `'1'`;
124
+ const v2 = attr.true !== undefined ? attr.true : `1`;
106
125
  args.push(v2);
107
126
  }
108
127
  else {
109
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`;
128
+ const v2 = attr.false !== undefined ? attr.false : `0`;
110
129
  args.push(v2);
111
130
  }
112
131
  }
@@ -117,23 +136,32 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
117
136
  }
118
137
  }
119
138
  }
120
- if (!isVersion && ver && ver.length > 0) {
139
+ if (cols.length > 0 && ver && isVersion === false) {
121
140
  const attr = attrs[ver];
122
- const field = attr.column ? attr.column : ver;
123
- cols.push(field);
124
- values.push(`${1}`);
141
+ if (attr) {
142
+ const field = attr.column ? attr.column : ver;
143
+ cols.push(field);
144
+ values.push("1");
145
+ }
125
146
  }
126
- if (pks.length === 0) {
147
+ if (isUpdate === false || pks.length === 0) {
127
148
  if (cols.length === 0) {
128
149
  return { query: "", params: args };
129
150
  }
130
151
  else {
131
- const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")})`;
132
- return { query: q, params: args };
152
+ if (pks.length === 0) {
153
+ const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")})`;
154
+ return { query: q, params: args };
155
+ }
156
+ else {
157
+ const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")})`;
158
+ return { query: q, params: args };
159
+ }
133
160
  }
134
161
  }
135
162
  else {
136
163
  const colSet = [];
164
+ let version = null;
137
165
  for (const k of ks) {
138
166
  const v = o[k];
139
167
  if (v !== undefined) {
@@ -141,116 +169,60 @@ export function buildToSave(obj, table, attrs, ver, buildParam, i) {
141
169
  if (attr && !attr.key && !attr.ignored && !attr.noupdate) {
142
170
  const field = attr.column ? attr.column : k;
143
171
  let x;
144
- if (v === null) {
145
- x = "null";
146
- }
147
- else if (v === "") {
148
- x = `''`;
149
- }
150
- else if (typeof v === "number") {
151
- x = toString(v);
172
+ if (attr.version) {
173
+ version = k;
174
+ x = `${field} + 1`;
152
175
  }
153
176
  else {
154
- x = buildParam(i++);
155
- if (typeof v === "boolean") {
156
- if (v === true) {
157
- const v2 = attr.true ? attr.true : `'1'`;
158
- args.push(v2);
177
+ if (v === "") {
178
+ x = `''`;
179
+ }
180
+ else if (typeof v === "number") {
181
+ x = toString(v);
182
+ }
183
+ else {
184
+ x = buildParam(i++);
185
+ if (typeof v === "boolean") {
186
+ if (v === true) {
187
+ const v2 = attr.true !== undefined ? attr.true : `1`;
188
+ args.push(v2);
189
+ }
190
+ else {
191
+ const v2 = attr.false !== undefined ? attr.false : `0`;
192
+ args.push(v2);
193
+ }
159
194
  }
160
195
  else {
161
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`;
162
- args.push(v2);
196
+ args.push(v);
163
197
  }
164
198
  }
165
- else {
166
- args.push(v);
167
- }
168
199
  }
169
200
  colSet.push(`${field}=${x}`);
170
201
  }
171
202
  }
172
203
  }
173
- if (colSet.length === 0) {
174
- const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")})`;
175
- return { query: q, params: args };
176
- }
177
- else {
178
- const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")}) on duplicate key update ${colSet.join(",")}`;
179
- return { query: q, params: args };
180
- }
181
- }
182
- }
183
- export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
184
- if (!buildParam) {
185
- buildParam = param;
186
- }
187
- const sts = [];
188
- const ks = Object.keys(attrs);
189
- for (const obj of objs) {
190
- let i = 1;
191
- const cols = [];
192
- const values = [];
193
- const args = [];
194
- let isVersion = false;
195
- const o = obj;
196
- for (const k of ks) {
197
- const attr = attrs[k];
198
- let v = o[k];
199
- if (v == null) {
200
- v = attr.default;
204
+ if (cols.length > 0) {
205
+ if (colSet.length === 0) {
206
+ const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")})`;
207
+ return { query: q, params: args };
201
208
  }
202
- if (v != null && !attr.ignored && !attr.noinsert) {
203
- const field = attr.column ? attr.column : k;
204
- cols.push(field);
205
- if (k === ver) {
206
- isVersion = true;
207
- values.push(`${1}`);
208
- }
209
- else {
210
- if (v === "") {
211
- values.push(`''`);
212
- }
213
- else if (typeof v === "number") {
214
- values.push(toString(v));
215
- }
216
- else {
217
- const p = buildParam(i++);
218
- values.push(p);
219
- if (typeof v === "boolean") {
220
- if (v === true) {
221
- const v2 = attr.true ? attr.true : `'1'`;
222
- args.push(v2);
223
- }
224
- else {
225
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`;
226
- args.push(v2);
227
- }
228
- }
229
- else {
230
- args.push(v);
231
- }
232
- }
233
- }
209
+ else {
210
+ const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")}) on duplicate key update ${colSet.join(",")}`;
211
+ return { query: q, params: args };
234
212
  }
235
213
  }
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
- const colSet = [];
243
- for (const k of ks) {
244
- const v = o[k];
245
- if (v !== undefined) {
246
- const attr = attrs[k];
247
- if (attr && !attr.key && !attr.ignored && k !== ver && !attr.noupdate) {
248
- const field = attr.column ? attr.column : k;
214
+ else {
215
+ if (colSet.length === 0) {
216
+ return { query: "", params: args };
217
+ }
218
+ else {
219
+ const colQuery = [];
220
+ for (const attr of pks) {
221
+ const field = attr.column ? attr.column : attr.name;
222
+ const na = attr.name ? attr.name : "";
223
+ const v = o[na];
249
224
  let x;
250
- if (v === null) {
251
- x = "null";
252
- }
253
- else if (v === "") {
225
+ if (v === "") {
254
226
  x = `''`;
255
227
  }
256
228
  else if (typeof v === "number") {
@@ -260,11 +232,11 @@ export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
260
232
  x = buildParam(i++);
261
233
  if (typeof v === "boolean") {
262
234
  if (v === true) {
263
- const v2 = attr.true ? attr.true : `'1'`;
235
+ const v2 = attr.true !== undefined ? "" + attr.true : `1`;
264
236
  args.push(v2);
265
237
  }
266
238
  else {
267
- const v2 = attr.false && attr.false !== 0 ? attr.false : `'0'`;
239
+ const v2 = attr.false !== undefined ? "" + attr.false : `0`;
268
240
  args.push(v2);
269
241
  }
270
242
  }
@@ -272,18 +244,46 @@ export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
272
244
  args.push(v);
273
245
  }
274
246
  }
275
- colSet.push(`${field}=${x}`);
247
+ colQuery.push(`${field}=${x}`);
248
+ }
249
+ if (version && version.length > 0) {
250
+ const v = o[version];
251
+ if (typeof v === "number" && !isNaN(v)) {
252
+ const attr = attrs[version];
253
+ if (attr) {
254
+ const field = attr.column ? attr.column : version;
255
+ colQuery.push(`${field}=${v}`);
256
+ }
257
+ }
276
258
  }
259
+ const q = `update ${table} set ${colSet.join(",")} where ${colQuery.join(" and ")}`;
260
+ return { query: q, params: args };
277
261
  }
278
262
  }
279
- if (colSet.length === 0) {
280
- const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")});`;
281
- const smt = { query: q, params: args };
282
- sts.push(smt);
263
+ }
264
+ }
265
+ export function buildToSaveBatch(objs, table, attrs, pks, ver, buildParam) {
266
+ if (!buildParam) {
267
+ buildParam = param;
268
+ }
269
+ const sts = [];
270
+ if (!pks) {
271
+ pks = [];
272
+ const ks = Object.keys(attrs);
273
+ for (const k of ks) {
274
+ const attr = attrs[k];
275
+ attr.name = k;
276
+ if (attr.key) {
277
+ pks.push(attr);
278
+ }
279
+ if (attr.version) {
280
+ ver = k;
281
+ }
283
282
  }
284
- else {
285
- const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")}) on duplicate key update ${colSet.join(",")};`;
286
- const smt = { query: q, params: args };
283
+ }
284
+ for (const obj of objs) {
285
+ const smt = buildToSave(obj, table, attrs, pks, ver, buildParam);
286
+ if (smt.query) {
287
287
  sts.push(smt);
288
288
  }
289
289
  }
@@ -313,32 +313,24 @@ export function getConnection(pool) {
313
313
  });
314
314
  });
315
315
  }
316
- export function beginTransaction(connection, rollbackIfError) {
316
+ export function beginTransaction(connection) {
317
317
  return new Promise((resolve, reject) => {
318
- connection.beginTransaction((err) => {
319
- if (err) {
320
- if (!rollbackIfError) {
321
- return reject(err);
322
- }
323
- return connection.rollback(() => {
324
- return reject(err);
325
- });
318
+ connection.beginTransaction((beginError) => {
319
+ if (beginError) {
320
+ connection.release();
321
+ return reject(beginError);
326
322
  }
327
323
  return resolve();
328
324
  });
329
325
  });
330
326
  }
331
- export function commit(connection, rollbackIfError) {
327
+ export function commit(connection) {
332
328
  return new Promise((resolve, reject) => {
333
329
  connection.commit((err) => {
334
330
  if (err) {
335
- if (!rollbackIfError) {
336
- return reject(err);
337
- }
338
- return connection.rollback(() => {
339
- return reject(err);
340
- });
331
+ return reject(err);
341
332
  }
333
+ connection.release();
342
334
  return resolve();
343
335
  });
344
336
  });
@@ -346,6 +338,7 @@ export function commit(connection, rollbackIfError) {
346
338
  export function rollback(connection) {
347
339
  return new Promise((resolve, reject) => {
348
340
  connection.rollback((err) => {
341
+ connection.release();
349
342
  if (err) {
350
343
  return reject(err);
351
344
  }
@@ -376,39 +369,31 @@ export class PoolManager {
376
369
  }
377
370
  connection.beginTransaction((beginError) => {
378
371
  if (beginError) {
379
- return connection.rollback(() => {
380
- connection.release();
381
- return reject(beginError);
382
- });
372
+ connection.release();
373
+ return reject(beginError);
383
374
  }
384
375
  return resolve(new PoolConnectionManager(connection));
385
376
  });
386
377
  });
387
378
  });
388
379
  }
389
- execute(sql, args, ctx) {
390
- const p = ctx ? ctx : this.pool;
391
- return execute(p, sql, args);
380
+ execute(sql, args) {
381
+ return execute(this.pool, sql, args);
392
382
  }
393
- executeBatch(statements, firstSuccess, ctx) {
394
- const p = ctx ? ctx : this.pool;
395
- return executeBatch(p, statements, firstSuccess);
383
+ executeBatch(statements, requireFirstAffected) {
384
+ return executeBatch(this.pool, statements, requireFirstAffected);
396
385
  }
397
- query(sql, args, m, bools, ctx) {
398
- const p = ctx ? ctx : this.pool;
399
- return query(p, sql, args, m, bools);
386
+ query(sql, args, m, bools) {
387
+ return query(this.pool, sql, args, m, bools);
400
388
  }
401
- queryOne(sql, args, m, bools, ctx) {
402
- const p = ctx ? ctx : this.pool;
403
- return queryOne(p, sql, args, m, bools);
389
+ queryOne(sql, args, m, bools) {
390
+ return queryOne(this.pool, sql, args, m, bools);
404
391
  }
405
- executeScalar(sql, args, ctx) {
406
- const p = ctx ? ctx : this.pool;
407
- return executeScalar(p, sql, args);
392
+ executeScalar(sql, args) {
393
+ return executeScalar(this.pool, sql, args);
408
394
  }
409
- count(sql, args, ctx) {
410
- const p = ctx ? ctx : this.pool;
411
- return count(p, sql, args);
395
+ count(sql, args) {
396
+ return count(this.pool, sql, args);
412
397
  }
413
398
  }
414
399
  export class PoolConnectionManager {
@@ -423,6 +408,7 @@ export class PoolConnectionManager {
423
408
  this.queryOne = this.queryOne.bind(this);
424
409
  this.executeScalar = this.executeScalar.bind(this);
425
410
  this.count = this.count.bind(this);
411
+ this.release = this.release.bind(this);
426
412
  }
427
413
  param(i) {
428
414
  return "?";
@@ -431,10 +417,7 @@ export class PoolConnectionManager {
431
417
  return new Promise((resolve, reject) => {
432
418
  this.connection.commit((err) => {
433
419
  if (err) {
434
- return this.connection.rollback(() => {
435
- this.release();
436
- return reject(err);
437
- });
420
+ return reject(err);
438
421
  }
439
422
  this.release();
440
423
  return resolve();
@@ -459,32 +442,26 @@ export class PoolConnectionManager {
459
442
  this.released = true;
460
443
  this.connection.release();
461
444
  }
462
- execute(sql, args, ctx) {
463
- const p = ctx ? ctx : this.connection;
464
- return execute(p, sql, args);
445
+ execute(sql, args) {
446
+ return execute(this.connection, sql, args);
465
447
  }
466
- executeBatch(statements, firstSuccess, ctx) {
467
- const p = ctx ? ctx : this.connection;
468
- return executeBatchConnection(p, statements, firstSuccess);
448
+ executeBatch(statements, requireFirstAffected) {
449
+ return executeBatchConnection(this.connection, statements, requireFirstAffected);
469
450
  }
470
- query(sql, args, m, bools, ctx) {
471
- const p = ctx ? ctx : this.connection;
472
- return query(p, sql, args, m, bools);
451
+ query(sql, args, m, bools) {
452
+ return query(this.connection, sql, args, m, bools);
473
453
  }
474
- queryOne(sql, args, m, bools, ctx) {
475
- const p = ctx ? ctx : this.connection;
476
- return queryOne(p, sql, args, m, bools);
454
+ queryOne(sql, args, m, bools) {
455
+ return queryOne(this.connection, sql, args, m, bools);
477
456
  }
478
- executeScalar(sql, args, ctx) {
479
- const p = ctx ? ctx : this.connection;
480
- return executeScalar(p, sql, args);
457
+ executeScalar(sql, args) {
458
+ return executeScalar(this.connection, sql, args);
481
459
  }
482
- count(sql, args, ctx) {
483
- const p = ctx ? ctx : this.connection;
484
- return count(p, sql, args);
460
+ count(sql, args) {
461
+ return count(this.connection, sql, args);
485
462
  }
486
463
  }
487
- export function executeBatch(pool, statements, firstSuccess) {
464
+ export function executeBatch(pool, statements, requireFirstAffected) {
488
465
  return __awaiter(this, void 0, void 0, function* () {
489
466
  if (!statements || statements.length === 0) {
490
467
  return 0;
@@ -493,31 +470,28 @@ export function executeBatch(pool, statements, firstSuccess) {
493
470
  return execute(pool, statements[0].query, statements[0].params);
494
471
  }
495
472
  const connection = yield getConnection(pool);
496
- try {
497
- return yield executeBatchConnectionTx(connection, statements, firstSuccess);
498
- }
499
- finally {
500
- connection.release();
501
- }
473
+ return yield executeBatchConnectionTx(connection, statements, requireFirstAffected);
502
474
  });
503
475
  }
504
- export function executeBatchConnectionTx(connection, statements, firstSuccess) {
476
+ export function executeBatchConnectionTx(connection, statements, requireFirstAffected) {
505
477
  return __awaiter(this, void 0, void 0, function* () {
506
478
  if (!statements || statements.length === 0) {
507
479
  return 0;
508
480
  }
509
481
  if (statements.length === 1) {
510
- return execute(connection, statements[0].query, statements[0].params);
482
+ const count = yield execute(connection, statements[0].query, statements[0].params);
483
+ connection.release();
484
+ return count;
511
485
  }
512
- yield beginTransaction(connection, true);
486
+ yield beginTransaction(connection);
513
487
  try {
514
488
  let count = 0;
515
489
  if (resource.multipleStatements) {
516
- if (firstSuccess) {
490
+ if (requireFirstAffected) {
517
491
  const firstCount = yield execute(connection, statements[0].query, statements[0].params);
518
492
  count = firstCount;
519
493
  if (firstCount === 0) {
520
- yield commit(connection, true);
494
+ yield commit(connection);
521
495
  return 0;
522
496
  }
523
497
  const remaining = statements.slice(1);
@@ -528,12 +502,12 @@ export function executeBatchConnectionTx(connection, statements, firstSuccess) {
528
502
  }
529
503
  }
530
504
  else {
531
- const start = firstSuccess ? 1 : 0;
532
- if (firstSuccess) {
505
+ const start = requireFirstAffected ? 1 : 0;
506
+ if (requireFirstAffected) {
533
507
  const firstCount = yield execute(connection, statements[0].query, statements[0].params);
534
508
  count = firstCount;
535
509
  if (firstCount === 0) {
536
- yield commit(connection, true);
510
+ yield commit(connection);
537
511
  return 0;
538
512
  }
539
513
  }
@@ -542,7 +516,7 @@ export function executeBatchConnectionTx(connection, statements, firstSuccess) {
542
516
  count += yield execute(connection, item.query, item.params);
543
517
  }
544
518
  }
545
- yield commit(connection, true);
519
+ yield commit(connection);
546
520
  return count;
547
521
  }
548
522
  catch (err) {
@@ -551,11 +525,12 @@ export function executeBatchConnectionTx(connection, statements, firstSuccess) {
551
525
  }
552
526
  catch (_a) {
553
527
  }
554
- throw buildError(err);
528
+ buildError(err);
529
+ throw err;
555
530
  }
556
531
  });
557
532
  }
558
- export function executeBatchConnection(connection, statements, firstSuccess) {
533
+ export function executeBatchConnection(connection, statements, requireFirstAffected) {
559
534
  return __awaiter(this, void 0, void 0, function* () {
560
535
  if (!statements || statements.length === 0) {
561
536
  return 0;
@@ -564,7 +539,7 @@ export function executeBatchConnection(connection, statements, firstSuccess) {
564
539
  return execute(connection, statements[0].query, statements[0].params);
565
540
  }
566
541
  if (resource.multipleStatements) {
567
- if (firstSuccess) {
542
+ if (requireFirstAffected) {
568
543
  const firstCount = yield execute(connection, statements[0].query, statements[0].params);
569
544
  if (firstCount === 0) {
570
545
  return 0;
@@ -574,8 +549,8 @@ export function executeBatchConnection(connection, statements, firstSuccess) {
574
549
  return executeMultipleStatements(connection, statements);
575
550
  }
576
551
  let count = 0;
577
- const start = firstSuccess ? 1 : 0;
578
- if (firstSuccess) {
552
+ const start = requireFirstAffected ? 1 : 0;
553
+ if (requireFirstAffected) {
579
554
  const firstCount = yield execute(connection, statements[0].query, statements[0].params);
580
555
  count = firstCount;
581
556
  if (firstCount === 0) {
@@ -693,27 +668,6 @@ export function executeScalar(pool, sql, args) {
693
668
  export function count(pool, sql, args) {
694
669
  return executeScalar(pool, sql, args).then((res) => (res !== null ? res : 0));
695
670
  }
696
- export function save(pool, obj, table, attrs, ver, buildParam) {
697
- const s = buildToSave(obj, table, attrs, ver, buildParam);
698
- if (!s.query) {
699
- return Promise.resolve(-1);
700
- }
701
- if (typeof pool === "function") {
702
- return pool(s.query, s.params);
703
- }
704
- else {
705
- return execute(pool, s.query, s.params);
706
- }
707
- }
708
- export function saveBatch(pool, objs, table, attrs, ver, buildParam) {
709
- const s = buildToSaveBatch(objs, table, attrs, ver, buildParam);
710
- if (typeof pool === "function") {
711
- return pool(s);
712
- }
713
- else {
714
- return executeBatch(pool, s);
715
- }
716
- }
717
671
  export function toArray(arr) {
718
672
  if (!arr || arr.length === 0) {
719
673
  return [];
@@ -905,17 +859,8 @@ export class StringAdapter {
905
859
  return execute(this.pool, s, values);
906
860
  }
907
861
  }
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
- }
862
+ export const StringRepository = StringAdapter;
863
+ export const StringService = StringAdapter;
919
864
  export class MySQLWriter {
920
865
  constructor(pool, table, attributes, oneIfSuccess, map, buildParam) {
921
866
  this.pool = pool;
@@ -925,10 +870,9 @@ export class MySQLWriter {
925
870
  this.map = map;
926
871
  this.write = this.write.bind(this);
927
872
  this.param = buildParam ? buildParam : param;
928
- const x = version(attributes);
929
- if (x) {
930
- this.version = x.name;
931
- }
873
+ const m = metadata(attributes);
874
+ this.keys = m.keys;
875
+ this.version = m.version;
932
876
  }
933
877
  write(obj) {
934
878
  if (!obj) {
@@ -938,7 +882,7 @@ export class MySQLWriter {
938
882
  if (this.map) {
939
883
  obj2 = this.map(obj);
940
884
  }
941
- const stmt = buildToSave(obj2, this.table, this.attributes, this.version, this.param);
885
+ const stmt = buildToSave(obj2, this.table, this.attributes, this.keys, this.version, this.param);
942
886
  if (stmt.query) {
943
887
  if (this.oneIfSuccess) {
944
888
  return execute(this.pool, stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0));
@@ -952,7 +896,7 @@ export class MySQLWriter {
952
896
  }
953
897
  }
954
898
  }
955
- export class MySQLStreamWriter {
899
+ export class BufferedBatchWriter {
956
900
  constructor(pool, table, attributes, size = 1000, map, buildParam) {
957
901
  this.pool = pool;
958
902
  this.table = table;
@@ -963,10 +907,9 @@ export class MySQLStreamWriter {
963
907
  this.write = this.write.bind(this);
964
908
  this.flush = this.flush.bind(this);
965
909
  this.param = buildParam ? buildParam : param;
966
- const x = version(attributes);
967
- if (x) {
968
- this.version = x.name;
969
- }
910
+ const m = metadata(attributes);
911
+ this.keys = m.keys;
912
+ this.version = m.version;
970
913
  }
971
914
  write(obj) {
972
915
  if (!obj) {
@@ -992,33 +935,31 @@ export class MySQLStreamWriter {
992
935
  return Promise.resolve(0);
993
936
  }
994
937
  else {
995
- const total = this.list.length;
996
- const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.version, this.param);
938
+ const stmt = buildToSaveBatch(this.list, this.table, this.attributes, this.keys, this.version, this.param);
997
939
  if (stmt.length > 0) {
998
940
  return executeBatch(this.pool, stmt).then((r) => {
999
941
  this.list = [];
1000
- return total;
942
+ return r;
1001
943
  });
1002
944
  }
1003
945
  else {
946
+ this.list = [];
1004
947
  return Promise.resolve(0);
1005
948
  }
1006
949
  }
1007
950
  }
1008
951
  }
1009
- export class MySQLBatchWriter {
1010
- constructor(pool, table, attributes, oneIfSuccess, map, buildParam) {
952
+ export class BatchWriter {
953
+ constructor(pool, table, attributes, map, buildParam) {
1011
954
  this.pool = pool;
1012
955
  this.table = table;
1013
956
  this.attributes = attributes;
1014
- this.oneIfSuccess = oneIfSuccess;
1015
957
  this.map = map;
1016
958
  this.write = this.write.bind(this);
1017
959
  this.param = buildParam ? buildParam : param;
1018
- const x = version(attributes);
1019
- if (x) {
1020
- this.version = x.name;
1021
- }
960
+ const m = metadata(attributes);
961
+ this.keys = m.keys;
962
+ this.version = m.version;
1022
963
  }
1023
964
  write(objs) {
1024
965
  if (!objs || objs.length === 0) {
@@ -1032,14 +973,9 @@ export class MySQLBatchWriter {
1032
973
  list.push(obj2);
1033
974
  }
1034
975
  }
1035
- const stmts = buildToSaveBatch(list, this.table, this.attributes, this.version, this.param);
976
+ const stmts = buildToSaveBatch(list, this.table, this.attributes, this.keys, this.version, this.param);
1036
977
  if (stmts.length > 0) {
1037
- if (this.oneIfSuccess) {
1038
- return executeBatch(this.pool, stmts).then((ct) => stmts.length);
1039
- }
1040
- else {
1041
- return executeBatch(this.pool, stmts);
1042
- }
978
+ return executeBatch(this.pool, stmts);
1043
979
  }
1044
980
  else {
1045
981
  return Promise.resolve(0);
@@ -1109,38 +1045,38 @@ export class Exporter {
1109
1045
  const reader = this.connection.query(stmt.query, stmt.params);
1110
1046
  let er;
1111
1047
  let i = 0;
1112
- let k = 0;
1048
+ let j = 0;
1113
1049
  reader.on("error", (err) => (er = err));
1114
1050
  if (this.map) {
1115
1051
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1116
1052
  ++i;
1117
- k++;
1053
+ j++;
1118
1054
  this.connection.pause();
1119
1055
  const obj = mapOne(row, this.map);
1120
1056
  const data = this.format(obj);
1121
1057
  this.write(data);
1122
1058
  this.connection.resume();
1123
- if (k >= this.progressSize) {
1059
+ if (j >= this.progressSize) {
1124
1060
  if (this.logInfo) {
1125
1061
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1126
1062
  }
1127
- k = 0;
1063
+ j = 0;
1128
1064
  }
1129
1065
  }));
1130
1066
  }
1131
1067
  else {
1132
1068
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1133
1069
  ++i;
1134
- k++;
1070
+ j++;
1135
1071
  this.connection.pause();
1136
1072
  const data = this.format(row);
1137
1073
  this.write(data);
1138
1074
  this.connection.resume();
1139
- if (k >= this.progressSize) {
1075
+ if (j >= this.progressSize) {
1140
1076
  if (this.logInfo) {
1141
1077
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1142
1078
  }
1143
- k = 0;
1079
+ j = 0;
1144
1080
  }
1145
1081
  }));
1146
1082
  }
@@ -1187,37 +1123,37 @@ export class ExportService {
1187
1123
  let er;
1188
1124
  reader.on("error", (err) => (er = err));
1189
1125
  let i = 0;
1190
- let k = 0;
1126
+ let j = 0;
1191
1127
  if (this.map) {
1192
1128
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1193
1129
  ++i;
1194
- k++;
1130
+ j++;
1195
1131
  this.connection.pause();
1196
1132
  const obj = mapOne(row, this.map);
1197
1133
  const data = this.formatter.format(obj);
1198
1134
  this.writer.write(data);
1199
1135
  this.connection.resume();
1200
- if (k >= this.progressSize) {
1136
+ if (j >= this.progressSize) {
1201
1137
  if (this.logInfo) {
1202
1138
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1203
1139
  }
1204
- k = 0;
1140
+ j = 0;
1205
1141
  }
1206
1142
  }));
1207
1143
  }
1208
1144
  else {
1209
1145
  reader.on("result", (row) => __awaiter(this, void 0, void 0, function* () {
1210
1146
  ++i;
1211
- k++;
1147
+ j++;
1212
1148
  this.connection.pause();
1213
1149
  const data = this.formatter.format(row);
1214
1150
  this.writer.write(data);
1215
1151
  this.connection.resume();
1216
- if (k >= this.progressSize) {
1152
+ if (j >= this.progressSize) {
1217
1153
  if (this.logInfo) {
1218
1154
  this.logInfo(`Progress: ${i} records processed of file '${this.filename}'`);
1219
1155
  }
1220
- k = 0;
1156
+ j = 0;
1221
1157
  }
1222
1158
  }));
1223
1159
  }