query-core 0.6.4 → 0.6.6
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/SearchBuilder.js +10 -24
- package/lib/health.js +1 -0
- package/lib/index.js +0 -12
- package/lib/services.js +55 -43
- package/package.json +1 -1
- package/src/SearchBuilder.ts +25 -29
- package/src/health.ts +1 -0
- package/src/index.ts +0 -11
- package/src/services.ts +74 -63
package/lib/SearchBuilder.js
CHANGED
|
@@ -8,11 +8,10 @@ exports.mssql = "mssql";
|
|
|
8
8
|
exports.mysql = "mysql";
|
|
9
9
|
exports.sqlite = "sqlite";
|
|
10
10
|
var SearchBuilder = (function () {
|
|
11
|
-
function SearchBuilder(
|
|
12
|
-
this.
|
|
11
|
+
function SearchBuilder(db, table, attrs, buildQ, fromDB, sort, q, excluding, buildSort, total) {
|
|
12
|
+
this.db = db;
|
|
13
13
|
this.table = table;
|
|
14
14
|
this.attrs = attrs;
|
|
15
|
-
this.provider = provider;
|
|
16
15
|
this.fromDB = fromDB;
|
|
17
16
|
this.sort = sort;
|
|
18
17
|
if (attrs) {
|
|
@@ -21,6 +20,9 @@ var SearchBuilder = (function () {
|
|
|
21
20
|
this.map = meta.map;
|
|
22
21
|
this.bools = meta.bools;
|
|
23
22
|
this.primaryKeys = meta.keys;
|
|
23
|
+
this.version = meta.version;
|
|
24
|
+
this.createdAt = meta.createdAt;
|
|
25
|
+
this.updatedAt = meta.updatedAt;
|
|
24
26
|
}
|
|
25
27
|
else {
|
|
26
28
|
this.primaryKeys = [];
|
|
@@ -30,23 +32,6 @@ var SearchBuilder = (function () {
|
|
|
30
32
|
this.q = q && q.length > 0 ? q : "q";
|
|
31
33
|
this.excluding = excluding && excluding.length > 0 ? excluding : "excluding";
|
|
32
34
|
this.search = this.search.bind(this);
|
|
33
|
-
if (buildParam) {
|
|
34
|
-
this.param = buildParam;
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
if (provider === search_1.oracle) {
|
|
38
|
-
this.param = query_1.buildOracleParam;
|
|
39
|
-
}
|
|
40
|
-
else if (provider === exports.postgres) {
|
|
41
|
-
this.param = query_1.buildDollarParam;
|
|
42
|
-
}
|
|
43
|
-
else if (provider === exports.mssql) {
|
|
44
|
-
this.param = query_1.buildMsSQLParam;
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
this.param = build_1.param;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
35
|
this.total = total && total.length > 0 ? total : "total";
|
|
51
36
|
}
|
|
52
37
|
SearchBuilder.prototype.search = function (filter, limit, page, fields) {
|
|
@@ -56,14 +41,14 @@ var SearchBuilder = (function () {
|
|
|
56
41
|
}
|
|
57
42
|
var st = this.sort ? this.sort : "sort";
|
|
58
43
|
var sn = filter[st];
|
|
59
|
-
var likeType = this.
|
|
60
|
-
var q2 = this.buildQuery(filter, this.param, sn, this.buildSort, this.attrs, this.table, fields, this.q, this.excluding, likeType);
|
|
44
|
+
var likeType = this.db.driver === exports.postgres ? "ilike" : "like";
|
|
45
|
+
var q2 = this.buildQuery(filter, this.db.param, sn, this.buildSort, this.attrs, this.table, fields, this.q, this.excluding, likeType);
|
|
61
46
|
if (!q2) {
|
|
62
47
|
throw new Error("Cannot build query");
|
|
63
48
|
}
|
|
64
49
|
var fn = this.fromDB;
|
|
65
50
|
if (fn) {
|
|
66
|
-
return search_1.buildFromQuery(this.query, q2.query, q2.params, limit, ipage, this.map, this.bools, this.
|
|
51
|
+
return search_1.buildFromQuery(this.db.query, q2.query, q2.params, limit, ipage, this.map, this.bools, this.db.driver, this.total).then(function (r) {
|
|
67
52
|
if (r.list && r.list.length > 0) {
|
|
68
53
|
r.list = r.list.map(function (o) { return fn(o); });
|
|
69
54
|
return r;
|
|
@@ -74,9 +59,10 @@ var SearchBuilder = (function () {
|
|
|
74
59
|
});
|
|
75
60
|
}
|
|
76
61
|
else {
|
|
77
|
-
return search_1.buildFromQuery(this.query, q2.query, q2.params, limit, ipage, this.map, this.bools, this.
|
|
62
|
+
return search_1.buildFromQuery(this.db.query, q2.query, q2.params, limit, ipage, this.map, this.bools, this.db.driver, this.total);
|
|
78
63
|
}
|
|
79
64
|
};
|
|
80
65
|
return SearchBuilder;
|
|
81
66
|
}());
|
|
82
67
|
exports.SearchBuilder = SearchBuilder;
|
|
68
|
+
exports.SearchRepository = SearchBuilder;
|
package/lib/health.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -3,19 +3,7 @@ function __export(m) {
|
|
|
3
3
|
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
|
4
4
|
}
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
var health_1 = require("./health");
|
|
7
|
-
exports.SqlChecker = health_1.Checker;
|
|
8
6
|
var build_1 = require("./build");
|
|
9
|
-
var services_1 = require("./services");
|
|
10
|
-
exports.SqlViewRepository = services_1.SqlLoader;
|
|
11
|
-
exports.SqlLoadService = services_1.SqlLoader;
|
|
12
|
-
exports.SqlViewService = services_1.SqlLoader;
|
|
13
|
-
exports.SqlGenericService = services_1.SqlWriter;
|
|
14
|
-
exports.GenericRepository = services_1.CRUDRepository;
|
|
15
|
-
exports.SqlGenericRepository = services_1.CRUDRepository;
|
|
16
|
-
exports.Repository = services_1.SqlRepository;
|
|
17
|
-
var SearchBuilder_1 = require("./SearchBuilder");
|
|
18
|
-
exports.SearchRepository = SearchBuilder_1.SearchBuilder;
|
|
19
7
|
__export(require("./batch"));
|
|
20
8
|
__export(require("./build"));
|
|
21
9
|
__export(require("./client"));
|
package/lib/services.js
CHANGED
|
@@ -86,6 +86,9 @@ var SqlLoader = (function () {
|
|
|
86
86
|
return SqlLoader;
|
|
87
87
|
}());
|
|
88
88
|
exports.SqlLoader = SqlLoader;
|
|
89
|
+
exports.SqlViewRepository = SqlLoader;
|
|
90
|
+
exports.SqlLoadService = SqlLoader;
|
|
91
|
+
exports.SqlViewServic = SqlLoader;
|
|
89
92
|
var QueryRepository = (function () {
|
|
90
93
|
function QueryRepository(db, table, attrs, sort, id) {
|
|
91
94
|
this.db = db;
|
|
@@ -154,21 +157,21 @@ var LogManager = (function () {
|
|
|
154
157
|
this.log = lg;
|
|
155
158
|
this.error = err;
|
|
156
159
|
this.param = this.param.bind(this);
|
|
157
|
-
this.
|
|
158
|
-
this.
|
|
160
|
+
this.execute = this.execute.bind(this);
|
|
161
|
+
this.executeBatch = this.executeBatch.bind(this);
|
|
159
162
|
this.query = this.query.bind(this);
|
|
160
163
|
this.queryOne = this.queryOne.bind(this);
|
|
161
|
-
this.
|
|
164
|
+
this.executeScalar = this.executeScalar.bind(this);
|
|
162
165
|
this.count = this.count.bind(this);
|
|
163
166
|
}
|
|
164
167
|
LogManager.prototype.param = function (i) {
|
|
165
168
|
return this.db.param(i);
|
|
166
169
|
};
|
|
167
|
-
LogManager.prototype.
|
|
170
|
+
LogManager.prototype.execute = function (sql, args, ctx) {
|
|
168
171
|
var _this = this;
|
|
169
172
|
var t1 = new Date();
|
|
170
173
|
return this.db
|
|
171
|
-
.
|
|
174
|
+
.execute(sql, args, ctx)
|
|
172
175
|
.then(function (v) {
|
|
173
176
|
setTimeout(function () {
|
|
174
177
|
if (_this.log) {
|
|
@@ -199,11 +202,11 @@ var LogManager = (function () {
|
|
|
199
202
|
throw er;
|
|
200
203
|
});
|
|
201
204
|
};
|
|
202
|
-
LogManager.prototype.
|
|
205
|
+
LogManager.prototype.executeBatch = function (statements, firstSuccess, ctx) {
|
|
203
206
|
var _this = this;
|
|
204
207
|
var t1 = new Date();
|
|
205
208
|
return this.db
|
|
206
|
-
.
|
|
209
|
+
.executeBatch(statements, firstSuccess, ctx)
|
|
207
210
|
.then(function (v) {
|
|
208
211
|
setTimeout(function () {
|
|
209
212
|
if (_this.log) {
|
|
@@ -312,11 +315,11 @@ var LogManager = (function () {
|
|
|
312
315
|
throw er;
|
|
313
316
|
});
|
|
314
317
|
};
|
|
315
|
-
LogManager.prototype.
|
|
318
|
+
LogManager.prototype.executeScalar = function (sql, args, ctx) {
|
|
316
319
|
var _this = this;
|
|
317
320
|
var t1 = new Date();
|
|
318
321
|
return this.db
|
|
319
|
-
.
|
|
322
|
+
.executeScalar(sql, args, ctx)
|
|
320
323
|
.then(function (v) {
|
|
321
324
|
setTimeout(function () {
|
|
322
325
|
if (_this.log) {
|
|
@@ -416,11 +419,12 @@ var SqlWriter = (function () {
|
|
|
416
419
|
this.attributes = attributes;
|
|
417
420
|
this.toDB = toDB;
|
|
418
421
|
var x = build_1.buildMetadata(attributes);
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
422
|
+
this.primaryKeys = x.keys;
|
|
423
|
+
this.map = x.map;
|
|
424
|
+
this.bools = x.bools;
|
|
425
|
+
this.version = x.version;
|
|
426
|
+
this.createdAt = x.createdAt;
|
|
427
|
+
this.updatedAt = x.updatedAt;
|
|
424
428
|
this.create = this.create.bind(this);
|
|
425
429
|
this.update = this.update.bind(this);
|
|
426
430
|
this.patch = this.patch.bind(this);
|
|
@@ -457,9 +461,6 @@ var SqlWriter = (function () {
|
|
|
457
461
|
if (this.toDB) {
|
|
458
462
|
obj2 = this.toDB(obj);
|
|
459
463
|
}
|
|
460
|
-
if (this.createdAt) {
|
|
461
|
-
obj2[this.createdAt] = new Date();
|
|
462
|
-
}
|
|
463
464
|
if (this.updatedAt) {
|
|
464
465
|
obj2[this.updatedAt] = new Date();
|
|
465
466
|
}
|
|
@@ -483,10 +484,6 @@ var CRUDRepository = (function (_super) {
|
|
|
483
484
|
function CRUDRepository(db, table, attributes, toDB, fromDB) {
|
|
484
485
|
var _this = _super.call(this, db, table, attributes, toDB) || this;
|
|
485
486
|
_this.fromDB = fromDB;
|
|
486
|
-
var m = build_1.buildMetadata(attributes);
|
|
487
|
-
_this.primaryKeys = m.keys;
|
|
488
|
-
_this.map = m.map;
|
|
489
|
-
_this.bools = m.bools;
|
|
490
487
|
_this.metadata = _this.metadata.bind(_this);
|
|
491
488
|
_this.all = _this.all.bind(_this);
|
|
492
489
|
_this.load = _this.load.bind(_this);
|
|
@@ -546,16 +543,18 @@ var CRUDRepository = (function (_super) {
|
|
|
546
543
|
return CRUDRepository;
|
|
547
544
|
}(SqlWriter));
|
|
548
545
|
exports.CRUDRepository = CRUDRepository;
|
|
546
|
+
exports.GenericRepository = CRUDRepository;
|
|
547
|
+
exports.SqlGenericRepository = CRUDRepository;
|
|
549
548
|
var SqlSearchWriter = (function (_super) {
|
|
550
549
|
__extends(SqlSearchWriter, _super);
|
|
551
|
-
function SqlSearchWriter(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort,
|
|
552
|
-
var _this = _super.call(this, db
|
|
550
|
+
function SqlSearchWriter(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, total) {
|
|
551
|
+
var _this = _super.call(this, db, table, attributes, buildQ, fromDB, sort, q, excluding, buildSort, total) || this;
|
|
553
552
|
_this.db = db;
|
|
554
553
|
_this.attributes = attributes;
|
|
555
554
|
_this.toDB = toDB;
|
|
556
|
-
var x = build_1.
|
|
555
|
+
var x = build_1.buildMetadata(attributes);
|
|
557
556
|
if (x) {
|
|
558
|
-
_this.version = x.
|
|
557
|
+
_this.version = x.version;
|
|
559
558
|
}
|
|
560
559
|
_this.create = _this.create.bind(_this);
|
|
561
560
|
_this.update = _this.update.bind(_this);
|
|
@@ -567,7 +566,13 @@ var SqlSearchWriter = (function (_super) {
|
|
|
567
566
|
if (this.toDB) {
|
|
568
567
|
obj2 = this.toDB(obj);
|
|
569
568
|
}
|
|
570
|
-
|
|
569
|
+
if (this.createdAt) {
|
|
570
|
+
obj2[this.createdAt] = new Date();
|
|
571
|
+
}
|
|
572
|
+
if (this.updatedAt) {
|
|
573
|
+
obj2[this.updatedAt] = new Date();
|
|
574
|
+
}
|
|
575
|
+
var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.db.param, this.version);
|
|
571
576
|
if (stmt.query) {
|
|
572
577
|
var db = tx ? tx : this.db;
|
|
573
578
|
return db.execute(stmt.query, stmt.params).catch(function (err) {
|
|
@@ -588,7 +593,10 @@ var SqlSearchWriter = (function (_super) {
|
|
|
588
593
|
if (this.toDB) {
|
|
589
594
|
obj2 = this.toDB(obj);
|
|
590
595
|
}
|
|
591
|
-
|
|
596
|
+
if (this.updatedAt) {
|
|
597
|
+
obj2[this.updatedAt] = new Date();
|
|
598
|
+
}
|
|
599
|
+
var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.db.param, this.version);
|
|
592
600
|
if (stmt.query) {
|
|
593
601
|
var db = tx ? tx : this.db;
|
|
594
602
|
return db.execute(stmt.query, stmt.params);
|
|
@@ -605,8 +613,8 @@ var SqlSearchWriter = (function (_super) {
|
|
|
605
613
|
exports.SqlSearchWriter = SqlSearchWriter;
|
|
606
614
|
var SqlRepository = (function (_super) {
|
|
607
615
|
__extends(SqlRepository, _super);
|
|
608
|
-
function SqlRepository(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort,
|
|
609
|
-
var _this = _super.call(this, db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort,
|
|
616
|
+
function SqlRepository(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, total) {
|
|
617
|
+
var _this = _super.call(this, db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, total) || this;
|
|
610
618
|
_this.attributes = attributes;
|
|
611
619
|
_this.toDB = toDB;
|
|
612
620
|
_this.metadata = _this.metadata.bind(_this);
|
|
@@ -625,7 +633,7 @@ var SqlRepository = (function (_super) {
|
|
|
625
633
|
return db.query(sql, [], this.map, this.bools);
|
|
626
634
|
};
|
|
627
635
|
SqlRepository.prototype.load = function (id, tx) {
|
|
628
|
-
var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
|
|
636
|
+
var stmt = build_1.select(id, this.table, this.primaryKeys, this.db.param);
|
|
629
637
|
if (!stmt.query) {
|
|
630
638
|
throw new Error("cannot build query by id");
|
|
631
639
|
}
|
|
@@ -648,7 +656,7 @@ var SqlRepository = (function (_super) {
|
|
|
648
656
|
};
|
|
649
657
|
SqlRepository.prototype.exist = function (id, tx) {
|
|
650
658
|
var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
|
|
651
|
-
var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
|
|
659
|
+
var stmt = build_1.exist(id, this.table, this.primaryKeys, this.db.param, field);
|
|
652
660
|
if (!stmt.query) {
|
|
653
661
|
throw new Error("cannot build query by id");
|
|
654
662
|
}
|
|
@@ -656,7 +664,7 @@ var SqlRepository = (function (_super) {
|
|
|
656
664
|
return db.query(stmt.query, stmt.params).then(function (res) { return (!res || res.length === 0 ? false : true); });
|
|
657
665
|
};
|
|
658
666
|
SqlRepository.prototype.delete = function (id, tx) {
|
|
659
|
-
var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.param);
|
|
667
|
+
var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.db.param);
|
|
660
668
|
if (stmt.query) {
|
|
661
669
|
var db = tx ? tx : this.db;
|
|
662
670
|
return db.execute(stmt.query, stmt.params);
|
|
@@ -668,10 +676,11 @@ var SqlRepository = (function (_super) {
|
|
|
668
676
|
return SqlRepository;
|
|
669
677
|
}(SqlSearchWriter));
|
|
670
678
|
exports.SqlRepository = SqlRepository;
|
|
679
|
+
exports.Repository = SqlRepository;
|
|
671
680
|
var Query = (function (_super) {
|
|
672
681
|
__extends(Query, _super);
|
|
673
|
-
function Query(
|
|
674
|
-
var _this = _super.call(this,
|
|
682
|
+
function Query(db, table, attributes, buildQ, fromDB, sort, q, excluding, buildSort, total) {
|
|
683
|
+
var _this = _super.call(this, db, table, attributes, buildQ, fromDB, sort, q, excluding, buildSort, total) || this;
|
|
675
684
|
var m = build_1.buildMetadata(attributes);
|
|
676
685
|
_this.primaryKeys = m.keys;
|
|
677
686
|
_this.map = m.map;
|
|
@@ -687,18 +696,20 @@ var Query = (function (_super) {
|
|
|
687
696
|
Query.prototype.metadata = function () {
|
|
688
697
|
return this.attrs;
|
|
689
698
|
};
|
|
690
|
-
Query.prototype.all = function (
|
|
699
|
+
Query.prototype.all = function (tx) {
|
|
691
700
|
var sql = "select * from " + this.table;
|
|
692
|
-
|
|
701
|
+
var db = tx ? tx : this.db;
|
|
702
|
+
return db.query(sql, [], this.map, this.bools);
|
|
693
703
|
};
|
|
694
|
-
Query.prototype.load = function (id,
|
|
695
|
-
var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
|
|
704
|
+
Query.prototype.load = function (id, tx) {
|
|
705
|
+
var stmt = build_1.select(id, this.table, this.primaryKeys, this.db.param);
|
|
696
706
|
if (!stmt.query) {
|
|
697
707
|
throw new Error("cannot build query by id");
|
|
698
708
|
}
|
|
709
|
+
var db = tx ? tx : this.db;
|
|
699
710
|
var fn = this.fromDB;
|
|
700
711
|
if (fn) {
|
|
701
|
-
return
|
|
712
|
+
return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) {
|
|
702
713
|
if (!res || res.length === 0) {
|
|
703
714
|
return null;
|
|
704
715
|
}
|
|
@@ -709,16 +720,17 @@ var Query = (function (_super) {
|
|
|
709
720
|
});
|
|
710
721
|
}
|
|
711
722
|
else {
|
|
712
|
-
return this.query(stmt.query, stmt.params, this.map, this.bools
|
|
723
|
+
return this.db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
|
|
713
724
|
}
|
|
714
725
|
};
|
|
715
|
-
Query.prototype.exist = function (id,
|
|
726
|
+
Query.prototype.exist = function (id, tx) {
|
|
716
727
|
var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
|
|
717
|
-
var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
|
|
728
|
+
var stmt = build_1.exist(id, this.table, this.primaryKeys, this.db.param, field);
|
|
718
729
|
if (!stmt.query) {
|
|
719
730
|
throw new Error("cannot build query by id");
|
|
720
731
|
}
|
|
721
|
-
|
|
732
|
+
var db = tx ? tx : this.db;
|
|
733
|
+
return db.query(stmt.query, stmt.params, undefined, undefined).then(function (res) { return (!res || res.length === 0 ? false : true); });
|
|
722
734
|
};
|
|
723
735
|
return Query;
|
|
724
736
|
}(SearchBuilder_1.SearchBuilder));
|
package/package.json
CHANGED
package/src/SearchBuilder.ts
CHANGED
|
@@ -8,11 +8,19 @@ export const mssql = "mssql"
|
|
|
8
8
|
export const mysql = "mysql"
|
|
9
9
|
export const sqlite = "sqlite"
|
|
10
10
|
|
|
11
|
+
interface MinDB {
|
|
12
|
+
driver?: string
|
|
13
|
+
param(i: number): string
|
|
14
|
+
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
|
|
15
|
+
}
|
|
11
16
|
export class SearchBuilder<T, S> {
|
|
12
|
-
map?: StringMap
|
|
13
|
-
bools?: Attribute[]
|
|
14
|
-
primaryKeys: Attribute[]
|
|
15
|
-
|
|
17
|
+
protected map?: StringMap
|
|
18
|
+
protected bools?: Attribute[]
|
|
19
|
+
protected primaryKeys: Attribute[]
|
|
20
|
+
protected version?: string
|
|
21
|
+
protected createdAt?: string
|
|
22
|
+
protected updatedAt?: string
|
|
23
|
+
protected buildQuery: (
|
|
16
24
|
s: S,
|
|
17
25
|
param: (i: number) => string,
|
|
18
26
|
sort?: string,
|
|
@@ -24,16 +32,14 @@ export class SearchBuilder<T, S> {
|
|
|
24
32
|
strExcluding?: string,
|
|
25
33
|
likeType?: LikeType
|
|
26
34
|
) => Statement | undefined
|
|
27
|
-
q?: string
|
|
28
|
-
excluding?: string
|
|
29
|
-
buildSort?: (sort?: string, map?: Attributes | StringMap) => string
|
|
30
|
-
|
|
31
|
-
total?: string
|
|
35
|
+
protected q?: string
|
|
36
|
+
protected excluding?: string
|
|
37
|
+
protected buildSort?: (sort?: string, map?: Attributes | StringMap) => string
|
|
38
|
+
protected total?: string
|
|
32
39
|
constructor(
|
|
33
|
-
protected
|
|
40
|
+
protected db: MinDB,
|
|
34
41
|
protected table: string,
|
|
35
42
|
protected attrs?: Attributes,
|
|
36
|
-
protected provider?: string,
|
|
37
43
|
buildQ?: (
|
|
38
44
|
s: S,
|
|
39
45
|
param: (i: number) => string,
|
|
@@ -51,7 +57,6 @@ export class SearchBuilder<T, S> {
|
|
|
51
57
|
q?: string,
|
|
52
58
|
excluding?: string,
|
|
53
59
|
buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
|
|
54
|
-
buildParam?: (i: number) => string,
|
|
55
60
|
total?: string,
|
|
56
61
|
) {
|
|
57
62
|
if (attrs) {
|
|
@@ -60,6 +65,9 @@ export class SearchBuilder<T, S> {
|
|
|
60
65
|
this.map = meta.map
|
|
61
66
|
this.bools = meta.bools
|
|
62
67
|
this.primaryKeys = meta.keys
|
|
68
|
+
this.version = meta.version
|
|
69
|
+
this.createdAt = meta.createdAt
|
|
70
|
+
this.updatedAt = meta.updatedAt
|
|
63
71
|
} else {
|
|
64
72
|
this.primaryKeys = []
|
|
65
73
|
}
|
|
@@ -68,19 +76,6 @@ export class SearchBuilder<T, S> {
|
|
|
68
76
|
this.q = q && q.length > 0 ? q : "q"
|
|
69
77
|
this.excluding = excluding && excluding.length > 0 ? excluding : "excluding"
|
|
70
78
|
this.search = this.search.bind(this)
|
|
71
|
-
if (buildParam) {
|
|
72
|
-
this.param = buildParam
|
|
73
|
-
} else {
|
|
74
|
-
if (provider === oracle) {
|
|
75
|
-
this.param = buildOracleParam
|
|
76
|
-
} else if (provider === postgres) {
|
|
77
|
-
this.param = buildDollarParam
|
|
78
|
-
} else if (provider === mssql) {
|
|
79
|
-
this.param = buildMsSQLParam
|
|
80
|
-
} else {
|
|
81
|
-
this.param = param
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
79
|
this.total = total && total.length > 0 ? total : "total"
|
|
85
80
|
}
|
|
86
81
|
search(filter: S, limit: number, page?: number | string, fields?: string[]): Promise<SearchResult<T>> {
|
|
@@ -90,14 +85,14 @@ export class SearchBuilder<T, S> {
|
|
|
90
85
|
}
|
|
91
86
|
const st = this.sort ? this.sort : "sort"
|
|
92
87
|
const sn = (filter as any)[st] as string
|
|
93
|
-
const likeType = this.
|
|
94
|
-
const q2 = this.buildQuery(filter, this.param, sn, this.buildSort, this.attrs, this.table, fields, this.q, this.excluding, likeType)
|
|
88
|
+
const likeType = this.db.driver === postgres ? "ilike" : "like"
|
|
89
|
+
const q2 = this.buildQuery(filter, this.db.param, sn, this.buildSort, this.attrs, this.table, fields, this.q, this.excluding, likeType)
|
|
95
90
|
if (!q2) {
|
|
96
91
|
throw new Error("Cannot build query")
|
|
97
92
|
}
|
|
98
93
|
const fn = this.fromDB
|
|
99
94
|
if (fn) {
|
|
100
|
-
return buildFromQuery<T>(this.query, q2.query, q2.params, limit, ipage, this.map, this.bools, this.
|
|
95
|
+
return buildFromQuery<T>(this.db.query, q2.query, q2.params, limit, ipage, this.map, this.bools, this.db.driver, this.total).then((r) => {
|
|
101
96
|
if (r.list && r.list.length > 0) {
|
|
102
97
|
r.list = r.list.map((o) => fn(o))
|
|
103
98
|
return r
|
|
@@ -106,7 +101,8 @@ export class SearchBuilder<T, S> {
|
|
|
106
101
|
}
|
|
107
102
|
})
|
|
108
103
|
} else {
|
|
109
|
-
return buildFromQuery(this.query, q2.query, q2.params, limit, ipage, this.map, this.bools, this.
|
|
104
|
+
return buildFromQuery(this.db.query, q2.query, q2.params, limit, ipage, this.map, this.bools, this.db.driver, this.total)
|
|
110
105
|
}
|
|
111
106
|
}
|
|
112
107
|
}
|
|
108
|
+
export const SearchRepository = SearchBuilder
|
package/src/health.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,16 +1,5 @@
|
|
|
1
|
-
import { Checker } from "./health"
|
|
2
|
-
export { Checker as SqlChecker }
|
|
3
|
-
|
|
4
1
|
import { resource } from "./build"
|
|
5
2
|
import { Attribute, StringMap } from "./metadata"
|
|
6
|
-
import { SqlLoader, SqlWriter, CRUDRepository, SqlRepository } from "./services"
|
|
7
|
-
// export {SqlLoader as SqlLoadRepository};
|
|
8
|
-
export { CRUDRepository as GenericRepository, CRUDRepository as SqlGenericRepository, SqlLoader as SqlViewRepository }
|
|
9
|
-
|
|
10
|
-
export { SqlWriter as SqlGenericService, SqlLoader as SqlLoadService, SqlLoader as SqlViewService }
|
|
11
|
-
|
|
12
|
-
import { SearchBuilder } from "./SearchBuilder"
|
|
13
|
-
export { SearchBuilder as SearchRepository, SqlRepository as Repository }
|
|
14
3
|
|
|
15
4
|
export * from "./batch"
|
|
16
5
|
export * from "./build"
|
package/src/services.ts
CHANGED
|
@@ -87,6 +87,10 @@ export class SqlLoader<T, ID> {
|
|
|
87
87
|
return db.query(stmt.query, stmt.params, undefined, undefined).then((res) => (!res || res.length === 0 ? false : true))
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
|
+
export const SqlViewRepository = SqlLoader
|
|
91
|
+
export const SqlLoadService = SqlLoader
|
|
92
|
+
export const SqlViewServic = SqlLoader
|
|
93
|
+
|
|
90
94
|
// tslint:disable-next-line:max-classes-per-file
|
|
91
95
|
export class QueryRepository<T, ID> {
|
|
92
96
|
constructor(protected db: Executor, protected table: string, protected attrs: Attributes, protected sort?: string, id?: string) {
|
|
@@ -135,11 +139,11 @@ export interface DB extends Executor {
|
|
|
135
139
|
export interface FullDB {
|
|
136
140
|
driver: string
|
|
137
141
|
param(i: number): string
|
|
138
|
-
|
|
139
|
-
|
|
142
|
+
execute(sql: string, args?: any[], ctx?: any): Promise<number>
|
|
143
|
+
executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>
|
|
140
144
|
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
|
|
141
145
|
queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T | null>
|
|
142
|
-
|
|
146
|
+
executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T>
|
|
143
147
|
count(sql: string, args?: any[], ctx?: any): Promise<number>
|
|
144
148
|
}
|
|
145
149
|
// export type ExtManager = FullDB
|
|
@@ -207,11 +211,11 @@ export class LogManager implements FullDB {
|
|
|
207
211
|
this.log = lg
|
|
208
212
|
this.error = err
|
|
209
213
|
this.param = this.param.bind(this)
|
|
210
|
-
this.
|
|
211
|
-
this.
|
|
214
|
+
this.execute = this.execute.bind(this)
|
|
215
|
+
this.executeBatch = this.executeBatch.bind(this)
|
|
212
216
|
this.query = this.query.bind(this)
|
|
213
217
|
this.queryOne = this.queryOne.bind(this)
|
|
214
|
-
this.
|
|
218
|
+
this.executeScalar = this.executeScalar.bind(this)
|
|
215
219
|
this.count = this.count.bind(this)
|
|
216
220
|
}
|
|
217
221
|
log?: (msg: string, m?: SimpleMap, ctx?: any) => void
|
|
@@ -225,10 +229,10 @@ export class LogManager implements FullDB {
|
|
|
225
229
|
param(i: number): string {
|
|
226
230
|
return this.db.param(i)
|
|
227
231
|
}
|
|
228
|
-
|
|
232
|
+
execute(sql: string, args?: any[], ctx?: any): Promise<number> {
|
|
229
233
|
const t1 = new Date()
|
|
230
234
|
return this.db
|
|
231
|
-
.
|
|
235
|
+
.execute(sql, args, ctx)
|
|
232
236
|
.then((v) => {
|
|
233
237
|
setTimeout(() => {
|
|
234
238
|
if (this.log) {
|
|
@@ -259,10 +263,10 @@ export class LogManager implements FullDB {
|
|
|
259
263
|
throw er
|
|
260
264
|
})
|
|
261
265
|
}
|
|
262
|
-
|
|
266
|
+
executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
|
|
263
267
|
const t1 = new Date()
|
|
264
268
|
return this.db
|
|
265
|
-
.
|
|
269
|
+
.executeBatch(statements, firstSuccess, ctx)
|
|
266
270
|
.then((v) => {
|
|
267
271
|
setTimeout(() => {
|
|
268
272
|
if (this.log) {
|
|
@@ -369,10 +373,10 @@ export class LogManager implements FullDB {
|
|
|
369
373
|
throw er
|
|
370
374
|
})
|
|
371
375
|
}
|
|
372
|
-
|
|
376
|
+
executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T> {
|
|
373
377
|
const t1 = new Date()
|
|
374
378
|
return this.db
|
|
375
|
-
.
|
|
379
|
+
.executeScalar<T>(sql, args, ctx)
|
|
376
380
|
.then((v) => {
|
|
377
381
|
setTimeout(() => {
|
|
378
382
|
if (this.log) {
|
|
@@ -469,30 +473,35 @@ const getDurationInMilliseconds = (start: [number, number] | undefined) => {
|
|
|
469
473
|
*/
|
|
470
474
|
// tslint:disable-next-line:max-classes-per-file
|
|
471
475
|
export class SqlWriter<T> {
|
|
476
|
+
protected primaryKeys: Attribute[]
|
|
477
|
+
protected map?: StringMap
|
|
478
|
+
protected bools?: Attribute[]
|
|
472
479
|
protected version?: string
|
|
473
480
|
protected createdAt?: string
|
|
474
481
|
protected updatedAt?: string
|
|
482
|
+
|
|
475
483
|
constructor(protected db: Executor, protected table: string, protected attributes: Attributes, protected toDB?: (v: T) => T) {
|
|
476
484
|
const x = buildMetadata(attributes)
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
485
|
+
this.primaryKeys = x.keys
|
|
486
|
+
this.map = x.map
|
|
487
|
+
this.bools = x.bools
|
|
488
|
+
this.version = x.version
|
|
489
|
+
this.createdAt = x.createdAt
|
|
490
|
+
this.updatedAt = x.updatedAt
|
|
482
491
|
this.create = this.create.bind(this)
|
|
483
492
|
this.update = this.update.bind(this)
|
|
484
493
|
this.patch = this.patch.bind(this)
|
|
485
494
|
}
|
|
486
495
|
create(obj: T, tx?: Transaction): Promise<number> {
|
|
487
|
-
let obj2 = obj
|
|
496
|
+
let obj2: any = obj
|
|
488
497
|
if (this.toDB) {
|
|
489
498
|
obj2 = this.toDB(obj)
|
|
490
499
|
}
|
|
491
500
|
if (this.createdAt) {
|
|
492
|
-
|
|
501
|
+
obj2[this.createdAt] = new Date()
|
|
493
502
|
}
|
|
494
503
|
if (this.updatedAt) {
|
|
495
|
-
|
|
504
|
+
obj2[this.updatedAt] = new Date()
|
|
496
505
|
}
|
|
497
506
|
const stmt = buildToInsert(obj2, this.table, this.attributes, this.db.param, this.version)
|
|
498
507
|
if (stmt.query) {
|
|
@@ -509,15 +518,12 @@ export class SqlWriter<T> {
|
|
|
509
518
|
}
|
|
510
519
|
}
|
|
511
520
|
update(obj: T, tx?: Transaction): Promise<number> {
|
|
512
|
-
let obj2 = obj
|
|
521
|
+
let obj2: any = obj
|
|
513
522
|
if (this.toDB) {
|
|
514
523
|
obj2 = this.toDB(obj)
|
|
515
524
|
}
|
|
516
|
-
if (this.createdAt) {
|
|
517
|
-
(obj2 as any)[this.createdAt] = new Date()
|
|
518
|
-
}
|
|
519
525
|
if (this.updatedAt) {
|
|
520
|
-
|
|
526
|
+
obj2[this.updatedAt] = new Date()
|
|
521
527
|
}
|
|
522
528
|
const stmt = buildToUpdate(obj2, this.table, this.attributes, this.db.param, this.version)
|
|
523
529
|
if (stmt.query) {
|
|
@@ -532,15 +538,8 @@ export class SqlWriter<T> {
|
|
|
532
538
|
}
|
|
533
539
|
}
|
|
534
540
|
export class CRUDRepository<T, ID> extends SqlWriter<T> {
|
|
535
|
-
protected primaryKeys: Attribute[]
|
|
536
|
-
protected map?: StringMap
|
|
537
|
-
protected bools?: Attribute[]
|
|
538
541
|
constructor(db: Executor, table: string, attributes: Attributes, toDB?: (v: T) => T, protected fromDB?: (v: T) => T) {
|
|
539
542
|
super(db, table, attributes, toDB)
|
|
540
|
-
const m = buildMetadata(attributes)
|
|
541
|
-
this.primaryKeys = m.keys
|
|
542
|
-
this.map = m.map
|
|
543
|
-
this.bools = m.bools
|
|
544
543
|
this.metadata = this.metadata.bind(this)
|
|
545
544
|
this.all = this.all.bind(this)
|
|
546
545
|
this.load = this.load.bind(this)
|
|
@@ -594,9 +593,10 @@ export class CRUDRepository<T, ID> extends SqlWriter<T> {
|
|
|
594
593
|
}
|
|
595
594
|
}
|
|
596
595
|
}
|
|
596
|
+
export const GenericRepository = CRUDRepository
|
|
597
|
+
export const SqlGenericRepository = CRUDRepository
|
|
597
598
|
|
|
598
599
|
export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
|
|
599
|
-
protected version?: string
|
|
600
600
|
constructor(
|
|
601
601
|
protected db: Executor,
|
|
602
602
|
table: string,
|
|
@@ -619,24 +619,29 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
|
|
|
619
619
|
q?: string,
|
|
620
620
|
excluding?: string,
|
|
621
621
|
buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
|
|
622
|
-
buildParam?: (i: number) => string,
|
|
623
622
|
total?: string,
|
|
624
623
|
) {
|
|
625
|
-
super(db
|
|
626
|
-
const x =
|
|
624
|
+
super(db, table, attributes, buildQ, fromDB, sort, q, excluding, buildSort, total)
|
|
625
|
+
const x = buildMetadata(attributes)
|
|
627
626
|
if (x) {
|
|
628
|
-
this.version = x.
|
|
627
|
+
this.version = x.version
|
|
629
628
|
}
|
|
630
629
|
this.create = this.create.bind(this)
|
|
631
630
|
this.update = this.update.bind(this)
|
|
632
631
|
this.patch = this.patch.bind(this)
|
|
633
632
|
}
|
|
634
633
|
create(obj: T, tx?: Transaction): Promise<number> {
|
|
635
|
-
let obj2 = obj
|
|
634
|
+
let obj2: any = obj
|
|
636
635
|
if (this.toDB) {
|
|
637
636
|
obj2 = this.toDB(obj)
|
|
638
637
|
}
|
|
639
|
-
|
|
638
|
+
if (this.createdAt) {
|
|
639
|
+
obj2[this.createdAt] = new Date()
|
|
640
|
+
}
|
|
641
|
+
if (this.updatedAt) {
|
|
642
|
+
obj2[this.updatedAt] = new Date()
|
|
643
|
+
}
|
|
644
|
+
const stmt = buildToInsert(obj2, this.table, this.attributes, this.db.param, this.version)
|
|
640
645
|
if (stmt.query) {
|
|
641
646
|
const db = tx ? tx: this.db
|
|
642
647
|
return db.execute(stmt.query, stmt.params).catch((err) => {
|
|
@@ -651,11 +656,14 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
|
|
|
651
656
|
}
|
|
652
657
|
}
|
|
653
658
|
update(obj: T, tx?: Transaction): Promise<number> {
|
|
654
|
-
let obj2 = obj
|
|
659
|
+
let obj2: any = obj
|
|
655
660
|
if (this.toDB) {
|
|
656
661
|
obj2 = this.toDB(obj)
|
|
657
662
|
}
|
|
658
|
-
|
|
663
|
+
if (this.updatedAt) {
|
|
664
|
+
obj2[this.updatedAt] = new Date()
|
|
665
|
+
}
|
|
666
|
+
const stmt = buildToUpdate(obj2, this.table, this.attributes, this.db.param, this.version)
|
|
659
667
|
if (stmt.query) {
|
|
660
668
|
const db = tx ? tx: this.db
|
|
661
669
|
return db.execute(stmt.query, stmt.params)
|
|
@@ -691,10 +699,9 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
|
|
|
691
699
|
q?: string,
|
|
692
700
|
excluding?: string,
|
|
693
701
|
buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
|
|
694
|
-
buildParam?: (i: number) => string,
|
|
695
702
|
total?: string,
|
|
696
703
|
) {
|
|
697
|
-
super(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort,
|
|
704
|
+
super(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, total)
|
|
698
705
|
this.metadata = this.metadata.bind(this)
|
|
699
706
|
this.all = this.all.bind(this)
|
|
700
707
|
this.load = this.load.bind(this)
|
|
@@ -710,7 +717,7 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
|
|
|
710
717
|
return db.query(sql, [], this.map, this.bools)
|
|
711
718
|
}
|
|
712
719
|
load(id: ID, tx?: Transaction): Promise<T | null> {
|
|
713
|
-
const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
|
|
720
|
+
const stmt = select<ID>(id, this.table, this.primaryKeys, this.db.param)
|
|
714
721
|
if (!stmt.query) {
|
|
715
722
|
throw new Error("cannot build query by id")
|
|
716
723
|
}
|
|
@@ -731,7 +738,7 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
|
|
|
731
738
|
}
|
|
732
739
|
exist(id: ID, tx?: Transaction): Promise<boolean> {
|
|
733
740
|
const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
|
|
734
|
-
const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
|
|
741
|
+
const stmt = exist<ID>(id, this.table, this.primaryKeys, this.db.param, field)
|
|
735
742
|
if (!stmt.query) {
|
|
736
743
|
throw new Error("cannot build query by id")
|
|
737
744
|
}
|
|
@@ -739,7 +746,7 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
|
|
|
739
746
|
return db.query(stmt.query, stmt.params).then((res) => (!res || res.length === 0 ? false : true))
|
|
740
747
|
}
|
|
741
748
|
delete(id: ID, tx?: Transaction): Promise<number> {
|
|
742
|
-
const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param)
|
|
749
|
+
const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.db.param)
|
|
743
750
|
if (stmt.query) {
|
|
744
751
|
const db = tx ? tx: this.db
|
|
745
752
|
return db.execute(stmt.query, stmt.params)
|
|
@@ -748,14 +755,17 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
|
|
|
748
755
|
}
|
|
749
756
|
}
|
|
750
757
|
}
|
|
758
|
+
export const Repository = SqlRepository
|
|
759
|
+
|
|
760
|
+
interface MinDB {
|
|
761
|
+
driver?: string
|
|
762
|
+
param(i: number): string
|
|
763
|
+
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
|
|
764
|
+
}
|
|
751
765
|
// tslint:disable-next-line:max-classes-per-file
|
|
752
766
|
export class Query<T, ID, S> extends SearchBuilder<T, S> {
|
|
753
|
-
primaryKeys: Attribute[]
|
|
754
|
-
map?: StringMap
|
|
755
|
-
// attributes: Attributes;
|
|
756
|
-
bools?: Attribute[]
|
|
757
767
|
constructor(
|
|
758
|
-
|
|
768
|
+
db: MinDB,
|
|
759
769
|
table: string,
|
|
760
770
|
attributes: Attributes,
|
|
761
771
|
buildQ?: (
|
|
@@ -770,16 +780,14 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
|
|
|
770
780
|
strExcluding?: string,
|
|
771
781
|
likeType?: LikeType
|
|
772
782
|
) => Statement | undefined,
|
|
773
|
-
provider?: string,
|
|
774
783
|
fromDB?: (v: T) => T,
|
|
775
784
|
sort?: string,
|
|
776
785
|
q?: string,
|
|
777
786
|
excluding?: string,
|
|
778
787
|
buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
|
|
779
|
-
buildParam?: (i: number) => string,
|
|
780
788
|
total?: string,
|
|
781
789
|
) {
|
|
782
|
-
super(
|
|
790
|
+
super(db, table, attributes, buildQ, fromDB, sort, q, excluding, buildSort, total)
|
|
783
791
|
const m = buildMetadata(attributes)
|
|
784
792
|
this.primaryKeys = m.keys
|
|
785
793
|
this.map = m.map
|
|
@@ -794,18 +802,20 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
|
|
|
794
802
|
metadata?(): Attributes | undefined {
|
|
795
803
|
return this.attrs
|
|
796
804
|
}
|
|
797
|
-
all(
|
|
805
|
+
all(tx?: Transaction): Promise<T[]> {
|
|
798
806
|
const sql = `select * from ${this.table}`
|
|
799
|
-
|
|
807
|
+
const db = tx ? tx : this.db
|
|
808
|
+
return db.query(sql, [], this.map, this.bools)
|
|
800
809
|
}
|
|
801
|
-
load(id: ID,
|
|
802
|
-
const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
|
|
810
|
+
load(id: ID, tx?: Transaction): Promise<T | null> {
|
|
811
|
+
const stmt = select<ID>(id, this.table, this.primaryKeys, this.db.param)
|
|
803
812
|
if (!stmt.query) {
|
|
804
813
|
throw new Error("cannot build query by id")
|
|
805
814
|
}
|
|
815
|
+
const db = tx ? tx : this.db
|
|
806
816
|
const fn = this.fromDB
|
|
807
817
|
if (fn) {
|
|
808
|
-
return
|
|
818
|
+
return db.query<T>(stmt.query, stmt.params, this.map, this.bools).then((res) => {
|
|
809
819
|
if (!res || res.length === 0) {
|
|
810
820
|
return null
|
|
811
821
|
} else {
|
|
@@ -814,15 +824,16 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
|
|
|
814
824
|
}
|
|
815
825
|
})
|
|
816
826
|
} else {
|
|
817
|
-
return this.query<T>(stmt.query, stmt.params, this.map, this.bools
|
|
827
|
+
return this.db.query<T>(stmt.query, stmt.params, this.map, this.bools).then((res) => (!res || res.length === 0 ? null : res[0]))
|
|
818
828
|
}
|
|
819
829
|
}
|
|
820
|
-
exist(id: ID,
|
|
830
|
+
exist(id: ID, tx?: Transaction): Promise<boolean> {
|
|
821
831
|
const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
|
|
822
|
-
const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
|
|
832
|
+
const stmt = exist<ID>(id, this.table, this.primaryKeys, this.db.param, field)
|
|
823
833
|
if (!stmt.query) {
|
|
824
834
|
throw new Error("cannot build query by id")
|
|
825
835
|
}
|
|
826
|
-
|
|
836
|
+
const db = tx ? tx : this.db
|
|
837
|
+
return db.query(stmt.query, stmt.params, undefined, undefined).then((res) => (!res || res.length === 0 ? false : true))
|
|
827
838
|
}
|
|
828
839
|
}
|