query-core 0.6.3 → 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,16 +8,15 @@ 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) {
19
18
  this.attrs = attrs;
20
- var meta = build_1.metadata(attrs);
19
+ var meta = build_1.buildMetadata(attrs);
21
20
  this.map = meta.map;
22
21
  this.bools = meta.bools;
23
22
  this.primaryKeys = meta.keys;
@@ -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/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/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
@@ -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,20 +71,24 @@ 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
  }());
86
88
  exports.SqlLoader = SqlLoader;
89
+ exports.SqlViewRepository = SqlLoader;
90
+ exports.SqlLoadService = SqlLoader;
91
+ exports.SqlViewServic = SqlLoader;
87
92
  var QueryRepository = (function () {
88
93
  function QueryRepository(db, table, attrs, sort, id) {
89
94
  this.db = db;
@@ -92,11 +97,11 @@ var QueryRepository = (function () {
92
97
  this.sort = sort;
93
98
  this.id = id && id.length > 0 ? id : "id";
94
99
  this.query = this.query.bind(this);
95
- var m = build_1.metadata(attrs);
100
+ var m = build_1.buildMetadata(attrs);
96
101
  this.map = m.map;
97
102
  this.bools = m.bools;
98
103
  }
99
- QueryRepository.prototype.query = function (ids, ctx) {
104
+ QueryRepository.prototype.query = function (ids, tx) {
100
105
  if (!ids || ids.length === 0) {
101
106
  return Promise.resolve([]);
102
107
  }
@@ -109,7 +114,7 @@ var QueryRepository = (function () {
109
114
  if (this.sort && this.sort.length > 0) {
110
115
  sql = sql + " order by " + this.sort;
111
116
  }
112
- var db = ctx ? ctx : this.db;
117
+ var db = tx ? tx : this.db;
113
118
  return db.query(sql, ids, this.map, this.bools);
114
119
  };
115
120
  return QueryRepository;
@@ -152,21 +157,21 @@ var LogManager = (function () {
152
157
  this.log = lg;
153
158
  this.error = err;
154
159
  this.param = this.param.bind(this);
155
- this.exec = this.exec.bind(this);
156
- this.execBatch = this.execBatch.bind(this);
160
+ this.execute = this.execute.bind(this);
161
+ this.executeBatch = this.executeBatch.bind(this);
157
162
  this.query = this.query.bind(this);
158
163
  this.queryOne = this.queryOne.bind(this);
159
- this.execScalar = this.execScalar.bind(this);
164
+ this.executeScalar = this.executeScalar.bind(this);
160
165
  this.count = this.count.bind(this);
161
166
  }
162
167
  LogManager.prototype.param = function (i) {
163
168
  return this.db.param(i);
164
169
  };
165
- LogManager.prototype.exec = function (sql, args, ctx) {
170
+ LogManager.prototype.execute = function (sql, args, ctx) {
166
171
  var _this = this;
167
172
  var t1 = new Date();
168
173
  return this.db
169
- .exec(sql, args, ctx)
174
+ .execute(sql, args, ctx)
170
175
  .then(function (v) {
171
176
  setTimeout(function () {
172
177
  if (_this.log) {
@@ -197,11 +202,11 @@ var LogManager = (function () {
197
202
  throw er;
198
203
  });
199
204
  };
200
- LogManager.prototype.execBatch = function (statements, firstSuccess, ctx) {
205
+ LogManager.prototype.executeBatch = function (statements, firstSuccess, ctx) {
201
206
  var _this = this;
202
207
  var t1 = new Date();
203
208
  return this.db
204
- .execBatch(statements, firstSuccess, ctx)
209
+ .executeBatch(statements, firstSuccess, ctx)
205
210
  .then(function (v) {
206
211
  setTimeout(function () {
207
212
  if (_this.log) {
@@ -310,11 +315,11 @@ var LogManager = (function () {
310
315
  throw er;
311
316
  });
312
317
  };
313
- LogManager.prototype.execScalar = function (sql, args, ctx) {
318
+ LogManager.prototype.executeScalar = function (sql, args, ctx) {
314
319
  var _this = this;
315
320
  var t1 = new Date();
316
321
  return this.db
317
- .execScalar(sql, args, ctx)
322
+ .executeScalar(sql, args, ctx)
318
323
  .then(function (v) {
319
324
  setTimeout(function () {
320
325
  if (_this.log) {
@@ -413,23 +418,32 @@ var SqlWriter = (function () {
413
418
  this.table = table;
414
419
  this.attributes = attributes;
415
420
  this.toDB = toDB;
416
- var x = build_1.version(attributes);
417
- if (x) {
418
- this.version = x.name;
419
- }
421
+ var x = build_1.buildMetadata(attributes);
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;
420
428
  this.create = this.create.bind(this);
421
429
  this.update = this.update.bind(this);
422
430
  this.patch = this.patch.bind(this);
423
431
  }
424
- SqlWriter.prototype.create = function (obj, ctx) {
432
+ SqlWriter.prototype.create = function (obj, tx) {
425
433
  var obj2 = obj;
426
434
  if (this.toDB) {
427
435
  obj2 = this.toDB(obj);
428
436
  }
437
+ if (this.createdAt) {
438
+ obj2[this.createdAt] = new Date();
439
+ }
440
+ if (this.updatedAt) {
441
+ obj2[this.updatedAt] = new Date();
442
+ }
429
443
  var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.db.param, this.version);
430
444
  if (stmt.query) {
431
- var db = ctx ? ctx : this.db;
432
- return db.exec(stmt.query, stmt.params).catch(function (err) {
445
+ var db = tx ? tx : this.db;
446
+ return db.execute(stmt.query, stmt.params).catch(function (err) {
433
447
  if (err && err.error === "duplicate") {
434
448
  return 0;
435
449
  }
@@ -442,22 +456,25 @@ var SqlWriter = (function () {
442
456
  return Promise.resolve(-1);
443
457
  }
444
458
  };
445
- SqlWriter.prototype.update = function (obj, ctx) {
459
+ SqlWriter.prototype.update = function (obj, tx) {
446
460
  var obj2 = obj;
447
461
  if (this.toDB) {
448
462
  obj2 = this.toDB(obj);
449
463
  }
464
+ if (this.updatedAt) {
465
+ obj2[this.updatedAt] = new Date();
466
+ }
450
467
  var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.db.param, this.version);
451
468
  if (stmt.query) {
452
- var db = ctx ? ctx : this.db;
453
- return db.exec(stmt.query, stmt.params);
469
+ var db = tx ? tx : this.db;
470
+ return db.execute(stmt.query, stmt.params);
454
471
  }
455
472
  else {
456
473
  return Promise.resolve(-1);
457
474
  }
458
475
  };
459
- SqlWriter.prototype.patch = function (obj, ctx) {
460
- return this.update(obj, ctx);
476
+ SqlWriter.prototype.patch = function (obj, tx) {
477
+ return this.update(obj, tx);
461
478
  };
462
479
  return SqlWriter;
463
480
  }());
@@ -467,10 +484,6 @@ var CRUDRepository = (function (_super) {
467
484
  function CRUDRepository(db, table, attributes, toDB, fromDB) {
468
485
  var _this = _super.call(this, db, table, attributes, toDB) || this;
469
486
  _this.fromDB = fromDB;
470
- var m = build_1.metadata(attributes);
471
- _this.primaryKeys = m.keys;
472
- _this.map = m.map;
473
- _this.bools = m.bools;
474
487
  _this.metadata = _this.metadata.bind(_this);
475
488
  _this.all = _this.all.bind(_this);
476
489
  _this.load = _this.load.bind(_this);
@@ -481,18 +494,18 @@ var CRUDRepository = (function (_super) {
481
494
  CRUDRepository.prototype.metadata = function () {
482
495
  return this.attributes;
483
496
  };
484
- CRUDRepository.prototype.all = function (ctx) {
497
+ CRUDRepository.prototype.all = function (tx) {
485
498
  var sql = "select * from " + this.table;
486
- var db = ctx ? ctx : this.db;
499
+ var db = tx ? tx : this.db;
487
500
  return db.query(sql, [], this.map, this.bools);
488
501
  };
489
- CRUDRepository.prototype.load = function (id, ctx) {
502
+ CRUDRepository.prototype.load = function (id, tx) {
490
503
  var stmt = build_1.select(id, this.table, this.primaryKeys, this.db.param);
491
504
  if (!stmt.query) {
492
505
  throw new Error("cannot build query by id");
493
506
  }
494
507
  var fn = this.fromDB;
495
- var db = ctx ? ctx : this.db;
508
+ var db = tx ? tx : this.db;
496
509
  if (fn) {
497
510
  return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) {
498
511
  if (!res || res.length === 0) {
@@ -508,20 +521,20 @@ var CRUDRepository = (function (_super) {
508
521
  return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
509
522
  }
510
523
  };
511
- CRUDRepository.prototype.exist = function (id, ctx) {
524
+ CRUDRepository.prototype.exist = function (id, tx) {
512
525
  var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
513
526
  var stmt = build_1.exist(id, this.table, this.primaryKeys, this.db.param, field);
514
527
  if (!stmt.query) {
515
528
  throw new Error("cannot build query by id");
516
529
  }
517
- var db = ctx ? ctx : this.db;
530
+ var db = tx ? tx : this.db;
518
531
  return db.query(stmt.query, stmt.params).then(function (res) { return (!res || res.length === 0 ? false : true); });
519
532
  };
520
- CRUDRepository.prototype.delete = function (id, ctx) {
533
+ CRUDRepository.prototype.delete = function (id, tx) {
521
534
  var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.db.param);
522
535
  if (stmt.query) {
523
- var db = ctx ? ctx : this.db;
524
- return db.exec(stmt.query, stmt.params);
536
+ var db = tx ? tx : this.db;
537
+ return db.execute(stmt.query, stmt.params);
525
538
  }
526
539
  else {
527
540
  throw new Error("cannot build delete query by id");
@@ -530,10 +543,12 @@ var CRUDRepository = (function (_super) {
530
543
  return CRUDRepository;
531
544
  }(SqlWriter));
532
545
  exports.CRUDRepository = CRUDRepository;
546
+ exports.GenericRepository = CRUDRepository;
547
+ exports.SqlGenericRepository = CRUDRepository;
533
548
  var SqlSearchWriter = (function (_super) {
534
549
  __extends(SqlSearchWriter, _super);
535
- function SqlSearchWriter(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
536
- 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;
537
552
  _this.db = db;
538
553
  _this.attributes = attributes;
539
554
  _this.toDB = toDB;
@@ -546,15 +561,15 @@ var SqlSearchWriter = (function (_super) {
546
561
  _this.patch = _this.patch.bind(_this);
547
562
  return _this;
548
563
  }
549
- SqlSearchWriter.prototype.create = function (obj, ctx) {
564
+ SqlSearchWriter.prototype.create = function (obj, tx) {
550
565
  var obj2 = obj;
551
566
  if (this.toDB) {
552
567
  obj2 = this.toDB(obj);
553
568
  }
554
- 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);
555
570
  if (stmt.query) {
556
- var db = ctx ? ctx : this.db;
557
- return db.exec(stmt.query, stmt.params).catch(function (err) {
571
+ var db = tx ? tx : this.db;
572
+ return db.execute(stmt.query, stmt.params).catch(function (err) {
558
573
  if (err && err.error === "duplicate") {
559
574
  return 0;
560
575
  }
@@ -567,22 +582,22 @@ var SqlSearchWriter = (function (_super) {
567
582
  return Promise.resolve(-1);
568
583
  }
569
584
  };
570
- SqlSearchWriter.prototype.update = function (obj, ctx) {
585
+ SqlSearchWriter.prototype.update = function (obj, tx) {
571
586
  var obj2 = obj;
572
587
  if (this.toDB) {
573
588
  obj2 = this.toDB(obj);
574
589
  }
575
- 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);
576
591
  if (stmt.query) {
577
- var db = ctx ? ctx : this.db;
578
- return db.exec(stmt.query, stmt.params);
592
+ var db = tx ? tx : this.db;
593
+ return db.execute(stmt.query, stmt.params);
579
594
  }
580
595
  else {
581
596
  return Promise.resolve(-1);
582
597
  }
583
598
  };
584
- SqlSearchWriter.prototype.patch = function (obj, ctx) {
585
- return this.update(obj, ctx);
599
+ SqlSearchWriter.prototype.patch = function (obj, tx) {
600
+ return this.update(obj, tx);
586
601
  };
587
602
  return SqlSearchWriter;
588
603
  }(SearchBuilder_1.SearchBuilder));
@@ -590,7 +605,7 @@ exports.SqlSearchWriter = SqlSearchWriter;
590
605
  var SqlRepository = (function (_super) {
591
606
  __extends(SqlRepository, _super);
592
607
  function SqlRepository(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
593
- 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;
594
609
  _this.attributes = attributes;
595
610
  _this.toDB = toDB;
596
611
  _this.metadata = _this.metadata.bind(_this);
@@ -603,18 +618,18 @@ var SqlRepository = (function (_super) {
603
618
  SqlRepository.prototype.metadata = function () {
604
619
  return this.attributes;
605
620
  };
606
- SqlRepository.prototype.all = function (ctx) {
621
+ SqlRepository.prototype.all = function (tx) {
607
622
  var sql = "select * from " + this.table;
608
- var db = ctx ? ctx : this.db;
623
+ var db = tx ? tx : this.db;
609
624
  return db.query(sql, [], this.map, this.bools);
610
625
  };
611
- SqlRepository.prototype.load = function (id, ctx) {
612
- var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
626
+ SqlRepository.prototype.load = function (id, tx) {
627
+ var stmt = build_1.select(id, this.table, this.primaryKeys, this.db.param);
613
628
  if (!stmt.query) {
614
629
  throw new Error("cannot build query by id");
615
630
  }
616
631
  var fn = this.fromDB;
617
- var db = ctx ? ctx : this.db;
632
+ var db = tx ? tx : this.db;
618
633
  if (fn) {
619
634
  return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) {
620
635
  if (!res || res.length === 0) {
@@ -630,20 +645,20 @@ var SqlRepository = (function (_super) {
630
645
  return db.query(stmt.query, stmt.params, this.map, this.bools).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
631
646
  }
632
647
  };
633
- SqlRepository.prototype.exist = function (id, ctx) {
648
+ SqlRepository.prototype.exist = function (id, tx) {
634
649
  var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
635
- 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);
636
651
  if (!stmt.query) {
637
652
  throw new Error("cannot build query by id");
638
653
  }
639
- var db = ctx ? ctx : this.db;
654
+ var db = tx ? tx : this.db;
640
655
  return db.query(stmt.query, stmt.params).then(function (res) { return (!res || res.length === 0 ? false : true); });
641
656
  };
642
- SqlRepository.prototype.delete = function (id, ctx) {
643
- var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.param);
657
+ SqlRepository.prototype.delete = function (id, tx) {
658
+ var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.db.param);
644
659
  if (stmt.query) {
645
- var db = ctx ? ctx : this.db;
646
- return db.exec(stmt.query, stmt.params);
660
+ var db = tx ? tx : this.db;
661
+ return db.execute(stmt.query, stmt.params);
647
662
  }
648
663
  else {
649
664
  throw new Error("cannot build delete query by id");
@@ -652,11 +667,12 @@ var SqlRepository = (function (_super) {
652
667
  return SqlRepository;
653
668
  }(SqlSearchWriter));
654
669
  exports.SqlRepository = SqlRepository;
670
+ exports.Repository = SqlRepository;
655
671
  var Query = (function (_super) {
656
672
  __extends(Query, _super);
657
- function Query(query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total) {
658
- 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);
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
+ var m = build_1.buildMetadata(attributes);
660
676
  _this.primaryKeys = m.keys;
661
677
  _this.map = m.map;
662
678
  _this.bools = m.bools;
@@ -671,18 +687,20 @@ var Query = (function (_super) {
671
687
  Query.prototype.metadata = function () {
672
688
  return this.attrs;
673
689
  };
674
- Query.prototype.all = function (ctx) {
690
+ Query.prototype.all = function (tx) {
675
691
  var sql = "select * from " + this.table;
676
- 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);
677
694
  };
678
- Query.prototype.load = function (id, ctx) {
679
- 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);
680
697
  if (!stmt.query) {
681
698
  throw new Error("cannot build query by id");
682
699
  }
700
+ var db = tx ? tx : this.db;
683
701
  var fn = this.fromDB;
684
702
  if (fn) {
685
- 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) {
686
704
  if (!res || res.length === 0) {
687
705
  return null;
688
706
  }
@@ -693,16 +711,17 @@ var Query = (function (_super) {
693
711
  });
694
712
  }
695
713
  else {
696
- 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]); });
697
715
  }
698
716
  };
699
- Query.prototype.exist = function (id, ctx) {
717
+ Query.prototype.exist = function (id, tx) {
700
718
  var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
701
- 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);
702
720
  if (!stmt.query) {
703
721
  throw new Error("cannot build query by id");
704
722
  }
705
- 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); });
706
725
  };
707
726
  return Query;
708
727
  }(SearchBuilder_1.SearchBuilder));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",