query-core 0.6.2 → 0.6.4

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.
@@ -17,7 +17,7 @@ var SearchBuilder = (function () {
17
17
  this.sort = sort;
18
18
  if (attrs) {
19
19
  this.attrs = attrs;
20
- var meta = build_1.metadata(attrs);
20
+ var meta = build_1.buildMetadata(attrs);
21
21
  this.map = meta.map;
22
22
  this.bools = meta.bools;
23
23
  this.primaryKeys = meta.keys;
package/lib/build.js CHANGED
@@ -543,7 +543,7 @@ function updateBatch(exec, objs, table, attrs, buildParam, notSkipInvalid) {
543
543
  exports.updateBatch = updateBatch;
544
544
  function buildToUpdateBatch(objs, table, attrs, buildParam, notSkipInvalid) {
545
545
  var sts = [];
546
- var meta = metadata(attrs);
546
+ var meta = buildMetadata(attrs);
547
547
  if (!meta.keys || meta.keys.length === 0) {
548
548
  return sts;
549
549
  }
@@ -724,7 +724,7 @@ function buildMap(attrs) {
724
724
  return mp;
725
725
  }
726
726
  exports.buildMap = buildMap;
727
- function metadata(attrs) {
727
+ function buildMetadata(attrs) {
728
728
  var mp = {};
729
729
  var ks = Object.keys(attrs);
730
730
  var ats = [];
@@ -748,6 +748,12 @@ function metadata(attrs) {
748
748
  if (attr.version) {
749
749
  m.version = k;
750
750
  }
751
+ if (attr.updatedAt) {
752
+ m.updatedAt = k;
753
+ }
754
+ else if (attr.createdAt) {
755
+ m.createdAt = k;
756
+ }
751
757
  var field = attr.column ? attr.column : k;
752
758
  var s = field.toLowerCase();
753
759
  if (s !== k) {
@@ -763,7 +769,7 @@ function metadata(attrs) {
763
769
  }
764
770
  return m;
765
771
  }
766
- exports.metadata = metadata;
772
+ exports.buildMetadata = buildMetadata;
767
773
  function attributes(attrs, isKey) {
768
774
  var ks = [];
769
775
  for (var _i = 0, attrs_1 = attrs; _i < attrs_1.length; _i++) {
package/lib/services.js CHANGED
@@ -15,16 +15,15 @@ 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, param, table, attrs, fromDB) {
19
- var l = new SqlLoader(q, param, table, attrs, fromDB);
18
+ function useGet(db, table, attrs, fromDB) {
19
+ var l = new SqlLoader(db, 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, param, table, attrs, fromDB) {
26
- this.query = query;
27
- this.param = param;
25
+ function SqlLoader(db, table, attrs, fromDB) {
26
+ this.db = db;
28
27
  this.table = table;
29
28
  this.fromDB = fromDB;
30
29
  if (Array.isArray(attrs)) {
@@ -32,7 +31,7 @@ var SqlLoader = (function () {
32
31
  this.attributes = {};
33
32
  }
34
33
  else {
35
- var m = build_1.metadata(attrs);
34
+ var m = build_1.buildMetadata(attrs);
36
35
  this.attributes = attrs;
37
36
  this.primaryKeys = m.keys;
38
37
  this.map = m.map;
@@ -48,18 +47,20 @@ var SqlLoader = (function () {
48
47
  SqlLoader.prototype.metadata = function () {
49
48
  return this.attributes;
50
49
  };
51
- SqlLoader.prototype.all = function (ctx) {
50
+ SqlLoader.prototype.all = function (tx) {
52
51
  var sql = "select * from " + this.table;
53
- return this.query(sql, [], this.map, this.bools, ctx);
52
+ var db = tx ? tx : this.db;
53
+ return db.query(sql, [], this.map, this.bools);
54
54
  };
55
- SqlLoader.prototype.load = function (id, ctx) {
56
- var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
55
+ SqlLoader.prototype.load = function (id, tx) {
56
+ var stmt = build_1.select(id, this.table, this.primaryKeys, this.db.param);
57
57
  if (!stmt.query) {
58
58
  throw new Error("cannot build query by id");
59
59
  }
60
+ var db = tx ? tx : this.db;
60
61
  var fn = this.fromDB;
61
62
  if (fn) {
62
- return this.query(stmt.query, stmt.params, this.map, this.bools, ctx).then(function (res) {
63
+ return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) {
63
64
  if (!res || res.length === 0) {
64
65
  return null;
65
66
  }
@@ -70,16 +71,17 @@ var SqlLoader = (function () {
70
71
  });
71
72
  }
72
73
  else {
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
+ return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
74
75
  }
75
76
  };
76
- SqlLoader.prototype.exist = function (id, ctx) {
77
+ SqlLoader.prototype.exist = function (id, tx) {
77
78
  var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
78
- var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
79
+ var stmt = build_1.exist(id, this.table, this.primaryKeys, this.db.param, field);
79
80
  if (!stmt.query) {
80
81
  throw new Error("cannot build query by id");
81
82
  }
82
- return this.query(stmt.query, stmt.params, undefined, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
83
+ var db = tx ? tx : this.db;
84
+ return db.query(stmt.query, stmt.params, undefined, undefined).then(function (res) { return (!res || res.length === 0 ? false : true); });
83
85
  };
84
86
  return SqlLoader;
85
87
  }());
@@ -92,11 +94,11 @@ var QueryRepository = (function () {
92
94
  this.sort = sort;
93
95
  this.id = id && id.length > 0 ? id : "id";
94
96
  this.query = this.query.bind(this);
95
- var m = build_1.metadata(attrs);
97
+ var m = build_1.buildMetadata(attrs);
96
98
  this.map = m.map;
97
99
  this.bools = m.bools;
98
100
  }
99
- QueryRepository.prototype.query = function (ids, ctx) {
101
+ QueryRepository.prototype.query = function (ids, tx) {
100
102
  if (!ids || ids.length === 0) {
101
103
  return Promise.resolve([]);
102
104
  }
@@ -109,7 +111,7 @@ var QueryRepository = (function () {
109
111
  if (this.sort && this.sort.length > 0) {
110
112
  sql = sql + " order by " + this.sort;
111
113
  }
112
- var db = ctx ? ctx : this.db;
114
+ var db = tx ? tx : this.db;
113
115
  return db.query(sql, ids, this.map, this.bools);
114
116
  };
115
117
  return QueryRepository;
@@ -413,23 +415,31 @@ var SqlWriter = (function () {
413
415
  this.table = table;
414
416
  this.attributes = attributes;
415
417
  this.toDB = toDB;
416
- var x = build_1.version(attributes);
418
+ var x = build_1.buildMetadata(attributes);
417
419
  if (x) {
418
- this.version = x.name;
420
+ this.version = x.version;
421
+ this.createdAt = x.createdAt;
422
+ this.updatedAt = x.updatedAt;
419
423
  }
420
424
  this.create = this.create.bind(this);
421
425
  this.update = this.update.bind(this);
422
426
  this.patch = this.patch.bind(this);
423
427
  }
424
- SqlWriter.prototype.create = function (obj, ctx) {
428
+ SqlWriter.prototype.create = function (obj, tx) {
425
429
  var obj2 = obj;
426
430
  if (this.toDB) {
427
431
  obj2 = this.toDB(obj);
428
432
  }
433
+ if (this.createdAt) {
434
+ obj2[this.createdAt] = new Date();
435
+ }
436
+ if (this.updatedAt) {
437
+ obj2[this.updatedAt] = new Date();
438
+ }
429
439
  var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.db.param, this.version);
430
440
  if (stmt.query) {
431
- var db = ctx ? ctx : this.db;
432
- return db.exec(stmt.query, stmt.params).catch(function (err) {
441
+ var db = tx ? tx : this.db;
442
+ return db.execute(stmt.query, stmt.params).catch(function (err) {
433
443
  if (err && err.error === "duplicate") {
434
444
  return 0;
435
445
  }
@@ -442,22 +452,28 @@ var SqlWriter = (function () {
442
452
  return Promise.resolve(-1);
443
453
  }
444
454
  };
445
- SqlWriter.prototype.update = function (obj, ctx) {
455
+ SqlWriter.prototype.update = function (obj, tx) {
446
456
  var obj2 = obj;
447
457
  if (this.toDB) {
448
458
  obj2 = this.toDB(obj);
449
459
  }
460
+ if (this.createdAt) {
461
+ obj2[this.createdAt] = new Date();
462
+ }
463
+ if (this.updatedAt) {
464
+ obj2[this.updatedAt] = new Date();
465
+ }
450
466
  var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.db.param, this.version);
451
467
  if (stmt.query) {
452
- var db = ctx ? ctx : this.db;
453
- return db.exec(stmt.query, stmt.params);
468
+ var db = tx ? tx : this.db;
469
+ return db.execute(stmt.query, stmt.params);
454
470
  }
455
471
  else {
456
472
  return Promise.resolve(-1);
457
473
  }
458
474
  };
459
- SqlWriter.prototype.patch = function (obj, ctx) {
460
- return this.update(obj, ctx);
475
+ SqlWriter.prototype.patch = function (obj, tx) {
476
+ return this.update(obj, tx);
461
477
  };
462
478
  return SqlWriter;
463
479
  }());
@@ -467,7 +483,7 @@ var CRUDRepository = (function (_super) {
467
483
  function CRUDRepository(db, table, attributes, toDB, fromDB) {
468
484
  var _this = _super.call(this, db, table, attributes, toDB) || this;
469
485
  _this.fromDB = fromDB;
470
- var m = build_1.metadata(attributes);
486
+ var m = build_1.buildMetadata(attributes);
471
487
  _this.primaryKeys = m.keys;
472
488
  _this.map = m.map;
473
489
  _this.bools = m.bools;
@@ -481,18 +497,18 @@ var CRUDRepository = (function (_super) {
481
497
  CRUDRepository.prototype.metadata = function () {
482
498
  return this.attributes;
483
499
  };
484
- CRUDRepository.prototype.all = function (ctx) {
500
+ CRUDRepository.prototype.all = function (tx) {
485
501
  var sql = "select * from " + this.table;
486
- var db = ctx ? ctx : this.db;
502
+ var db = tx ? tx : this.db;
487
503
  return db.query(sql, [], this.map, this.bools);
488
504
  };
489
- CRUDRepository.prototype.load = function (id, ctx) {
505
+ CRUDRepository.prototype.load = function (id, tx) {
490
506
  var stmt = build_1.select(id, this.table, this.primaryKeys, this.db.param);
491
507
  if (!stmt.query) {
492
508
  throw new Error("cannot build query by id");
493
509
  }
494
510
  var fn = this.fromDB;
495
- var db = ctx ? ctx : this.db;
511
+ var db = tx ? tx : this.db;
496
512
  if (fn) {
497
513
  return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) {
498
514
  if (!res || res.length === 0) {
@@ -508,23 +524,23 @@ var CRUDRepository = (function (_super) {
508
524
  return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
509
525
  }
510
526
  };
511
- CRUDRepository.prototype.exist = function (id, ctx) {
527
+ CRUDRepository.prototype.exist = function (id, tx) {
512
528
  var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
513
529
  var stmt = build_1.exist(id, this.table, this.primaryKeys, this.db.param, field);
514
530
  if (!stmt.query) {
515
531
  throw new Error("cannot build query by id");
516
532
  }
517
- var db = ctx ? ctx : this.db;
533
+ var db = tx ? tx : this.db;
518
534
  return db.query(stmt.query, stmt.params).then(function (res) { return (!res || res.length === 0 ? false : true); });
519
535
  };
520
- CRUDRepository.prototype.delete = function (id, ctx) {
536
+ CRUDRepository.prototype.delete = function (id, tx) {
521
537
  var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.db.param);
522
538
  if (stmt.query) {
523
- var db = ctx ? ctx : this.db;
524
- return db.exec(stmt.query, stmt.params);
539
+ var db = tx ? tx : this.db;
540
+ return db.execute(stmt.query, stmt.params);
525
541
  }
526
542
  else {
527
- return Promise.resolve(-1);
543
+ throw new Error("cannot build delete query by id");
528
544
  }
529
545
  };
530
546
  return CRUDRepository;
@@ -546,15 +562,15 @@ var SqlSearchWriter = (function (_super) {
546
562
  _this.patch = _this.patch.bind(_this);
547
563
  return _this;
548
564
  }
549
- SqlSearchWriter.prototype.create = function (obj, ctx) {
565
+ SqlSearchWriter.prototype.create = function (obj, tx) {
550
566
  var obj2 = obj;
551
567
  if (this.toDB) {
552
568
  obj2 = this.toDB(obj);
553
569
  }
554
570
  var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
555
571
  if (stmt.query) {
556
- var db = ctx ? ctx : this.db;
557
- return db.exec(stmt.query, stmt.params).catch(function (err) {
572
+ var db = tx ? tx : this.db;
573
+ return db.execute(stmt.query, stmt.params).catch(function (err) {
558
574
  if (err && err.error === "duplicate") {
559
575
  return 0;
560
576
  }
@@ -567,22 +583,22 @@ var SqlSearchWriter = (function (_super) {
567
583
  return Promise.resolve(-1);
568
584
  }
569
585
  };
570
- SqlSearchWriter.prototype.update = function (obj, ctx) {
586
+ SqlSearchWriter.prototype.update = function (obj, tx) {
571
587
  var obj2 = obj;
572
588
  if (this.toDB) {
573
589
  obj2 = this.toDB(obj);
574
590
  }
575
591
  var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
576
592
  if (stmt.query) {
577
- var db = ctx ? ctx : this.db;
578
- return db.exec(stmt.query, stmt.params);
593
+ var db = tx ? tx : this.db;
594
+ return db.execute(stmt.query, stmt.params);
579
595
  }
580
596
  else {
581
597
  return Promise.resolve(-1);
582
598
  }
583
599
  };
584
- SqlSearchWriter.prototype.patch = function (obj, ctx) {
585
- return this.update(obj, ctx);
600
+ SqlSearchWriter.prototype.patch = function (obj, tx) {
601
+ return this.update(obj, tx);
586
602
  };
587
603
  return SqlSearchWriter;
588
604
  }(SearchBuilder_1.SearchBuilder));
@@ -603,18 +619,18 @@ var SqlRepository = (function (_super) {
603
619
  SqlRepository.prototype.metadata = function () {
604
620
  return this.attributes;
605
621
  };
606
- SqlRepository.prototype.all = function (ctx) {
622
+ SqlRepository.prototype.all = function (tx) {
607
623
  var sql = "select * from " + this.table;
608
- var db = ctx ? ctx : this.db;
624
+ var db = tx ? tx : this.db;
609
625
  return db.query(sql, [], this.map, this.bools);
610
626
  };
611
- SqlRepository.prototype.load = function (id, ctx) {
627
+ SqlRepository.prototype.load = function (id, tx) {
612
628
  var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
613
- if (!stmt) {
629
+ if (!stmt.query) {
614
630
  throw new Error("cannot build query by id");
615
631
  }
616
632
  var fn = this.fromDB;
617
- var db = ctx ? ctx : this.db;
633
+ var db = tx ? tx : this.db;
618
634
  if (fn) {
619
635
  return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) {
620
636
  if (!res || res.length === 0) {
@@ -630,23 +646,23 @@ var SqlRepository = (function (_super) {
630
646
  return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
631
647
  }
632
648
  };
633
- SqlRepository.prototype.exist = function (id, ctx) {
649
+ SqlRepository.prototype.exist = function (id, tx) {
634
650
  var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
635
651
  var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
636
- if (!stmt) {
652
+ if (!stmt.query) {
637
653
  throw new Error("cannot build query by id");
638
654
  }
639
- var db = ctx ? ctx : this.db;
655
+ var db = tx ? tx : this.db;
640
656
  return db.query(stmt.query, stmt.params).then(function (res) { return (!res || res.length === 0 ? false : true); });
641
657
  };
642
- SqlRepository.prototype.delete = function (id, ctx) {
658
+ SqlRepository.prototype.delete = function (id, tx) {
643
659
  var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.param);
644
660
  if (stmt.query) {
645
- var db = ctx ? ctx : this.db;
646
- return db.exec(stmt.query, stmt.params);
661
+ var db = tx ? tx : this.db;
662
+ return db.execute(stmt.query, stmt.params);
647
663
  }
648
664
  else {
649
- return Promise.resolve(-1);
665
+ throw new Error("cannot build delete query by id");
650
666
  }
651
667
  };
652
668
  return SqlRepository;
@@ -656,7 +672,7 @@ var Query = (function (_super) {
656
672
  __extends(Query, _super);
657
673
  function Query(query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total) {
658
674
  var _this = _super.call(this, query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
659
- var m = build_1.metadata(attributes);
675
+ var m = build_1.buildMetadata(attributes);
660
676
  _this.primaryKeys = m.keys;
661
677
  _this.map = m.map;
662
678
  _this.bools = m.bools;
@@ -699,7 +715,7 @@ var Query = (function (_super) {
699
715
  Query.prototype.exist = function (id, ctx) {
700
716
  var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
701
717
  var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
702
- if (stmt.query) {
718
+ if (!stmt.query) {
703
719
  throw new Error("cannot build query by id");
704
720
  }
705
721
  return this.query(stmt.query, stmt.params, undefined, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.6.2",
3
+ "version": "0.6.4",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -1,4 +1,4 @@
1
- import { metadata, param } from "./build"
1
+ import { buildMetadata, param } from "./build"
2
2
  import { Attribute, Attributes, Statement, StringMap } from "./metadata"
3
3
  import { buildSort as bs, buildDollarParam, buildMsSQLParam, buildOracleParam, buildQuery, LikeType } from "./query"
4
4
  import { buildFromQuery, oracle, SearchResult } from "./search"
@@ -56,7 +56,7 @@ export class SearchBuilder<T, S> {
56
56
  ) {
57
57
  if (attrs) {
58
58
  this.attrs = attrs
59
- const meta = metadata(attrs)
59
+ const meta = buildMetadata(attrs)
60
60
  this.map = meta.map
61
61
  this.bools = meta.bools
62
62
  this.primaryKeys = meta.keys
package/src/build.ts CHANGED
@@ -509,7 +509,7 @@ export function buildToUpdateBatch<T>(
509
509
  notSkipInvalid?: boolean,
510
510
  ): Statement[] {
511
511
  const sts: Statement[] = []
512
- const meta = metadata(attrs)
512
+ const meta = buildMetadata(attrs)
513
513
  if (!meta.keys || meta.keys.length === 0) {
514
514
  return sts
515
515
  }
@@ -670,8 +670,10 @@ export interface Metadata {
670
670
  map?: StringMap
671
671
  version?: string
672
672
  fields?: string[]
673
+ updatedAt?: string
674
+ createdAt?: string
673
675
  }
674
- export function metadata(attrs: Attributes): Metadata {
676
+ export function buildMetadata(attrs: Attributes): Metadata {
675
677
  const mp: StringMap = {}
676
678
  const ks = Object.keys(attrs)
677
679
  const ats: Attribute[] = []
@@ -694,6 +696,11 @@ export function metadata(attrs: Attributes): Metadata {
694
696
  if (attr.version) {
695
697
  m.version = k
696
698
  }
699
+ if (attr.updatedAt) {
700
+ m.updatedAt = k
701
+ } else if (attr.createdAt) {
702
+ m.createdAt = k
703
+ }
697
704
  const field = attr.column ? attr.column : k
698
705
  const s = field.toLowerCase()
699
706
  if (s !== k) {
package/src/metadata.ts CHANGED
@@ -41,15 +41,6 @@ export type DataType =
41
41
  | "times"
42
42
  export type Operator = "=" | "like" | "!=" | "<>" | ">" | ">=" | "<" | "<="
43
43
 
44
- export interface Model {
45
- name?: string
46
- attributes: Attributes
47
- source?: string
48
- table?: string
49
- collection?: string
50
- model?: any
51
- schema?: any
52
- }
53
44
  export interface Attribute {
54
45
  name?: string
55
46
  column?: string
@@ -65,6 +56,8 @@ export interface Attribute {
65
56
  ignored?: boolean
66
57
  true?: string | number
67
58
  false?: string | number
59
+ createdAt?: boolean
60
+ updatedAt?: boolean
68
61
  }
69
62
  export interface Attributes {
70
63
  [key: string]: Attribute
package/src/services.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { attributes, buildToDelete, buildToInsert, buildToUpdate, exist, metadata, select, version } from "./build"
1
+ import { attributes, buildToDelete, buildToInsert, buildToUpdate, exist, buildMetadata, select, version } from "./build"
2
2
  import { Attribute, Attributes, Statement, StringMap } from "./metadata"
3
3
  import { LikeType } from "./query"
4
4
  import { SearchBuilder } from "./SearchBuilder"
@@ -12,13 +12,12 @@ export interface Filter {
12
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
- q: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
16
- param: (i: number) => string,
15
+ db: DB,
17
16
  table: string,
18
17
  attrs: Attributes | string[],
19
18
  fromDB?: (v: T) => T,
20
19
  ): Load<T, ID> {
21
- const l = new SqlLoader<T, ID>(q, param, table, attrs, fromDB)
20
+ const l = new SqlLoader<T, ID>(db, table, attrs, fromDB)
22
21
  return l.load
23
22
  }
24
23
  export const useLoad = useGet
@@ -28,8 +27,7 @@ export class SqlLoader<T, ID> {
28
27
  attributes: Attributes
29
28
  bools?: Attribute[]
30
29
  constructor(
31
- protected query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
32
- protected param: (i: number) => string,
30
+ protected db: DB,
33
31
  protected table: string,
34
32
  attrs: Attributes | string[],
35
33
  protected fromDB?: (v: T) => T,
@@ -38,7 +36,7 @@ export class SqlLoader<T, ID> {
38
36
  this.primaryKeys = attributes(attrs)
39
37
  this.attributes = {} as any
40
38
  } else {
41
- const m = metadata(attrs)
39
+ const m = buildMetadata(attrs)
42
40
  this.attributes = attrs
43
41
  this.primaryKeys = m.keys
44
42
  this.map = m.map
@@ -54,18 +52,20 @@ export class SqlLoader<T, ID> {
54
52
  metadata?(): Attributes | undefined {
55
53
  return this.attributes
56
54
  }
57
- all(ctx?: any): Promise<T[]> {
55
+ all(tx?: Transaction): Promise<T[]> {
58
56
  const sql = `select * from ${this.table}`
59
- return this.query(sql, [], this.map, this.bools, ctx)
57
+ const db = tx ? tx: this.db
58
+ return db.query(sql, [], this.map, this.bools)
60
59
  }
61
- load(id: ID, ctx?: any): Promise<T | null> {
62
- const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
60
+ load(id: ID, tx?: Transaction): Promise<T | null> {
61
+ const stmt = select<ID>(id, this.table, this.primaryKeys, this.db.param)
63
62
  if (!stmt.query) {
64
63
  throw new Error("cannot build query by id")
65
64
  }
65
+ const db = tx ? tx: this.db
66
66
  const fn = this.fromDB
67
67
  if (fn) {
68
- return this.query<T>(stmt.query, stmt.params, this.map, this.bools, ctx).then((res) => {
68
+ return db.query<T>(stmt.query, stmt.params, this.map, this.bools).then((res) => {
69
69
  if (!res || res.length === 0) {
70
70
  return null
71
71
  } else {
@@ -74,16 +74,17 @@ export class SqlLoader<T, ID> {
74
74
  }
75
75
  })
76
76
  } else {
77
- return this.query<T>(stmt.query, stmt.params, this.map, this.bools, ctx).then((res) => (!res || res.length === 0 ? null : res[0]))
77
+ return db.query<T>(stmt.query, stmt.params, this.map, this.bools).then((res) => (!res || res.length === 0 ? null : res[0]))
78
78
  }
79
79
  }
80
- exist(id: ID, ctx?: any): Promise<boolean> {
80
+ exist(id: ID, tx?: Transaction): Promise<boolean> {
81
81
  const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
82
- const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
82
+ const stmt = exist<ID>(id, this.table, this.primaryKeys, this.db.param, field)
83
83
  if (!stmt.query) {
84
84
  throw new Error("cannot build query by id")
85
85
  }
86
- return this.query(stmt.query, stmt.params, undefined, undefined, ctx).then((res) => (!res || res.length === 0 ? false : true))
86
+ const db = tx ? tx: this.db
87
+ return db.query(stmt.query, stmt.params, undefined, undefined).then((res) => (!res || res.length === 0 ? false : true))
87
88
  }
88
89
  }
89
90
  // tslint:disable-next-line:max-classes-per-file
@@ -91,14 +92,14 @@ export class QueryRepository<T, ID> {
91
92
  constructor(protected db: Executor, protected table: string, protected attrs: Attributes, protected sort?: string, id?: string) {
92
93
  this.id = id && id.length > 0 ? id : "id"
93
94
  this.query = this.query.bind(this)
94
- const m = metadata(attrs)
95
+ const m = buildMetadata(attrs)
95
96
  this.map = m.map
96
97
  this.bools = m.bools
97
98
  }
98
99
  id: string
99
100
  map?: StringMap
100
101
  bools?: Attribute[]
101
- query(ids: ID[], ctx?: Transaction): Promise<T[]> {
102
+ query(ids: ID[], tx?: Transaction): Promise<T[]> {
102
103
  if (!ids || ids.length === 0) {
103
104
  return Promise.resolve([])
104
105
  }
@@ -111,7 +112,7 @@ export class QueryRepository<T, ID> {
111
112
  if (this.sort && this.sort.length > 0) {
112
113
  sql = sql + " order by " + this.sort
113
114
  }
114
- const db = ctx ? ctx : this.db
115
+ const db = tx ? tx: this.db
115
116
  return db.query<T>(sql, ids, this.map, this.bools)
116
117
  }
117
118
  }
@@ -119,14 +120,13 @@ export class QueryRepository<T, ID> {
119
120
  export interface Executor {
120
121
  driver: string
121
122
  param(i: number): string
122
- exec(sql: string, args?: any[], ctx?: any): Promise<number>
123
- execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>
123
+ execute(sql: string, args?: any[], ctx?: any): Promise<number>
124
+ executeBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>
124
125
  query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
125
126
  }
126
127
  export interface Transaction extends Executor {
127
128
  commit(): Promise<void>
128
129
  rollback(): Promise<void>
129
- end(): Promise<void>
130
130
  }
131
131
  export interface DB extends Executor {
132
132
  beginTransaction(): Promise<Transaction>
@@ -470,24 +470,34 @@ const getDurationInMilliseconds = (start: [number, number] | undefined) => {
470
470
  // tslint:disable-next-line:max-classes-per-file
471
471
  export class SqlWriter<T> {
472
472
  protected version?: string
473
+ protected createdAt?: string
474
+ protected updatedAt?: string
473
475
  constructor(protected db: Executor, protected table: string, protected attributes: Attributes, protected toDB?: (v: T) => T) {
474
- const x = version(attributes)
476
+ const x = buildMetadata(attributes)
475
477
  if (x) {
476
- this.version = x.name
478
+ this.version = x.version
479
+ this.createdAt = x.createdAt
480
+ this.updatedAt = x.updatedAt
477
481
  }
478
482
  this.create = this.create.bind(this)
479
483
  this.update = this.update.bind(this)
480
484
  this.patch = this.patch.bind(this)
481
485
  }
482
- create(obj: T, ctx?: Transaction): Promise<number> {
486
+ create(obj: T, tx?: Transaction): Promise<number> {
483
487
  let obj2 = obj
484
488
  if (this.toDB) {
485
489
  obj2 = this.toDB(obj)
486
490
  }
491
+ if (this.createdAt) {
492
+ (obj2 as any)[this.createdAt] = new Date()
493
+ }
494
+ if (this.updatedAt) {
495
+ (obj2 as any)[this.updatedAt] = new Date()
496
+ }
487
497
  const stmt = buildToInsert(obj2, this.table, this.attributes, this.db.param, this.version)
488
498
  if (stmt.query) {
489
- const db = ctx ? ctx : this.db
490
- return db.exec(stmt.query, stmt.params).catch((err) => {
499
+ const db = tx ? tx: this.db
500
+ return db.execute(stmt.query, stmt.params).catch((err) => {
491
501
  if (err && err.error === "duplicate") {
492
502
  return 0
493
503
  } else {
@@ -498,21 +508,27 @@ export class SqlWriter<T> {
498
508
  return Promise.resolve(-1)
499
509
  }
500
510
  }
501
- update(obj: T, ctx?: Transaction): Promise<number> {
511
+ update(obj: T, tx?: Transaction): Promise<number> {
502
512
  let obj2 = obj
503
513
  if (this.toDB) {
504
514
  obj2 = this.toDB(obj)
505
515
  }
516
+ if (this.createdAt) {
517
+ (obj2 as any)[this.createdAt] = new Date()
518
+ }
519
+ if (this.updatedAt) {
520
+ (obj2 as any)[this.updatedAt] = new Date()
521
+ }
506
522
  const stmt = buildToUpdate(obj2, this.table, this.attributes, this.db.param, this.version)
507
523
  if (stmt.query) {
508
- const db = ctx ? ctx : this.db
509
- return db.exec(stmt.query, stmt.params)
524
+ const db = tx ? tx: this.db
525
+ return db.execute(stmt.query, stmt.params)
510
526
  } else {
511
527
  return Promise.resolve(-1)
512
528
  }
513
529
  }
514
- patch(obj: Partial<T>, ctx?: any): Promise<number> {
515
- return this.update(obj as any, ctx)
530
+ patch(obj: Partial<T>, tx?: Transaction): Promise<number> {
531
+ return this.update(obj as any, tx)
516
532
  }
517
533
  }
518
534
  export class CRUDRepository<T, ID> extends SqlWriter<T> {
@@ -521,7 +537,7 @@ export class CRUDRepository<T, ID> extends SqlWriter<T> {
521
537
  protected bools?: Attribute[]
522
538
  constructor(db: Executor, table: string, attributes: Attributes, toDB?: (v: T) => T, protected fromDB?: (v: T) => T) {
523
539
  super(db, table, attributes, toDB)
524
- const m = metadata(attributes)
540
+ const m = buildMetadata(attributes)
525
541
  this.primaryKeys = m.keys
526
542
  this.map = m.map
527
543
  this.bools = m.bools
@@ -534,18 +550,18 @@ export class CRUDRepository<T, ID> extends SqlWriter<T> {
534
550
  metadata(): Attributes {
535
551
  return this.attributes
536
552
  }
537
- all(ctx?: Transaction): Promise<T[]> {
553
+ all(tx?: Transaction): Promise<T[]> {
538
554
  const sql = `select * from ${this.table}`
539
- const db = ctx ? ctx : this.db
555
+ const db = tx ? tx: this.db
540
556
  return db.query(sql, [], this.map, this.bools)
541
557
  }
542
- load(id: ID, ctx?: Transaction): Promise<T | null> {
558
+ load(id: ID, tx?: Transaction): Promise<T | null> {
543
559
  const stmt = select<ID>(id, this.table, this.primaryKeys, this.db.param)
544
560
  if (!stmt.query) {
545
561
  throw new Error("cannot build query by id")
546
562
  }
547
563
  const fn = this.fromDB
548
- const db = ctx ? ctx : this.db
564
+ const db = tx ? tx: this.db
549
565
  if (fn) {
550
566
  return db.query<T>(stmt.query, stmt.params, this.map, this.bools).then((res) => {
551
567
  if (!res || res.length === 0) {
@@ -559,22 +575,22 @@ export class CRUDRepository<T, ID> extends SqlWriter<T> {
559
575
  return db.query<T>(stmt.query, stmt.params, this.map, this.bools).then((res) => (!res || res.length === 0 ? null : res[0]))
560
576
  }
561
577
  }
562
- exist(id: ID, ctx?: Transaction): Promise<boolean> {
578
+ exist(id: ID, tx?: Transaction): Promise<boolean> {
563
579
  const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
564
580
  const stmt = exist<ID>(id, this.table, this.primaryKeys, this.db.param, field)
565
581
  if (!stmt.query) {
566
582
  throw new Error("cannot build query by id")
567
583
  }
568
- const db = ctx ? ctx : this.db
584
+ const db = tx ? tx: this.db
569
585
  return db.query(stmt.query, stmt.params).then((res) => (!res || res.length === 0 ? false : true))
570
586
  }
571
- delete(id: ID, ctx?: Transaction): Promise<number> {
587
+ delete(id: ID, tx?: Transaction): Promise<number> {
572
588
  const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.db.param)
573
589
  if (stmt.query) {
574
- const db = ctx ? ctx : this.db
575
- return db.exec(stmt.query, stmt.params)
590
+ const db = tx ? tx: this.db
591
+ return db.execute(stmt.query, stmt.params)
576
592
  } else {
577
- return Promise.resolve(-1)
593
+ throw new Error("cannot build delete query by id")
578
594
  }
579
595
  }
580
596
  }
@@ -615,15 +631,15 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
615
631
  this.update = this.update.bind(this)
616
632
  this.patch = this.patch.bind(this)
617
633
  }
618
- create(obj: T, ctx?: Transaction): Promise<number> {
634
+ create(obj: T, tx?: Transaction): Promise<number> {
619
635
  let obj2 = obj
620
636
  if (this.toDB) {
621
637
  obj2 = this.toDB(obj)
622
638
  }
623
639
  const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version)
624
640
  if (stmt.query) {
625
- const db = ctx ? ctx : this.db
626
- return db.exec(stmt.query, stmt.params).catch((err) => {
641
+ const db = tx ? tx: this.db
642
+ return db.execute(stmt.query, stmt.params).catch((err) => {
627
643
  if (err && err.error === "duplicate") {
628
644
  return 0
629
645
  } else {
@@ -634,21 +650,21 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
634
650
  return Promise.resolve(-1)
635
651
  }
636
652
  }
637
- update(obj: T, ctx?: Transaction): Promise<number> {
653
+ update(obj: T, tx?: Transaction): Promise<number> {
638
654
  let obj2 = obj
639
655
  if (this.toDB) {
640
656
  obj2 = this.toDB(obj)
641
657
  }
642
658
  const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version)
643
659
  if (stmt.query) {
644
- const db = ctx ? ctx : this.db
645
- return db.exec(stmt.query, stmt.params)
660
+ const db = tx ? tx: this.db
661
+ return db.execute(stmt.query, stmt.params)
646
662
  } else {
647
663
  return Promise.resolve(-1)
648
664
  }
649
665
  }
650
- patch(obj: Partial<T>, ctx?: Transaction): Promise<number> {
651
- return this.update(obj as any, ctx)
666
+ patch(obj: Partial<T>, tx?: Transaction): Promise<number> {
667
+ return this.update(obj as any, tx)
652
668
  }
653
669
  }
654
670
  // tslint:disable-next-line:max-classes-per-file
@@ -688,18 +704,18 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
688
704
  metadata(): Attributes {
689
705
  return this.attributes
690
706
  }
691
- all(ctx?: Transaction): Promise<T[]> {
707
+ all(tx?: Transaction): Promise<T[]> {
692
708
  const sql = `select * from ${this.table}`
693
- const db = ctx ? ctx : this.db
709
+ const db = tx ? tx: this.db
694
710
  return db.query(sql, [], this.map, this.bools)
695
711
  }
696
- load(id: ID, ctx?: Transaction): Promise<T | null> {
712
+ load(id: ID, tx?: Transaction): Promise<T | null> {
697
713
  const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
698
- if (!stmt) {
714
+ if (!stmt.query) {
699
715
  throw new Error("cannot build query by id")
700
716
  }
701
717
  const fn = this.fromDB
702
- const db = ctx ? ctx : this.db
718
+ const db = tx ? tx: this.db
703
719
  if (fn) {
704
720
  return db.query<T>(stmt.query, stmt.params, this.map, this.bools).then((res) => {
705
721
  if (!res || res.length === 0) {
@@ -713,22 +729,22 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
713
729
  return db.query<T>(stmt.query, stmt.params, this.map, this.bools).then((res) => (!res || res.length === 0 ? null : res[0]))
714
730
  }
715
731
  }
716
- exist(id: ID, ctx?: Transaction): Promise<boolean> {
732
+ exist(id: ID, tx?: Transaction): Promise<boolean> {
717
733
  const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
718
734
  const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
719
- if (!stmt) {
735
+ if (!stmt.query) {
720
736
  throw new Error("cannot build query by id")
721
737
  }
722
- const db = ctx ? ctx : this.db
738
+ const db = tx ? tx: this.db
723
739
  return db.query(stmt.query, stmt.params).then((res) => (!res || res.length === 0 ? false : true))
724
740
  }
725
- delete(id: ID, ctx?: Transaction): Promise<number> {
741
+ delete(id: ID, tx?: Transaction): Promise<number> {
726
742
  const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param)
727
743
  if (stmt.query) {
728
- const db = ctx ? ctx : this.db
729
- return db.exec(stmt.query, stmt.params)
744
+ const db = tx ? tx: this.db
745
+ return db.execute(stmt.query, stmt.params)
730
746
  } else {
731
- return Promise.resolve(-1)
747
+ throw new Error("cannot build delete query by id")
732
748
  }
733
749
  }
734
750
  }
@@ -764,7 +780,7 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
764
780
  total?: string,
765
781
  ) {
766
782
  super(query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total)
767
- const m = metadata(attributes)
783
+ const m = buildMetadata(attributes)
768
784
  this.primaryKeys = m.keys
769
785
  this.map = m.map
770
786
  this.bools = m.bools
@@ -804,7 +820,7 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
804
820
  exist(id: ID, ctx?: any): Promise<boolean> {
805
821
  const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
806
822
  const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
807
- if (stmt.query) {
823
+ if (!stmt.query) {
808
824
  throw new Error("cannot build query by id")
809
825
  }
810
826
  return this.query(stmt.query, stmt.params, undefined, undefined, ctx).then((res) => (!res || res.length === 0 ? false : true))