query-core 0.6.0 → 0.6.2

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/batch.js CHANGED
@@ -24,7 +24,7 @@ var SqlInserter = (function () {
24
24
  obj2 = this.map(obj);
25
25
  }
26
26
  var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
27
- if (stmt.query.length > 0) {
27
+ if (stmt.query.length) {
28
28
  if (this.oneIfSuccess) {
29
29
  return this.exec(stmt.query, stmt.params).then(function (ct) { return (ct > 0 ? 1 : 0); });
30
30
  }
@@ -62,7 +62,7 @@ var SqlUpdater = (function () {
62
62
  obj2 = this.map(obj);
63
63
  }
64
64
  var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
65
- if (stmt.query.length > 0) {
65
+ if (stmt.query.length) {
66
66
  if (this.oneIfSuccess) {
67
67
  return this.exec(stmt.query, stmt.params).then(function (ct) { return (ct > 0 ? 1 : 0); });
68
68
  }
@@ -105,7 +105,7 @@ var SqlBatchInserter = (function () {
105
105
  }
106
106
  }
107
107
  var stmt = build_1.buildToInsertBatch(list, this.table, this.attributes, this.param, this.version);
108
- if (stmt.query.length > 0) {
108
+ if (stmt.query.length) {
109
109
  if (this.oneIfSuccess) {
110
110
  return this.exec(stmt.query, stmt.params).then(function (ct) { return objs.length; });
111
111
  }
@@ -210,7 +210,7 @@ var StreamInserter = (function () {
210
210
  else {
211
211
  var total_1 = this.list.length;
212
212
  var stmt = build_1.buildToInsertBatch(this.list, this.table, this.attributes, this.param, this.version);
213
- if (stmt.query.length > 0) {
213
+ if (stmt.query.length) {
214
214
  return this.exec(stmt.query, stmt.params).then(function (r) {
215
215
  _this.list = [];
216
216
  return total_1;
package/lib/build.js CHANGED
@@ -124,7 +124,7 @@ function buildToDelete(obj, table, ks, buildParam, i) {
124
124
  exports.buildToDelete = buildToDelete;
125
125
  function insert(exec, obj, table, attrs, buildParam, ver, i) {
126
126
  var stm = buildToInsert(obj, table, attrs, buildParam, ver, i);
127
- if (stm.query.length === 0) {
127
+ if (!stm.query) {
128
128
  return Promise.resolve(0);
129
129
  }
130
130
  else {
@@ -217,7 +217,7 @@ function buildToInsert(obj, table, attrs, buildParam, ver, i) {
217
217
  exports.buildToInsert = buildToInsert;
218
218
  function insertBatch(exec, objs, table, attrs, buildParam, ver, i) {
219
219
  var stm = buildToInsertBatch(objs, table, attrs, buildParam, ver, i);
220
- if (stm.query.length === 0) {
220
+ if (!stm.query) {
221
221
  return Promise.resolve(0);
222
222
  }
223
223
  else {
@@ -397,7 +397,7 @@ function buildToInsertBatch(objs, table, attrs, buildParam, ver, i) {
397
397
  exports.buildToInsertBatch = buildToInsertBatch;
398
398
  function update(exec, obj, table, attrs, buildParam, ver, i) {
399
399
  var stm = buildToUpdate(obj, table, attrs, buildParam, ver, i);
400
- if (stm.query.length === 0) {
400
+ if (!stm.query) {
401
401
  return Promise.resolve(0);
402
402
  }
403
403
  else {
package/lib/client.js CHANGED
@@ -105,7 +105,7 @@ var ProxyClient = (function () {
105
105
  };
106
106
  ProxyClient.prototype.insert = function (table, attrs, obj, buildParam, ver) {
107
107
  var s = build_1.buildToInsert(obj, table, attrs, buildParam, ver);
108
- if (s.query.length > 0) {
108
+ if (s.query) {
109
109
  return this.exec(s.query, s.params);
110
110
  }
111
111
  else {
@@ -114,7 +114,7 @@ var ProxyClient = (function () {
114
114
  };
115
115
  ProxyClient.prototype.update = function (table, attrs, obj, buildParam, ver) {
116
116
  var s = build_1.buildToUpdate(obj, table, attrs, buildParam, ver);
117
- if (s.query.length > 0) {
117
+ if (s.query) {
118
118
  return this.exec(s.query, s.params);
119
119
  }
120
120
  else {
@@ -123,7 +123,7 @@ var ProxyClient = (function () {
123
123
  };
124
124
  ProxyClient.prototype.insertBatch = function (table, attrs, objs, buildParam, driver) {
125
125
  var s = build_1.buildToInsertBatch(objs, table, attrs, buildParam);
126
- if (s.query.length > 0) {
126
+ if (s.query) {
127
127
  return this.exec(s.query, s.params);
128
128
  }
129
129
  else {
@@ -141,7 +141,7 @@ var ProxyClient = (function () {
141
141
  };
142
142
  ProxyClient.prototype.insertWithTx = function (tx, commit, table, attrs, obj, buildParam, ver) {
143
143
  var s = build_1.buildToInsert(obj, table, attrs, buildParam, ver);
144
- if (s.query.length > 0) {
144
+ if (s.query) {
145
145
  return this.execWithTx(tx, commit, s.query, s.params);
146
146
  }
147
147
  else {
@@ -150,7 +150,7 @@ var ProxyClient = (function () {
150
150
  };
151
151
  ProxyClient.prototype.updateWithTx = function (tx, commit, table, attrs, obj, buildParam, ver) {
152
152
  var s = build_1.buildToUpdate(obj, table, attrs, buildParam, ver);
153
- if (s.query.length > 0) {
153
+ if (s.query) {
154
154
  return this.execWithTx(tx, commit, s.query, s.params);
155
155
  }
156
156
  else {
@@ -159,7 +159,7 @@ var ProxyClient = (function () {
159
159
  };
160
160
  ProxyClient.prototype.insertBatchWithTx = function (tx, commit, table, attrs, objs, buildParam, driver) {
161
161
  var s = build_1.buildToInsertBatch(objs, table, attrs, buildParam);
162
- if (s.query.length > 0) {
162
+ if (s.query) {
163
163
  return this.execWithTx(tx, commit, s.query, s.params);
164
164
  }
165
165
  else {
package/lib/index.js CHANGED
@@ -25,11 +25,11 @@ __export(require("./search"));
25
25
  __export(require("./SearchBuilder"));
26
26
  __export(require("./services"));
27
27
  var Loader = (function () {
28
- function Loader(query, sql, m, bools) {
28
+ function Loader(query, sql, map, bools) {
29
29
  this.query = query;
30
30
  this.sql = sql;
31
+ this.map = map;
31
32
  this.bools = bools;
32
- this.map = m;
33
33
  this.load = this.load.bind(this);
34
34
  }
35
35
  Loader.prototype.load = function () {
package/lib/services.js CHANGED
@@ -15,17 +15,17 @@ var __extends = (this && this.__extends) || (function () {
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  var build_1 = require("./build");
17
17
  var SearchBuilder_1 = require("./SearchBuilder");
18
- function useGet(q, table, attrs, param, fromDB) {
19
- var l = new SqlLoader(q, table, attrs, param, fromDB);
18
+ function useGet(q, param, table, attrs, fromDB) {
19
+ var l = new SqlLoader(q, param, table, attrs, fromDB);
20
20
  return l.load;
21
21
  }
22
22
  exports.useGet = useGet;
23
23
  exports.useLoad = useGet;
24
24
  var SqlLoader = (function () {
25
- function SqlLoader(query, table, attrs, param, fromDB) {
25
+ function SqlLoader(query, param, table, attrs, fromDB) {
26
26
  this.query = query;
27
- this.table = table;
28
27
  this.param = param;
28
+ this.table = table;
29
29
  this.fromDB = fromDB;
30
30
  if (Array.isArray(attrs)) {
31
31
  this.primaryKeys = build_1.attributes(attrs);
@@ -48,18 +48,18 @@ var SqlLoader = (function () {
48
48
  SqlLoader.prototype.metadata = function () {
49
49
  return this.attributes;
50
50
  };
51
- SqlLoader.prototype.all = function () {
51
+ SqlLoader.prototype.all = function (ctx) {
52
52
  var sql = "select * from " + this.table;
53
- return this.query(sql, [], this.map);
53
+ return this.query(sql, [], this.map, this.bools, ctx);
54
54
  };
55
55
  SqlLoader.prototype.load = function (id, ctx) {
56
56
  var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
57
- if (!stmt) {
57
+ if (!stmt.query) {
58
58
  throw new Error("cannot build query by id");
59
59
  }
60
60
  var fn = this.fromDB;
61
61
  if (fn) {
62
- return this.query(stmt.query, stmt.params, this.map, ctx).then(function (res) {
62
+ return this.query(stmt.query, stmt.params, this.map, this.bools, ctx).then(function (res) {
63
63
  if (!res || res.length === 0) {
64
64
  return null;
65
65
  }
@@ -70,16 +70,16 @@ var SqlLoader = (function () {
70
70
  });
71
71
  }
72
72
  else {
73
- return this.query(stmt.query, stmt.params, this.map).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
73
+ return this.query(stmt.query, stmt.params, this.map, this.bools, ctx).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
74
74
  }
75
75
  };
76
76
  SqlLoader.prototype.exist = function (id, ctx) {
77
77
  var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
78
78
  var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
79
- if (!stmt) {
79
+ if (!stmt.query) {
80
80
  throw new Error("cannot build query by id");
81
81
  }
82
- return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
82
+ return this.query(stmt.query, stmt.params, undefined, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
83
83
  };
84
84
  return SqlLoader;
85
85
  }());
@@ -96,7 +96,7 @@ var QueryRepository = (function () {
96
96
  this.map = m.map;
97
97
  this.bools = m.bools;
98
98
  }
99
- QueryRepository.prototype.query = function (ids) {
99
+ QueryRepository.prototype.query = function (ids, ctx) {
100
100
  if (!ids || ids.length === 0) {
101
101
  return Promise.resolve([]);
102
102
  }
@@ -109,7 +109,8 @@ var QueryRepository = (function () {
109
109
  if (this.sort && this.sort.length > 0) {
110
110
  sql = sql + " order by " + this.sort;
111
111
  }
112
- return this.db.query(sql, ids, this.map, this.bools);
112
+ var db = ctx ? ctx : this.db;
113
+ return db.query(sql, ids, this.map, this.bools);
113
114
  };
114
115
  return QueryRepository;
115
116
  }());
@@ -407,9 +408,8 @@ function diff(d1) {
407
408
  }
408
409
  exports.diff = diff;
409
410
  var SqlWriter = (function () {
410
- function SqlWriter(exec, param, table, attributes, toDB) {
411
- this.exec = exec;
412
- this.param = param;
411
+ function SqlWriter(db, table, attributes, toDB) {
412
+ this.db = db;
413
413
  this.table = table;
414
414
  this.attributes = attributes;
415
415
  this.toDB = toDB;
@@ -426,9 +426,10 @@ var SqlWriter = (function () {
426
426
  if (this.toDB) {
427
427
  obj2 = this.toDB(obj);
428
428
  }
429
- var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
430
- if (stmt.query.length > 0) {
431
- return this.exec(stmt.query, stmt.params, ctx).catch(function (err) {
429
+ var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.db.param, this.version);
430
+ if (stmt.query) {
431
+ var db = ctx ? ctx : this.db;
432
+ return db.exec(stmt.query, stmt.params).catch(function (err) {
432
433
  if (err && err.error === "duplicate") {
433
434
  return 0;
434
435
  }
@@ -438,7 +439,7 @@ var SqlWriter = (function () {
438
439
  });
439
440
  }
440
441
  else {
441
- return Promise.resolve(0);
442
+ return Promise.resolve(-1);
442
443
  }
443
444
  };
444
445
  SqlWriter.prototype.update = function (obj, ctx) {
@@ -446,12 +447,13 @@ var SqlWriter = (function () {
446
447
  if (this.toDB) {
447
448
  obj2 = this.toDB(obj);
448
449
  }
449
- var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
450
- if (stmt.query.length > 0) {
451
- return this.exec(stmt.query, stmt.params, ctx);
450
+ var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.db.param, this.version);
451
+ if (stmt.query) {
452
+ var db = ctx ? ctx : this.db;
453
+ return db.exec(stmt.query, stmt.params);
452
454
  }
453
455
  else {
454
- return Promise.resolve(0);
456
+ return Promise.resolve(-1);
455
457
  }
456
458
  };
457
459
  SqlWriter.prototype.patch = function (obj, ctx) {
@@ -463,9 +465,8 @@ exports.SqlWriter = SqlWriter;
463
465
  var CRUDRepository = (function (_super) {
464
466
  __extends(CRUDRepository, _super);
465
467
  function CRUDRepository(db, table, attributes, toDB, fromDB) {
466
- var _this = _super.call(this, db.exec, db.param, table, attributes, toDB) || this;
468
+ var _this = _super.call(this, db, table, attributes, toDB) || this;
467
469
  _this.fromDB = fromDB;
468
- _this.query = db.query;
469
470
  var m = build_1.metadata(attributes);
470
471
  _this.primaryKeys = m.keys;
471
472
  _this.map = m.map;
@@ -480,18 +481,20 @@ var CRUDRepository = (function (_super) {
480
481
  CRUDRepository.prototype.metadata = function () {
481
482
  return this.attributes;
482
483
  };
483
- CRUDRepository.prototype.all = function () {
484
+ CRUDRepository.prototype.all = function (ctx) {
484
485
  var sql = "select * from " + this.table;
485
- return this.query(sql, [], this.map);
486
+ var db = ctx ? ctx : this.db;
487
+ return db.query(sql, [], this.map, this.bools);
486
488
  };
487
489
  CRUDRepository.prototype.load = function (id, ctx) {
488
- var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
489
- if (stmt.query.length === 0) {
490
+ var stmt = build_1.select(id, this.table, this.primaryKeys, this.db.param);
491
+ if (!stmt.query) {
490
492
  throw new Error("cannot build query by id");
491
493
  }
492
494
  var fn = this.fromDB;
495
+ var db = ctx ? ctx : this.db;
493
496
  if (fn) {
494
- return this.query(stmt.query, stmt.params, this.map, ctx).then(function (res) {
497
+ return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) {
495
498
  if (!res || res.length === 0) {
496
499
  return null;
497
500
  }
@@ -502,24 +505,26 @@ var CRUDRepository = (function (_super) {
502
505
  });
503
506
  }
504
507
  else {
505
- return this.query(stmt.query, stmt.params, this.map).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
508
+ return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
506
509
  }
507
510
  };
508
511
  CRUDRepository.prototype.exist = function (id, ctx) {
509
512
  var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
510
- var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
511
- if (stmt.query.length === 0) {
513
+ var stmt = build_1.exist(id, this.table, this.primaryKeys, this.db.param, field);
514
+ if (!stmt.query) {
512
515
  throw new Error("cannot build query by id");
513
516
  }
514
- return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
517
+ var db = ctx ? ctx : this.db;
518
+ return db.query(stmt.query, stmt.params).then(function (res) { return (!res || res.length === 0 ? false : true); });
515
519
  };
516
520
  CRUDRepository.prototype.delete = function (id, ctx) {
517
- var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.param);
518
- if (stmt.query.length > 0) {
519
- return this.exec(stmt.query, stmt.params, ctx);
521
+ var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.db.param);
522
+ if (stmt.query) {
523
+ var db = ctx ? ctx : this.db;
524
+ return db.exec(stmt.query, stmt.params);
520
525
  }
521
526
  else {
522
- return Promise.resolve(0);
527
+ return Promise.resolve(-1);
523
528
  }
524
529
  };
525
530
  return CRUDRepository;
@@ -529,9 +534,9 @@ var SqlSearchWriter = (function (_super) {
529
534
  __extends(SqlSearchWriter, _super);
530
535
  function SqlSearchWriter(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
531
536
  var _this = _super.call(this, db.query, table, attributes, db.driver, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
537
+ _this.db = db;
532
538
  _this.attributes = attributes;
533
539
  _this.toDB = toDB;
534
- _this.exec = db.exec;
535
540
  var x = build_1.version(attributes);
536
541
  if (x) {
537
542
  _this.version = x.name;
@@ -547,8 +552,9 @@ var SqlSearchWriter = (function (_super) {
547
552
  obj2 = this.toDB(obj);
548
553
  }
549
554
  var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
550
- if (stmt.query.length > 0) {
551
- return this.exec(stmt.query, stmt.params, ctx).catch(function (err) {
555
+ if (stmt.query) {
556
+ var db = ctx ? ctx : this.db;
557
+ return db.exec(stmt.query, stmt.params).catch(function (err) {
552
558
  if (err && err.error === "duplicate") {
553
559
  return 0;
554
560
  }
@@ -558,7 +564,7 @@ var SqlSearchWriter = (function (_super) {
558
564
  });
559
565
  }
560
566
  else {
561
- return Promise.resolve(0);
567
+ return Promise.resolve(-1);
562
568
  }
563
569
  };
564
570
  SqlSearchWriter.prototype.update = function (obj, ctx) {
@@ -567,11 +573,12 @@ var SqlSearchWriter = (function (_super) {
567
573
  obj2 = this.toDB(obj);
568
574
  }
569
575
  var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
570
- if (stmt.query.length > 0) {
571
- return this.exec(stmt.query, stmt.params, ctx);
576
+ if (stmt.query) {
577
+ var db = ctx ? ctx : this.db;
578
+ return db.exec(stmt.query, stmt.params);
572
579
  }
573
580
  else {
574
- return Promise.resolve(0);
581
+ return Promise.resolve(-1);
575
582
  }
576
583
  };
577
584
  SqlSearchWriter.prototype.patch = function (obj, ctx) {
@@ -596,9 +603,10 @@ var SqlRepository = (function (_super) {
596
603
  SqlRepository.prototype.metadata = function () {
597
604
  return this.attributes;
598
605
  };
599
- SqlRepository.prototype.all = function () {
606
+ SqlRepository.prototype.all = function (ctx) {
600
607
  var sql = "select * from " + this.table;
601
- return this.query(sql, [], this.map);
608
+ var db = ctx ? ctx : this.db;
609
+ return db.query(sql, [], this.map, this.bools);
602
610
  };
603
611
  SqlRepository.prototype.load = function (id, ctx) {
604
612
  var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
@@ -606,8 +614,9 @@ var SqlRepository = (function (_super) {
606
614
  throw new Error("cannot build query by id");
607
615
  }
608
616
  var fn = this.fromDB;
617
+ var db = ctx ? ctx : this.db;
609
618
  if (fn) {
610
- return this.query(stmt.query, stmt.params, this.map, ctx).then(function (res) {
619
+ return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) {
611
620
  if (!res || res.length === 0) {
612
621
  return null;
613
622
  }
@@ -618,7 +627,7 @@ var SqlRepository = (function (_super) {
618
627
  });
619
628
  }
620
629
  else {
621
- return this.query(stmt.query, stmt.params, this.map).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
630
+ return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
622
631
  }
623
632
  };
624
633
  SqlRepository.prototype.exist = function (id, ctx) {
@@ -627,15 +636,17 @@ var SqlRepository = (function (_super) {
627
636
  if (!stmt) {
628
637
  throw new Error("cannot build query by id");
629
638
  }
630
- return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
639
+ var db = ctx ? ctx : this.db;
640
+ return db.query(stmt.query, stmt.params).then(function (res) { return (!res || res.length === 0 ? false : true); });
631
641
  };
632
642
  SqlRepository.prototype.delete = function (id, ctx) {
633
643
  var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.param);
634
- if (stmt) {
635
- return this.exec(stmt.query, stmt.params, ctx);
644
+ if (stmt.query) {
645
+ var db = ctx ? ctx : this.db;
646
+ return db.exec(stmt.query, stmt.params);
636
647
  }
637
648
  else {
638
- return Promise.resolve(0);
649
+ return Promise.resolve(-1);
639
650
  }
640
651
  };
641
652
  return SqlRepository;
@@ -660,18 +671,18 @@ var Query = (function (_super) {
660
671
  Query.prototype.metadata = function () {
661
672
  return this.attrs;
662
673
  };
663
- Query.prototype.all = function () {
674
+ Query.prototype.all = function (ctx) {
664
675
  var sql = "select * from " + this.table;
665
- return this.query(sql, [], this.map);
676
+ return this.query(sql, [], this.map, this.bools, ctx);
666
677
  };
667
678
  Query.prototype.load = function (id, ctx) {
668
679
  var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
669
- if (!stmt) {
680
+ if (!stmt.query) {
670
681
  throw new Error("cannot build query by id");
671
682
  }
672
683
  var fn = this.fromDB;
673
684
  if (fn) {
674
- return this.query(stmt.query, stmt.params, this.map, ctx).then(function (res) {
685
+ return this.query(stmt.query, stmt.params, this.map, this.bools, ctx).then(function (res) {
675
686
  if (!res || res.length === 0) {
676
687
  return null;
677
688
  }
@@ -682,16 +693,16 @@ var Query = (function (_super) {
682
693
  });
683
694
  }
684
695
  else {
685
- return this.query(stmt.query, stmt.params, this.map).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
696
+ return this.query(stmt.query, stmt.params, this.map, this.bools, ctx).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
686
697
  }
687
698
  };
688
699
  Query.prototype.exist = function (id, ctx) {
689
700
  var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
690
701
  var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
691
- if (!stmt) {
702
+ if (stmt.query) {
692
703
  throw new Error("cannot build query by id");
693
704
  }
694
- return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
705
+ return this.query(stmt.query, stmt.params, undefined, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
695
706
  };
696
707
  return Query;
697
708
  }(SearchBuilder_1.SearchBuilder));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/batch.ts CHANGED
@@ -26,7 +26,7 @@ export class SqlInserter<T> {
26
26
  obj2 = this.map(obj)
27
27
  }
28
28
  const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version)
29
- if (stmt.query.length > 0) {
29
+ if (stmt.query.length) {
30
30
  if (this.oneIfSuccess) {
31
31
  return this.exec(stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0))
32
32
  } else {
@@ -63,7 +63,7 @@ export class SqlUpdater<T> {
63
63
  obj2 = this.map(obj)
64
64
  }
65
65
  const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version)
66
- if (stmt.query.length > 0) {
66
+ if (stmt.query.length) {
67
67
  if (this.oneIfSuccess) {
68
68
  return this.exec(stmt.query, stmt.params).then((ct) => (ct > 0 ? 1 : 0))
69
69
  } else {
@@ -104,7 +104,7 @@ export class SqlBatchInserter<T> {
104
104
  }
105
105
  }
106
106
  const stmt = buildToInsertBatch(list, this.table, this.attributes, this.param, this.version)
107
- if (stmt.query.length > 0) {
107
+ if (stmt.query.length) {
108
108
  if (this.oneIfSuccess) {
109
109
  return this.exec(stmt.query, stmt.params).then((ct) => objs.length)
110
110
  } else {
@@ -205,7 +205,7 @@ export class StreamInserter<T> {
205
205
  } else {
206
206
  const total = this.list.length
207
207
  const stmt = buildToInsertBatch(this.list, this.table, this.attributes, this.param, this.version)
208
- if (stmt.query.length > 0) {
208
+ if (stmt.query.length) {
209
209
  return this.exec(stmt.query, stmt.params).then((r) => {
210
210
  this.list = []
211
211
  return total
package/src/build.ts CHANGED
@@ -115,7 +115,7 @@ export function insert<T>(
115
115
  i?: number,
116
116
  ): Promise<number> {
117
117
  const stm = buildToInsert(obj, table, attrs, buildParam, ver, i)
118
- if (stm.query.length === 0) {
118
+ if (!stm.query) {
119
119
  return Promise.resolve(0)
120
120
  } else {
121
121
  return exec(stm.query, stm.params)
@@ -203,7 +203,7 @@ export function insertBatch<T>(
203
203
  i?: number,
204
204
  ): Promise<number> {
205
205
  const stm = buildToInsertBatch(objs, table, attrs, buildParam, ver, i)
206
- if (stm.query.length === 0) {
206
+ if (!stm.query) {
207
207
  return Promise.resolve(0)
208
208
  } else {
209
209
  return exec(stm.query, stm.params)
@@ -373,7 +373,7 @@ export function update<T>(
373
373
  i?: number,
374
374
  ): Promise<number> {
375
375
  const stm = buildToUpdate(obj, table, attrs, buildParam, ver, i)
376
- if (stm.query.length === 0) {
376
+ if (!stm.query) {
377
377
  return Promise.resolve(0)
378
378
  } else {
379
379
  return exec(stm.query, stm.params)
package/src/client.ts CHANGED
@@ -153,7 +153,7 @@ export class ProxyClient {
153
153
 
154
154
  insert<T>(table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number> {
155
155
  const s = buildToInsert(obj, table, attrs, buildParam, ver)
156
- if (s.query.length > 0) {
156
+ if (s.query) {
157
157
  return this.exec(s.query, s.params)
158
158
  } else {
159
159
  return Promise.resolve(-1)
@@ -161,7 +161,7 @@ export class ProxyClient {
161
161
  }
162
162
  update<T>(table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number> {
163
163
  const s = buildToUpdate(obj, table, attrs, buildParam, ver)
164
- if (s.query.length > 0) {
164
+ if (s.query) {
165
165
  return this.exec(s.query, s.params)
166
166
  } else {
167
167
  return Promise.resolve(-1)
@@ -169,7 +169,7 @@ export class ProxyClient {
169
169
  }
170
170
  insertBatch<T>(table: string, attrs: Attributes, objs: T[], buildParam: (i: number) => string, driver?: string): Promise<number> {
171
171
  const s = buildToInsertBatch(objs, table, attrs, buildParam)
172
- if (s.query.length > 0) {
172
+ if (s.query) {
173
173
  return this.exec(s.query, s.params)
174
174
  } else {
175
175
  return Promise.resolve(-1)
@@ -185,7 +185,7 @@ export class ProxyClient {
185
185
  }
186
186
  insertWithTx<T>(tx: string, commit: boolean, table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number> {
187
187
  const s = buildToInsert(obj, table, attrs, buildParam, ver)
188
- if (s.query.length > 0) {
188
+ if (s.query) {
189
189
  return this.execWithTx(tx, commit, s.query, s.params)
190
190
  } else {
191
191
  return Promise.resolve(-1)
@@ -193,7 +193,7 @@ export class ProxyClient {
193
193
  }
194
194
  updateWithTx<T>(tx: string, commit: boolean, table: string, attrs: Attributes, obj: T, buildParam: (i: number) => string, ver?: string): Promise<number> {
195
195
  const s = buildToUpdate(obj, table, attrs, buildParam, ver)
196
- if (s.query.length > 0) {
196
+ if (s.query) {
197
197
  return this.execWithTx(tx, commit, s.query, s.params)
198
198
  } else {
199
199
  return Promise.resolve(-1)
@@ -209,7 +209,7 @@ export class ProxyClient {
209
209
  driver?: string,
210
210
  ): Promise<number> {
211
211
  const s = buildToInsertBatch(objs, table, attrs, buildParam)
212
- if (s.query.length > 0) {
212
+ if (s.query) {
213
213
  return this.execWithTx(tx, commit, s.query, s.params)
214
214
  } else {
215
215
  return Promise.resolve(-1)
package/src/index.ts CHANGED
@@ -38,14 +38,12 @@ export interface Config {
38
38
 
39
39
  // tslint:disable-next-line:max-classes-per-file
40
40
  export class Loader<T> {
41
- map?: StringMap
42
41
  constructor(
43
- public query: (sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<T[]>,
44
- public sql: string,
45
- m?: StringMap,
46
- public bools?: Attribute[],
42
+ protected query: (sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<T[]>,
43
+ protected sql: string,
44
+ protected map?: StringMap,
45
+ protected bools?: Attribute[],
47
46
  ) {
48
- this.map = m
49
47
  this.load = this.load.bind(this)
50
48
  }
51
49
  load(): Promise<T[]> {
package/src/services.ts CHANGED
@@ -13,12 +13,12 @@ export type Load<T, ID> = (id: ID, ctx?: any) => Promise<T | null>
13
13
  export type Get<T, ID> = Load<T, ID>
14
14
  export function useGet<T, ID>(
15
15
  q: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
16
+ param: (i: number) => string,
16
17
  table: string,
17
18
  attrs: Attributes | string[],
18
- param: (i: number) => string,
19
19
  fromDB?: (v: T) => T,
20
20
  ): Load<T, ID> {
21
- const l = new SqlLoader<T, ID>(q, table, attrs, param, fromDB)
21
+ const l = new SqlLoader<T, ID>(q, param, table, attrs, fromDB)
22
22
  return l.load
23
23
  }
24
24
  export const useLoad = useGet
@@ -29,9 +29,9 @@ export class SqlLoader<T, ID> {
29
29
  bools?: Attribute[]
30
30
  constructor(
31
31
  protected query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
32
+ protected param: (i: number) => string,
32
33
  protected table: string,
33
34
  attrs: Attributes | string[],
34
- protected param: (i: number) => string,
35
35
  protected fromDB?: (v: T) => T,
36
36
  ) {
37
37
  if (Array.isArray(attrs)) {
@@ -54,18 +54,18 @@ export class SqlLoader<T, ID> {
54
54
  metadata?(): Attributes | undefined {
55
55
  return this.attributes
56
56
  }
57
- all(): Promise<T[]> {
57
+ all(ctx?: any): Promise<T[]> {
58
58
  const sql = `select * from ${this.table}`
59
- return this.query(sql, [], this.map)
59
+ return this.query(sql, [], this.map, this.bools, ctx)
60
60
  }
61
61
  load(id: ID, ctx?: any): Promise<T | null> {
62
62
  const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
63
- if (!stmt) {
63
+ if (!stmt.query) {
64
64
  throw new Error("cannot build query by id")
65
65
  }
66
66
  const fn = this.fromDB
67
67
  if (fn) {
68
- return this.query<T>(stmt.query, stmt.params, this.map, ctx).then((res) => {
68
+ return this.query<T>(stmt.query, stmt.params, this.map, this.bools, ctx).then((res) => {
69
69
  if (!res || res.length === 0) {
70
70
  return null
71
71
  } else {
@@ -74,21 +74,21 @@ export class SqlLoader<T, ID> {
74
74
  }
75
75
  })
76
76
  } else {
77
- return this.query<T>(stmt.query, stmt.params, this.map).then((res) => (!res || res.length === 0 ? null : res[0]))
77
+ return this.query<T>(stmt.query, stmt.params, this.map, this.bools, ctx).then((res) => (!res || res.length === 0 ? null : res[0]))
78
78
  }
79
79
  }
80
80
  exist(id: ID, ctx?: any): Promise<boolean> {
81
81
  const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
82
82
  const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
83
- if (!stmt) {
83
+ if (!stmt.query) {
84
84
  throw new Error("cannot build query by id")
85
85
  }
86
- return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then((res) => (!res || res.length === 0 ? false : true))
86
+ return this.query(stmt.query, stmt.params, undefined, undefined, ctx).then((res) => (!res || res.length === 0 ? false : true))
87
87
  }
88
88
  }
89
89
  // tslint:disable-next-line:max-classes-per-file
90
90
  export class QueryRepository<T, ID> {
91
- constructor(public db: DB, public table: string, public attrs: Attributes, public sort?: string, id?: string) {
91
+ constructor(protected db: Executor, protected table: string, protected attrs: Attributes, protected sort?: string, id?: string) {
92
92
  this.id = id && id.length > 0 ? id : "id"
93
93
  this.query = this.query.bind(this)
94
94
  const m = metadata(attrs)
@@ -98,7 +98,7 @@ export class QueryRepository<T, ID> {
98
98
  id: string
99
99
  map?: StringMap
100
100
  bools?: Attribute[]
101
- query(ids: ID[]): Promise<T[]> {
101
+ query(ids: ID[], ctx?: Transaction): Promise<T[]> {
102
102
  if (!ids || ids.length === 0) {
103
103
  return Promise.resolve([])
104
104
  }
@@ -111,18 +111,27 @@ export class QueryRepository<T, ID> {
111
111
  if (this.sort && this.sort.length > 0) {
112
112
  sql = sql + " order by " + this.sort
113
113
  }
114
- return this.db.query<T>(sql, ids, this.map, this.bools)
114
+ const db = ctx ? ctx : this.db
115
+ return db.query<T>(sql, ids, this.map, this.bools)
115
116
  }
116
117
  }
117
118
 
118
- export interface DB {
119
+ export interface Executor {
119
120
  driver: string
120
121
  param(i: number): string
121
122
  exec(sql: string, args?: any[], ctx?: any): Promise<number>
122
123
  execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>
123
124
  query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
124
125
  }
125
- export type Manager = DB
126
+ export interface Transaction extends Executor {
127
+ commit(): Promise<void>
128
+ rollback(): Promise<void>
129
+ end(): Promise<void>
130
+ }
131
+ export interface DB extends Executor {
132
+ beginTransaction(): Promise<Transaction>
133
+ }
134
+ // export type Manager = DB
126
135
  export interface FullDB {
127
136
  driver: string
128
137
  param(i: number): string
@@ -133,7 +142,7 @@ export interface FullDB {
133
142
  execScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T>
134
143
  count(sql: string, args?: any[], ctx?: any): Promise<number>
135
144
  }
136
- export type ExtManager = FullDB
145
+ // export type ExtManager = FullDB
137
146
  export interface SimpleMap {
138
147
  [key: string]: string | number | boolean | Date
139
148
  }
@@ -145,7 +154,7 @@ export interface Logger {
145
154
  isDebugEnabled(): boolean
146
155
  isInfoEnabled(): boolean
147
156
  }
148
- export function log(db: ExtManager, isLog: boolean | undefined | null, logger: Logger, q?: string, result?: string, r?: string, duration?: string): ExtManager {
157
+ export function log(db: FullDB, isLog: boolean | undefined | null, logger: Logger, q?: string, result?: string, r?: string, duration?: string): FullDB {
149
158
  if (!isLog) {
150
159
  return db
151
160
  }
@@ -161,7 +170,7 @@ export function log(db: ExtManager, isLog: boolean | undefined | null, logger: L
161
170
  return new LogManager(db, logger.error, logger.info, q, result, r, duration)
162
171
  }
163
172
  export function useLog(
164
- db: ExtManager,
173
+ db: FullDB,
165
174
  isLog: boolean | undefined | null,
166
175
  err: ((msg: string, m?: SimpleMap) => void) | undefined,
167
176
  lg?: (msg: string, m?: SimpleMap) => void,
@@ -169,7 +178,7 @@ export function useLog(
169
178
  result?: string,
170
179
  r?: string,
171
180
  duration?: string,
172
- ): ExtManager {
181
+ ): FullDB {
173
182
  if (!isLog) {
174
183
  return db
175
184
  }
@@ -179,9 +188,9 @@ export function useLog(
179
188
  return db
180
189
  }
181
190
  // tslint:disable-next-line:max-classes-per-file
182
- export class LogManager implements ExtManager {
191
+ export class LogManager implements FullDB {
183
192
  constructor(
184
- public db: ExtManager,
193
+ public db: FullDB,
185
194
  err: (msg: string, m?: SimpleMap) => void,
186
195
  lg?: (msg: string, m?: SimpleMap) => void,
187
196
  q?: string,
@@ -461,7 +470,7 @@ const getDurationInMilliseconds = (start: [number, number] | undefined) => {
461
470
  // tslint:disable-next-line:max-classes-per-file
462
471
  export class SqlWriter<T> {
463
472
  protected version?: string
464
- constructor(protected exec: (sql: string, args?: any[], ctx?: any) => Promise<number>, protected param: (i: number) => string, protected table: string, protected attributes: Attributes, public toDB?: (v: T) => T) {
473
+ constructor(protected db: Executor, protected table: string, protected attributes: Attributes, protected toDB?: (v: T) => T) {
465
474
  const x = version(attributes)
466
475
  if (x) {
467
476
  this.version = x.name
@@ -470,14 +479,15 @@ export class SqlWriter<T> {
470
479
  this.update = this.update.bind(this)
471
480
  this.patch = this.patch.bind(this)
472
481
  }
473
- create(obj: T, ctx?: any): Promise<number> {
482
+ create(obj: T, ctx?: Transaction): Promise<number> {
474
483
  let obj2 = obj
475
484
  if (this.toDB) {
476
485
  obj2 = this.toDB(obj)
477
486
  }
478
- const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version)
479
- if (stmt.query.length > 0) {
480
- return this.exec(stmt.query, stmt.params, ctx).catch((err) => {
487
+ const stmt = buildToInsert(obj2, this.table, this.attributes, this.db.param, this.version)
488
+ if (stmt.query) {
489
+ const db = ctx ? ctx : this.db
490
+ return db.exec(stmt.query, stmt.params).catch((err) => {
481
491
  if (err && err.error === "duplicate") {
482
492
  return 0
483
493
  } else {
@@ -485,19 +495,20 @@ export class SqlWriter<T> {
485
495
  }
486
496
  })
487
497
  } else {
488
- return Promise.resolve(0)
498
+ return Promise.resolve(-1)
489
499
  }
490
500
  }
491
- update(obj: T, ctx?: any): Promise<number> {
501
+ update(obj: T, ctx?: Transaction): Promise<number> {
492
502
  let obj2 = obj
493
503
  if (this.toDB) {
494
504
  obj2 = this.toDB(obj)
495
505
  }
496
- const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version)
497
- if (stmt.query.length > 0) {
498
- return this.exec(stmt.query, stmt.params, ctx)
506
+ const stmt = buildToUpdate(obj2, this.table, this.attributes, this.db.param, this.version)
507
+ if (stmt.query) {
508
+ const db = ctx ? ctx : this.db
509
+ return db.exec(stmt.query, stmt.params)
499
510
  } else {
500
- return Promise.resolve(0)
511
+ return Promise.resolve(-1)
501
512
  }
502
513
  }
503
514
  patch(obj: Partial<T>, ctx?: any): Promise<number> {
@@ -508,10 +519,8 @@ export class CRUDRepository<T, ID> extends SqlWriter<T> {
508
519
  protected primaryKeys: Attribute[]
509
520
  protected map?: StringMap
510
521
  protected bools?: Attribute[]
511
- protected query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>
512
- constructor(db: DB, table: string, attributes: Attributes, toDB?: (v: T) => T, protected fromDB?: (v: T) => T) {
513
- super(db.exec, db.param, table, attributes, toDB)
514
- this.query = db.query
522
+ constructor(db: Executor, table: string, attributes: Attributes, toDB?: (v: T) => T, protected fromDB?: (v: T) => T) {
523
+ super(db, table, attributes, toDB)
515
524
  const m = metadata(attributes)
516
525
  this.primaryKeys = m.keys
517
526
  this.map = m.map
@@ -525,18 +534,20 @@ export class CRUDRepository<T, ID> extends SqlWriter<T> {
525
534
  metadata(): Attributes {
526
535
  return this.attributes
527
536
  }
528
- all(): Promise<T[]> {
537
+ all(ctx?: Transaction): Promise<T[]> {
529
538
  const sql = `select * from ${this.table}`
530
- return this.query(sql, [], this.map)
539
+ const db = ctx ? ctx : this.db
540
+ return db.query(sql, [], this.map, this.bools)
531
541
  }
532
- load(id: ID, ctx?: any): Promise<T | null> {
533
- const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
534
- if (stmt.query.length === 0) {
542
+ load(id: ID, ctx?: Transaction): Promise<T | null> {
543
+ const stmt = select<ID>(id, this.table, this.primaryKeys, this.db.param)
544
+ if (!stmt.query) {
535
545
  throw new Error("cannot build query by id")
536
546
  }
537
547
  const fn = this.fromDB
548
+ const db = ctx ? ctx : this.db
538
549
  if (fn) {
539
- return this.query<T>(stmt.query, stmt.params, this.map, ctx).then((res) => {
550
+ return db.query<T>(stmt.query, stmt.params, this.map, this.bools).then((res) => {
540
551
  if (!res || res.length === 0) {
541
552
  return null
542
553
  } else {
@@ -545,32 +556,33 @@ export class CRUDRepository<T, ID> extends SqlWriter<T> {
545
556
  }
546
557
  })
547
558
  } else {
548
- return this.query<T>(stmt.query, stmt.params, this.map).then((res) => (!res || res.length === 0 ? null : res[0]))
559
+ return db.query<T>(stmt.query, stmt.params, this.map, this.bools).then((res) => (!res || res.length === 0 ? null : res[0]))
549
560
  }
550
561
  }
551
- exist(id: ID, ctx?: any): Promise<boolean> {
562
+ exist(id: ID, ctx?: Transaction): Promise<boolean> {
552
563
  const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
553
- const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
554
- if (stmt.query.length === 0) {
564
+ const stmt = exist<ID>(id, this.table, this.primaryKeys, this.db.param, field)
565
+ if (!stmt.query) {
555
566
  throw new Error("cannot build query by id")
556
567
  }
557
- return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then((res) => (!res || res.length === 0 ? false : true))
558
- }
559
- delete(id: ID, ctx?: any): Promise<number> {
560
- const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param)
561
- if (stmt.query.length > 0) {
562
- return this.exec(stmt.query, stmt.params, ctx)
568
+ const db = ctx ? ctx : this.db
569
+ return db.query(stmt.query, stmt.params).then((res) => (!res || res.length === 0 ? false : true))
570
+ }
571
+ delete(id: ID, ctx?: Transaction): Promise<number> {
572
+ const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.db.param)
573
+ if (stmt.query) {
574
+ const db = ctx ? ctx : this.db
575
+ return db.exec(stmt.query, stmt.params)
563
576
  } else {
564
- return Promise.resolve(0)
577
+ return Promise.resolve(-1)
565
578
  }
566
579
  }
567
580
  }
568
581
 
569
582
  export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
570
583
  protected version?: string
571
- protected exec: (sql: string, args?: any[], ctx?: any) => Promise<number>
572
584
  constructor(
573
- db: DB,
585
+ protected db: Executor,
574
586
  table: string,
575
587
  protected attributes: Attributes,
576
588
  buildQ?: (
@@ -595,7 +607,6 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
595
607
  total?: string,
596
608
  ) {
597
609
  super(db.query, table, attributes, db.driver, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total)
598
- this.exec = db.exec
599
610
  const x = version(attributes)
600
611
  if (x) {
601
612
  this.version = x.name
@@ -604,14 +615,15 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
604
615
  this.update = this.update.bind(this)
605
616
  this.patch = this.patch.bind(this)
606
617
  }
607
- create(obj: T, ctx?: any): Promise<number> {
618
+ create(obj: T, ctx?: Transaction): Promise<number> {
608
619
  let obj2 = obj
609
620
  if (this.toDB) {
610
621
  obj2 = this.toDB(obj)
611
622
  }
612
623
  const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version)
613
- if (stmt.query.length > 0) {
614
- return this.exec(stmt.query, stmt.params, ctx).catch((err) => {
624
+ if (stmt.query) {
625
+ const db = ctx ? ctx : this.db
626
+ return db.exec(stmt.query, stmt.params).catch((err) => {
615
627
  if (err && err.error === "duplicate") {
616
628
  return 0
617
629
  } else {
@@ -619,29 +631,30 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
619
631
  }
620
632
  })
621
633
  } else {
622
- return Promise.resolve(0)
634
+ return Promise.resolve(-1)
623
635
  }
624
636
  }
625
- update(obj: T, ctx?: any): Promise<number> {
637
+ update(obj: T, ctx?: Transaction): Promise<number> {
626
638
  let obj2 = obj
627
639
  if (this.toDB) {
628
640
  obj2 = this.toDB(obj)
629
641
  }
630
642
  const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version)
631
- if (stmt.query.length > 0) {
632
- return this.exec(stmt.query, stmt.params, ctx)
643
+ if (stmt.query) {
644
+ const db = ctx ? ctx : this.db
645
+ return db.exec(stmt.query, stmt.params)
633
646
  } else {
634
- return Promise.resolve(0)
647
+ return Promise.resolve(-1)
635
648
  }
636
649
  }
637
- patch(obj: Partial<T>, ctx?: any): Promise<number> {
650
+ patch(obj: Partial<T>, ctx?: Transaction): Promise<number> {
638
651
  return this.update(obj as any, ctx)
639
652
  }
640
653
  }
641
654
  // tslint:disable-next-line:max-classes-per-file
642
655
  export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
643
656
  constructor(
644
- db: DB,
657
+ db: Executor,
645
658
  table: string,
646
659
  protected attributes: Attributes,
647
660
  buildQ?: (
@@ -675,18 +688,20 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
675
688
  metadata(): Attributes {
676
689
  return this.attributes
677
690
  }
678
- all(): Promise<T[]> {
691
+ all(ctx?: Transaction): Promise<T[]> {
679
692
  const sql = `select * from ${this.table}`
680
- return this.query(sql, [], this.map)
693
+ const db = ctx ? ctx : this.db
694
+ return db.query(sql, [], this.map, this.bools)
681
695
  }
682
- load(id: ID, ctx?: any): Promise<T | null> {
696
+ load(id: ID, ctx?: Transaction): Promise<T | null> {
683
697
  const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
684
698
  if (!stmt) {
685
699
  throw new Error("cannot build query by id")
686
700
  }
687
701
  const fn = this.fromDB
702
+ const db = ctx ? ctx : this.db
688
703
  if (fn) {
689
- return this.query<T>(stmt.query, stmt.params, this.map, ctx).then((res) => {
704
+ return db.query<T>(stmt.query, stmt.params, this.map, this.bools).then((res) => {
690
705
  if (!res || res.length === 0) {
691
706
  return null
692
707
  } else {
@@ -695,23 +710,25 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
695
710
  }
696
711
  })
697
712
  } else {
698
- return this.query<T>(stmt.query, stmt.params, this.map).then((res) => (!res || res.length === 0 ? null : res[0]))
713
+ return db.query<T>(stmt.query, stmt.params, this.map, this.bools).then((res) => (!res || res.length === 0 ? null : res[0]))
699
714
  }
700
715
  }
701
- exist(id: ID, ctx?: any): Promise<boolean> {
716
+ exist(id: ID, ctx?: Transaction): Promise<boolean> {
702
717
  const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
703
718
  const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
704
719
  if (!stmt) {
705
720
  throw new Error("cannot build query by id")
706
721
  }
707
- return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then((res) => (!res || res.length === 0 ? false : true))
722
+ const db = ctx ? ctx : this.db
723
+ return db.query(stmt.query, stmt.params).then((res) => (!res || res.length === 0 ? false : true))
708
724
  }
709
- delete(id: ID, ctx?: any): Promise<number> {
725
+ delete(id: ID, ctx?: Transaction): Promise<number> {
710
726
  const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param)
711
- if (stmt) {
712
- return this.exec(stmt.query, stmt.params, ctx)
727
+ if (stmt.query) {
728
+ const db = ctx ? ctx : this.db
729
+ return db.exec(stmt.query, stmt.params)
713
730
  } else {
714
- return Promise.resolve(0)
731
+ return Promise.resolve(-1)
715
732
  }
716
733
  }
717
734
  }
@@ -761,18 +778,18 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
761
778
  metadata?(): Attributes | undefined {
762
779
  return this.attrs
763
780
  }
764
- all(): Promise<T[]> {
781
+ all(ctx?: any): Promise<T[]> {
765
782
  const sql = `select * from ${this.table}`
766
- return this.query(sql, [], this.map)
783
+ return this.query(sql, [], this.map, this.bools, ctx)
767
784
  }
768
785
  load(id: ID, ctx?: any): Promise<T | null> {
769
786
  const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
770
- if (!stmt) {
787
+ if (!stmt.query) {
771
788
  throw new Error("cannot build query by id")
772
789
  }
773
790
  const fn = this.fromDB
774
791
  if (fn) {
775
- return this.query<T>(stmt.query, stmt.params, this.map, ctx).then((res) => {
792
+ return this.query<T>(stmt.query, stmt.params, this.map, this.bools, ctx).then((res) => {
776
793
  if (!res || res.length === 0) {
777
794
  return null
778
795
  } else {
@@ -781,15 +798,15 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
781
798
  }
782
799
  })
783
800
  } else {
784
- return this.query<T>(stmt.query, stmt.params, this.map).then((res) => (!res || res.length === 0 ? null : res[0]))
801
+ return this.query<T>(stmt.query, stmt.params, this.map, this.bools, ctx).then((res) => (!res || res.length === 0 ? null : res[0]))
785
802
  }
786
803
  }
787
804
  exist(id: ID, ctx?: any): Promise<boolean> {
788
805
  const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
789
806
  const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
790
- if (!stmt) {
807
+ if (stmt.query) {
791
808
  throw new Error("cannot build query by id")
792
809
  }
793
- return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then((res) => (!res || res.length === 0 ? false : true))
810
+ return this.query(stmt.query, stmt.params, undefined, undefined, ctx).then((res) => (!res || res.length === 0 ? false : true))
794
811
  }
795
812
  }