query-core 0.6.4 → 0.6.5

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.
@@ -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(query, table, attrs, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) {
12
- this.query = query;
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) {
@@ -30,23 +29,6 @@ var SearchBuilder = (function () {
30
29
  this.q = q && q.length > 0 ? q : "q";
31
30
  this.excluding = excluding && excluding.length > 0 ? excluding : "excluding";
32
31
  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
32
  this.total = total && total.length > 0 ? total : "total";
51
33
  }
52
34
  SearchBuilder.prototype.search = function (filter, limit, page, fields) {
@@ -56,14 +38,14 @@ var SearchBuilder = (function () {
56
38
  }
57
39
  var st = this.sort ? this.sort : "sort";
58
40
  var sn = filter[st];
59
- var likeType = this.provider === exports.postgres ? "ilike" : "like";
60
- var q2 = this.buildQuery(filter, this.param, sn, this.buildSort, this.attrs, this.table, fields, this.q, this.excluding, likeType);
41
+ var likeType = this.db.driver === exports.postgres ? "ilike" : "like";
42
+ var q2 = this.buildQuery(filter, this.db.param, sn, this.buildSort, this.attrs, this.table, fields, this.q, this.excluding, likeType);
61
43
  if (!q2) {
62
44
  throw new Error("Cannot build query");
63
45
  }
64
46
  var fn = this.fromDB;
65
47
  if (fn) {
66
- return search_1.buildFromQuery(this.query, q2.query, q2.params, limit, ipage, this.map, this.bools, this.provider, this.total).then(function (r) {
48
+ 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
49
  if (r.list && r.list.length > 0) {
68
50
  r.list = r.list.map(function (o) { return fn(o); });
69
51
  return r;
@@ -74,9 +56,10 @@ var SearchBuilder = (function () {
74
56
  });
75
57
  }
76
58
  else {
77
- return search_1.buildFromQuery(this.query, q2.query, q2.params, limit, ipage, this.map, this.bools, this.provider, this.total);
59
+ return search_1.buildFromQuery(this.db.query, q2.query, q2.params, limit, ipage, this.map, this.bools, this.db.driver, this.total);
78
60
  }
79
61
  };
80
62
  return SearchBuilder;
81
63
  }());
82
64
  exports.SearchBuilder = SearchBuilder;
65
+ exports.SearchRepository = SearchBuilder;
package/lib/health.js CHANGED
@@ -77,6 +77,7 @@ var Checker = (function () {
77
77
  return Checker;
78
78
  }());
79
79
  exports.Checker = Checker;
80
+ exports.SqlChecker = Checker;
80
81
  function promiseTimeOut(timeoutInMilliseconds, promise) {
81
82
  return Promise.race([
82
83
  promise,
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.exec = this.exec.bind(this);
158
- this.execBatch = this.execBatch.bind(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.execScalar = this.execScalar.bind(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.exec = function (sql, args, ctx) {
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
- .exec(sql, args, ctx)
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.execBatch = function (statements, firstSuccess, ctx) {
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
- .execBatch(statements, firstSuccess, ctx)
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.execScalar = function (sql, args, ctx) {
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
- .execScalar(sql, args, ctx)
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
- if (x) {
420
- this.version = x.version;
421
- this.createdAt = x.createdAt;
422
- this.updatedAt = x.updatedAt;
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,10 +543,12 @@ 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, buildParam, total) {
552
- var _this = _super.call(this, db.query, table, attributes, db.driver, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
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;
@@ -567,7 +566,7 @@ var SqlSearchWriter = (function (_super) {
567
566
  if (this.toDB) {
568
567
  obj2 = this.toDB(obj);
569
568
  }
570
- var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
569
+ var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.db.param, this.version);
571
570
  if (stmt.query) {
572
571
  var db = tx ? tx : this.db;
573
572
  return db.execute(stmt.query, stmt.params).catch(function (err) {
@@ -588,7 +587,7 @@ var SqlSearchWriter = (function (_super) {
588
587
  if (this.toDB) {
589
588
  obj2 = this.toDB(obj);
590
589
  }
591
- var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
590
+ var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.db.param, this.version);
592
591
  if (stmt.query) {
593
592
  var db = tx ? tx : this.db;
594
593
  return db.execute(stmt.query, stmt.params);
@@ -606,7 +605,7 @@ exports.SqlSearchWriter = SqlSearchWriter;
606
605
  var SqlRepository = (function (_super) {
607
606
  __extends(SqlRepository, _super);
608
607
  function SqlRepository(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
609
- var _this = _super.call(this, db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
608
+ var _this = _super.call(this, db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, total) || this;
610
609
  _this.attributes = attributes;
611
610
  _this.toDB = toDB;
612
611
  _this.metadata = _this.metadata.bind(_this);
@@ -625,7 +624,7 @@ var SqlRepository = (function (_super) {
625
624
  return db.query(sql, [], this.map, this.bools);
626
625
  };
627
626
  SqlRepository.prototype.load = function (id, tx) {
628
- var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
627
+ var stmt = build_1.select(id, this.table, this.primaryKeys, this.db.param);
629
628
  if (!stmt.query) {
630
629
  throw new Error("cannot build query by id");
631
630
  }
@@ -648,7 +647,7 @@ var SqlRepository = (function (_super) {
648
647
  };
649
648
  SqlRepository.prototype.exist = function (id, tx) {
650
649
  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);
650
+ var stmt = build_1.exist(id, this.table, this.primaryKeys, this.db.param, field);
652
651
  if (!stmt.query) {
653
652
  throw new Error("cannot build query by id");
654
653
  }
@@ -656,7 +655,7 @@ var SqlRepository = (function (_super) {
656
655
  return db.query(stmt.query, stmt.params).then(function (res) { return (!res || res.length === 0 ? false : true); });
657
656
  };
658
657
  SqlRepository.prototype.delete = function (id, tx) {
659
- var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.param);
658
+ var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.db.param);
660
659
  if (stmt.query) {
661
660
  var db = tx ? tx : this.db;
662
661
  return db.execute(stmt.query, stmt.params);
@@ -668,10 +667,11 @@ var SqlRepository = (function (_super) {
668
667
  return SqlRepository;
669
668
  }(SqlSearchWriter));
670
669
  exports.SqlRepository = SqlRepository;
670
+ exports.Repository = SqlRepository;
671
671
  var Query = (function (_super) {
672
672
  __extends(Query, _super);
673
- function Query(query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total) {
674
- var _this = _super.call(this, query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
673
+ function Query(db, table, attributes, buildQ, fromDB, sort, q, excluding, buildSort, total) {
674
+ var _this = _super.call(this, db, table, attributes, buildQ, fromDB, sort, q, excluding, buildSort, total) || this;
675
675
  var m = build_1.buildMetadata(attributes);
676
676
  _this.primaryKeys = m.keys;
677
677
  _this.map = m.map;
@@ -687,18 +687,20 @@ var Query = (function (_super) {
687
687
  Query.prototype.metadata = function () {
688
688
  return this.attrs;
689
689
  };
690
- Query.prototype.all = function (ctx) {
690
+ Query.prototype.all = function (tx) {
691
691
  var sql = "select * from " + this.table;
692
- return this.query(sql, [], this.map, this.bools, ctx);
692
+ var db = tx ? tx : this.db;
693
+ return db.query(sql, [], this.map, this.bools);
693
694
  };
694
- Query.prototype.load = function (id, ctx) {
695
- var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
695
+ Query.prototype.load = function (id, tx) {
696
+ var stmt = build_1.select(id, this.table, this.primaryKeys, this.db.param);
696
697
  if (!stmt.query) {
697
698
  throw new Error("cannot build query by id");
698
699
  }
700
+ var db = tx ? tx : this.db;
699
701
  var fn = this.fromDB;
700
702
  if (fn) {
701
- return this.query(stmt.query, stmt.params, this.map, this.bools, ctx).then(function (res) {
703
+ return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) {
702
704
  if (!res || res.length === 0) {
703
705
  return null;
704
706
  }
@@ -709,16 +711,17 @@ var Query = (function (_super) {
709
711
  });
710
712
  }
711
713
  else {
712
- return this.query(stmt.query, stmt.params, this.map, this.bools, ctx).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
714
+ return this.db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
713
715
  }
714
716
  };
715
- Query.prototype.exist = function (id, ctx) {
717
+ Query.prototype.exist = function (id, tx) {
716
718
  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);
719
+ var stmt = build_1.exist(id, this.table, this.primaryKeys, this.db.param, field);
718
720
  if (!stmt.query) {
719
721
  throw new Error("cannot build query by id");
720
722
  }
721
- return this.query(stmt.query, stmt.params, undefined, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
723
+ var db = tx ? tx : this.db;
724
+ return db.query(stmt.query, stmt.params, undefined, undefined).then(function (res) { return (!res || res.length === 0 ? false : true); });
722
725
  };
723
726
  return Query;
724
727
  }(SearchBuilder_1.SearchBuilder));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.6.4",
3
+ "version": "0.6.5",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -8,11 +8,16 @@ 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
- buildQuery: (
17
+ protected map?: StringMap
18
+ protected bools?: Attribute[]
19
+ protected primaryKeys: Attribute[]
20
+ protected buildQuery: (
16
21
  s: S,
17
22
  param: (i: number) => string,
18
23
  sort?: string,
@@ -24,16 +29,14 @@ export class SearchBuilder<T, S> {
24
29
  strExcluding?: string,
25
30
  likeType?: LikeType
26
31
  ) => Statement | undefined
27
- q?: string
28
- excluding?: string
29
- buildSort?: (sort?: string, map?: Attributes | StringMap) => string
30
- param: (i: number) => string
31
- total?: string
32
+ protected q?: string
33
+ protected excluding?: string
34
+ protected buildSort?: (sort?: string, map?: Attributes | StringMap) => string
35
+ protected total?: string
32
36
  constructor(
33
- protected query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
37
+ protected db: MinDB,
34
38
  protected table: string,
35
39
  protected attrs?: Attributes,
36
- protected provider?: string,
37
40
  buildQ?: (
38
41
  s: S,
39
42
  param: (i: number) => string,
@@ -51,7 +54,6 @@ export class SearchBuilder<T, S> {
51
54
  q?: string,
52
55
  excluding?: string,
53
56
  buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
54
- buildParam?: (i: number) => string,
55
57
  total?: string,
56
58
  ) {
57
59
  if (attrs) {
@@ -68,19 +70,6 @@ export class SearchBuilder<T, S> {
68
70
  this.q = q && q.length > 0 ? q : "q"
69
71
  this.excluding = excluding && excluding.length > 0 ? excluding : "excluding"
70
72
  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
73
  this.total = total && total.length > 0 ? total : "total"
85
74
  }
86
75
  search(filter: S, limit: number, page?: number | string, fields?: string[]): Promise<SearchResult<T>> {
@@ -90,14 +79,14 @@ export class SearchBuilder<T, S> {
90
79
  }
91
80
  const st = this.sort ? this.sort : "sort"
92
81
  const sn = (filter as any)[st] as string
93
- const likeType = this.provider === postgres ? "ilike" : "like"
94
- const q2 = this.buildQuery(filter, this.param, sn, this.buildSort, this.attrs, this.table, fields, this.q, this.excluding, likeType)
82
+ const likeType = this.db.driver === postgres ? "ilike" : "like"
83
+ const q2 = this.buildQuery(filter, this.db.param, sn, this.buildSort, this.attrs, this.table, fields, this.q, this.excluding, likeType)
95
84
  if (!q2) {
96
85
  throw new Error("Cannot build query")
97
86
  }
98
87
  const fn = this.fromDB
99
88
  if (fn) {
100
- return buildFromQuery<T>(this.query, q2.query, q2.params, limit, ipage, this.map, this.bools, this.provider, this.total).then((r) => {
89
+ return buildFromQuery<T>(this.db.query, q2.query, q2.params, limit, ipage, this.map, this.bools, this.db.driver, this.total).then((r) => {
101
90
  if (r.list && r.list.length > 0) {
102
91
  r.list = r.list.map((o) => fn(o))
103
92
  return r
@@ -106,7 +95,8 @@ export class SearchBuilder<T, S> {
106
95
  }
107
96
  })
108
97
  } else {
109
- return buildFromQuery(this.query, q2.query, q2.params, limit, ipage, this.map, this.bools, this.provider, this.total)
98
+ return buildFromQuery(this.db.query, q2.query, q2.params, limit, ipage, this.map, this.bools, this.db.driver, this.total)
110
99
  }
111
100
  }
112
101
  }
102
+ export const SearchRepository = SearchBuilder
package/src/health.ts CHANGED
@@ -39,6 +39,7 @@ export class Checker {
39
39
  return data
40
40
  }
41
41
  }
42
+ export const SqlChecker = Checker
42
43
 
43
44
  function promiseTimeOut(timeoutInMilliseconds: number, promise: Promise<any>): Promise<any> {
44
45
  return Promise.race([
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
- exec(sql: string, args?: any[], ctx?: any): Promise<number>
139
- execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>
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
- execScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T>
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.exec = this.exec.bind(this)
211
- this.execBatch = this.execBatch.bind(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.execScalar = this.execScalar.bind(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
- exec(sql: string, args?: any[], ctx?: any): Promise<number> {
232
+ execute(sql: string, args?: any[], ctx?: any): Promise<number> {
229
233
  const t1 = new Date()
230
234
  return this.db
231
- .exec(sql, args, ctx)
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
- execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
266
+ executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
263
267
  const t1 = new Date()
264
268
  return this.db
265
- .execBatch(statements, firstSuccess, ctx)
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
- execScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T> {
376
+ executeScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T> {
373
377
  const t1 = new Date()
374
378
  return this.db
375
- .execScalar<T>(sql, args, ctx)
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
- if (x) {
478
- this.version = x.version
479
- this.createdAt = x.createdAt
480
- this.updatedAt = x.updatedAt
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
- (obj2 as any)[this.createdAt] = new Date()
501
+ obj2[this.createdAt] = new Date()
493
502
  }
494
503
  if (this.updatedAt) {
495
- (obj2 as any)[this.updatedAt] = new Date()
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
- (obj2 as any)[this.updatedAt] = new Date()
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,6 +593,8 @@ 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
600
  protected version?: string
@@ -619,10 +620,9 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
619
620
  q?: string,
620
621
  excluding?: string,
621
622
  buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
622
- buildParam?: (i: number) => string,
623
623
  total?: string,
624
624
  ) {
625
- super(db.query, table, attributes, db.driver, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total)
625
+ super(db, table, attributes, buildQ, fromDB, sort, q, excluding, buildSort, total)
626
626
  const x = version(attributes)
627
627
  if (x) {
628
628
  this.version = x.name
@@ -636,7 +636,7 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
636
636
  if (this.toDB) {
637
637
  obj2 = this.toDB(obj)
638
638
  }
639
- const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version)
639
+ const stmt = buildToInsert(obj2, this.table, this.attributes, this.db.param, this.version)
640
640
  if (stmt.query) {
641
641
  const db = tx ? tx: this.db
642
642
  return db.execute(stmt.query, stmt.params).catch((err) => {
@@ -655,7 +655,7 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
655
655
  if (this.toDB) {
656
656
  obj2 = this.toDB(obj)
657
657
  }
658
- const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version)
658
+ const stmt = buildToUpdate(obj2, this.table, this.attributes, this.db.param, this.version)
659
659
  if (stmt.query) {
660
660
  const db = tx ? tx: this.db
661
661
  return db.execute(stmt.query, stmt.params)
@@ -694,7 +694,7 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
694
694
  buildParam?: (i: number) => string,
695
695
  total?: string,
696
696
  ) {
697
- super(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total)
697
+ super(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, total)
698
698
  this.metadata = this.metadata.bind(this)
699
699
  this.all = this.all.bind(this)
700
700
  this.load = this.load.bind(this)
@@ -710,7 +710,7 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
710
710
  return db.query(sql, [], this.map, this.bools)
711
711
  }
712
712
  load(id: ID, tx?: Transaction): Promise<T | null> {
713
- const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
713
+ const stmt = select<ID>(id, this.table, this.primaryKeys, this.db.param)
714
714
  if (!stmt.query) {
715
715
  throw new Error("cannot build query by id")
716
716
  }
@@ -731,7 +731,7 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
731
731
  }
732
732
  exist(id: ID, tx?: Transaction): Promise<boolean> {
733
733
  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)
734
+ const stmt = exist<ID>(id, this.table, this.primaryKeys, this.db.param, field)
735
735
  if (!stmt.query) {
736
736
  throw new Error("cannot build query by id")
737
737
  }
@@ -739,7 +739,7 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
739
739
  return db.query(stmt.query, stmt.params).then((res) => (!res || res.length === 0 ? false : true))
740
740
  }
741
741
  delete(id: ID, tx?: Transaction): Promise<number> {
742
- const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param)
742
+ const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.db.param)
743
743
  if (stmt.query) {
744
744
  const db = tx ? tx: this.db
745
745
  return db.execute(stmt.query, stmt.params)
@@ -748,14 +748,17 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
748
748
  }
749
749
  }
750
750
  }
751
+ export const Repository = SqlRepository
752
+
753
+ interface MinDB {
754
+ driver?: string
755
+ param(i: number): string
756
+ query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
757
+ }
751
758
  // tslint:disable-next-line:max-classes-per-file
752
759
  export class Query<T, ID, S> extends SearchBuilder<T, S> {
753
- primaryKeys: Attribute[]
754
- map?: StringMap
755
- // attributes: Attributes;
756
- bools?: Attribute[]
757
760
  constructor(
758
- query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
761
+ db: MinDB,
759
762
  table: string,
760
763
  attributes: Attributes,
761
764
  buildQ?: (
@@ -770,16 +773,14 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
770
773
  strExcluding?: string,
771
774
  likeType?: LikeType
772
775
  ) => Statement | undefined,
773
- provider?: string,
774
776
  fromDB?: (v: T) => T,
775
777
  sort?: string,
776
778
  q?: string,
777
779
  excluding?: string,
778
780
  buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
779
- buildParam?: (i: number) => string,
780
781
  total?: string,
781
782
  ) {
782
- super(query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total)
783
+ super(db, table, attributes, buildQ, fromDB, sort, q, excluding, buildSort, total)
783
784
  const m = buildMetadata(attributes)
784
785
  this.primaryKeys = m.keys
785
786
  this.map = m.map
@@ -794,18 +795,20 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
794
795
  metadata?(): Attributes | undefined {
795
796
  return this.attrs
796
797
  }
797
- all(ctx?: any): Promise<T[]> {
798
+ all(tx?: Transaction): Promise<T[]> {
798
799
  const sql = `select * from ${this.table}`
799
- return this.query(sql, [], this.map, this.bools, ctx)
800
+ const db = tx ? tx : this.db
801
+ return db.query(sql, [], this.map, this.bools)
800
802
  }
801
- load(id: ID, ctx?: any): Promise<T | null> {
802
- const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
803
+ load(id: ID, tx?: Transaction): Promise<T | null> {
804
+ const stmt = select<ID>(id, this.table, this.primaryKeys, this.db.param)
803
805
  if (!stmt.query) {
804
806
  throw new Error("cannot build query by id")
805
807
  }
808
+ const db = tx ? tx : this.db
806
809
  const fn = this.fromDB
807
810
  if (fn) {
808
- return this.query<T>(stmt.query, stmt.params, this.map, this.bools, ctx).then((res) => {
811
+ return db.query<T>(stmt.query, stmt.params, this.map, this.bools).then((res) => {
809
812
  if (!res || res.length === 0) {
810
813
  return null
811
814
  } else {
@@ -814,15 +817,16 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
814
817
  }
815
818
  })
816
819
  } else {
817
- return this.query<T>(stmt.query, stmt.params, this.map, this.bools, ctx).then((res) => (!res || res.length === 0 ? null : res[0]))
820
+ return this.db.query<T>(stmt.query, stmt.params, this.map, this.bools).then((res) => (!res || res.length === 0 ? null : res[0]))
818
821
  }
819
822
  }
820
- exist(id: ID, ctx?: any): Promise<boolean> {
823
+ exist(id: ID, tx?: Transaction): Promise<boolean> {
821
824
  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)
825
+ const stmt = exist<ID>(id, this.table, this.primaryKeys, this.db.param, field)
823
826
  if (!stmt.query) {
824
827
  throw new Error("cannot build query by id")
825
828
  }
826
- return this.query(stmt.query, stmt.params, undefined, undefined, ctx).then((res) => (!res || res.length === 0 ? false : true))
829
+ const db = tx ? tx : this.db
830
+ return db.query(stmt.query, stmt.params, undefined, undefined).then((res) => (!res || res.length === 0 ? false : true))
827
831
  }
828
832
  }