query-core 0.5.2 → 0.6.0

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.
@@ -1,17 +1,4 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- extendStatics(d, b);
11
- function __() { this.constructor = d; }
12
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
- };
14
- })();
15
2
  Object.defineProperty(exports, "__esModule", { value: true });
16
3
  var build_1 = require("./build");
17
4
  var query_1 = require("./query");
@@ -93,174 +80,3 @@ var SearchBuilder = (function () {
93
80
  return SearchBuilder;
94
81
  }());
95
82
  exports.SearchBuilder = SearchBuilder;
96
- var SqlSearchWriter = (function (_super) {
97
- __extends(SqlSearchWriter, _super);
98
- function SqlSearchWriter(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
99
- var _this = _super.call(this, db.query, table, attributes, db.driver, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
100
- _this.attributes = attributes;
101
- _this.toDB = toDB;
102
- _this.exec = db.exec;
103
- var x = build_1.version(attributes);
104
- if (x) {
105
- _this.version = x.name;
106
- }
107
- _this.create = _this.create.bind(_this);
108
- _this.update = _this.update.bind(_this);
109
- _this.patch = _this.patch.bind(_this);
110
- return _this;
111
- }
112
- SqlSearchWriter.prototype.create = function (obj, ctx) {
113
- var obj2 = obj;
114
- if (this.toDB) {
115
- obj2 = this.toDB(obj);
116
- }
117
- var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
118
- if (stmt) {
119
- return this.exec(stmt.query, stmt.params, ctx).catch(function (err) {
120
- if (err && err.error === "duplicate") {
121
- return 0;
122
- }
123
- else {
124
- throw err;
125
- }
126
- });
127
- }
128
- else {
129
- return Promise.resolve(0);
130
- }
131
- };
132
- SqlSearchWriter.prototype.update = function (obj, ctx) {
133
- var obj2 = obj;
134
- if (this.toDB) {
135
- obj2 = this.toDB(obj);
136
- }
137
- var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
138
- if (stmt) {
139
- return this.exec(stmt.query, stmt.params, ctx);
140
- }
141
- else {
142
- return Promise.resolve(0);
143
- }
144
- };
145
- SqlSearchWriter.prototype.patch = function (obj, ctx) {
146
- return this.update(obj, ctx);
147
- };
148
- return SqlSearchWriter;
149
- }(SearchBuilder));
150
- exports.SqlSearchWriter = SqlSearchWriter;
151
- var SqlRepository = (function (_super) {
152
- __extends(SqlRepository, _super);
153
- function SqlRepository(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
154
- var _this = _super.call(this, db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
155
- _this.attributes = attributes;
156
- _this.toDB = toDB;
157
- _this.metadata = _this.metadata.bind(_this);
158
- _this.all = _this.all.bind(_this);
159
- _this.load = _this.load.bind(_this);
160
- _this.exist = _this.exist.bind(_this);
161
- _this.delete = _this.delete.bind(_this);
162
- return _this;
163
- }
164
- SqlRepository.prototype.metadata = function () {
165
- return this.attributes;
166
- };
167
- SqlRepository.prototype.all = function () {
168
- var sql = "select * from " + this.table;
169
- return this.query(sql, [], this.map);
170
- };
171
- SqlRepository.prototype.load = function (id, ctx) {
172
- var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
173
- if (!stmt) {
174
- throw new Error("cannot build query by id");
175
- }
176
- var fn = this.fromDB;
177
- if (fn) {
178
- return this.query(stmt.query, stmt.params, this.map, ctx).then(function (res) {
179
- if (!res || res.length === 0) {
180
- return null;
181
- }
182
- else {
183
- var obj = res[0];
184
- return fn(obj);
185
- }
186
- });
187
- }
188
- else {
189
- return this.query(stmt.query, stmt.params, this.map).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
190
- }
191
- };
192
- SqlRepository.prototype.exist = function (id, ctx) {
193
- var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
194
- var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
195
- if (!stmt) {
196
- throw new Error("cannot build query by id");
197
- }
198
- return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
199
- };
200
- SqlRepository.prototype.delete = function (id, ctx) {
201
- var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.param);
202
- if (stmt) {
203
- return this.exec(stmt.query, stmt.params, ctx);
204
- }
205
- else {
206
- return Promise.resolve(0);
207
- }
208
- };
209
- return SqlRepository;
210
- }(SqlSearchWriter));
211
- exports.SqlRepository = SqlRepository;
212
- var Query = (function (_super) {
213
- __extends(Query, _super);
214
- function Query(query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total) {
215
- var _this = _super.call(this, query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
216
- var m = build_1.metadata(attributes);
217
- _this.primaryKeys = m.keys;
218
- _this.map = m.map;
219
- _this.bools = m.bools;
220
- if (_this.metadata) {
221
- _this.metadata = _this.metadata.bind(_this);
222
- }
223
- _this.all = _this.all.bind(_this);
224
- _this.load = _this.load.bind(_this);
225
- _this.exist = _this.exist.bind(_this);
226
- return _this;
227
- }
228
- Query.prototype.metadata = function () {
229
- return this.attrs;
230
- };
231
- Query.prototype.all = function () {
232
- var sql = "select * from " + this.table;
233
- return this.query(sql, [], this.map);
234
- };
235
- Query.prototype.load = function (id, ctx) {
236
- var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
237
- if (!stmt) {
238
- throw new Error("cannot build query by id");
239
- }
240
- var fn = this.fromDB;
241
- if (fn) {
242
- return this.query(stmt.query, stmt.params, this.map, ctx).then(function (res) {
243
- if (!res || res.length === 0) {
244
- return null;
245
- }
246
- else {
247
- var obj = res[0];
248
- return fn(obj);
249
- }
250
- });
251
- }
252
- else {
253
- return this.query(stmt.query, stmt.params, this.map).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
254
- }
255
- };
256
- Query.prototype.exist = function (id, ctx) {
257
- var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
258
- var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
259
- if (!stmt) {
260
- throw new Error("cannot build query by id");
261
- }
262
- return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
263
- };
264
- return Query;
265
- }(SearchBuilder));
266
- exports.Query = Query;
package/lib/batch.js CHANGED
@@ -24,7 +24,7 @@ var SqlInserter = (function () {
24
24
  obj2 = this.map(obj);
25
25
  }
26
26
  var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
27
- if (stmt) {
27
+ if (stmt.query.length > 0) {
28
28
  if (this.oneIfSuccess) {
29
29
  return this.exec(stmt.query, stmt.params).then(function (ct) { return (ct > 0 ? 1 : 0); });
30
30
  }
@@ -62,7 +62,7 @@ var SqlUpdater = (function () {
62
62
  obj2 = this.map(obj);
63
63
  }
64
64
  var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
65
- if (stmt) {
65
+ if (stmt.query.length > 0) {
66
66
  if (this.oneIfSuccess) {
67
67
  return this.exec(stmt.query, stmt.params).then(function (ct) { return (ct > 0 ? 1 : 0); });
68
68
  }
@@ -105,7 +105,7 @@ var SqlBatchInserter = (function () {
105
105
  }
106
106
  }
107
107
  var stmt = build_1.buildToInsertBatch(list, this.table, this.attributes, this.param, this.version);
108
- if (stmt) {
108
+ if (stmt.query.length > 0) {
109
109
  if (this.oneIfSuccess) {
110
110
  return this.exec(stmt.query, stmt.params).then(function (ct) { return objs.length; });
111
111
  }
@@ -210,7 +210,7 @@ var StreamInserter = (function () {
210
210
  else {
211
211
  var total_1 = this.list.length;
212
212
  var stmt = build_1.buildToInsertBatch(this.list, this.table, this.attributes, this.param, this.version);
213
- if (stmt) {
213
+ if (stmt.query.length > 0) {
214
214
  return this.exec(stmt.query, stmt.params).then(function (r) {
215
215
  _this.list = [];
216
216
  return total_1;
package/lib/build.js CHANGED
@@ -47,7 +47,7 @@ function select(obj, table, ks, buildParam, i) {
47
47
  return { query: query, params: args };
48
48
  }
49
49
  else {
50
- return undefined;
50
+ return { query: "", params: [] };
51
51
  }
52
52
  }
53
53
  exports.select = select;
@@ -84,7 +84,7 @@ function exist(obj, table, ks, buildParam, col, i) {
84
84
  return { query: query, params: args };
85
85
  }
86
86
  else {
87
- return undefined;
87
+ return { query: "", params: [] };
88
88
  }
89
89
  }
90
90
  exports.exist = exist;
@@ -118,13 +118,13 @@ function buildToDelete(obj, table, ks, buildParam, i) {
118
118
  return { query: query, params: args };
119
119
  }
120
120
  else {
121
- return undefined;
121
+ return { query: "", params: [] };
122
122
  }
123
123
  }
124
124
  exports.buildToDelete = buildToDelete;
125
125
  function insert(exec, obj, table, attrs, buildParam, ver, i) {
126
126
  var stm = buildToInsert(obj, table, attrs, buildParam, ver, i);
127
- if (!stm) {
127
+ if (stm.query.length === 0) {
128
128
  return Promise.resolve(0);
129
129
  }
130
130
  else {
@@ -207,7 +207,7 @@ function buildToInsert(obj, table, attrs, buildParam, ver, i) {
207
207
  values.push("" + 1);
208
208
  }
209
209
  if (cols.length === 0) {
210
- return undefined;
210
+ return { query: "", params: args };
211
211
  }
212
212
  else {
213
213
  var query = "insert into " + table + "(" + cols.join(",") + ")values(" + values.join(",") + ")";
@@ -217,7 +217,7 @@ function buildToInsert(obj, table, attrs, buildParam, ver, i) {
217
217
  exports.buildToInsert = buildToInsert;
218
218
  function insertBatch(exec, objs, table, attrs, buildParam, ver, i) {
219
219
  var stm = buildToInsertBatch(objs, table, attrs, buildParam, ver, i);
220
- if (!stm) {
220
+ if (stm.query.length === 0) {
221
221
  return Promise.resolve(0);
222
222
  }
223
223
  else {
@@ -379,7 +379,7 @@ function buildToInsertBatch(objs, table, attrs, buildParam, ver, i) {
379
379
  }
380
380
  if (cols.length === 0) {
381
381
  if (notSkipInvalid) {
382
- return undefined;
382
+ return { query: "", params: args };
383
383
  }
384
384
  }
385
385
  else {
@@ -388,7 +388,7 @@ function buildToInsertBatch(objs, table, attrs, buildParam, ver, i) {
388
388
  }
389
389
  }
390
390
  if (rows.length === 0) {
391
- return undefined;
391
+ return { query: "", params: args };
392
392
  }
393
393
  var query = "insert all " + rows.join(" ") + " select * from dual";
394
394
  return { query: query, params: args };
@@ -397,7 +397,7 @@ function buildToInsertBatch(objs, table, attrs, buildParam, ver, i) {
397
397
  exports.buildToInsertBatch = buildToInsertBatch;
398
398
  function update(exec, obj, table, attrs, buildParam, ver, i) {
399
399
  var stm = buildToUpdate(obj, table, attrs, buildParam, ver, i);
400
- if (!stm) {
400
+ if (stm.query.length === 0) {
401
401
  return Promise.resolve(0);
402
402
  }
403
403
  else {
@@ -477,7 +477,7 @@ function buildToUpdate(obj, table, attrs, buildParam, ver, i) {
477
477
  var na = pk.name ? pk.name : "";
478
478
  var v = o[na];
479
479
  if (!v) {
480
- return undefined;
480
+ return { query: "", params: args };
481
481
  }
482
482
  else {
483
483
  var attr = attrs[na];
@@ -523,7 +523,7 @@ function buildToUpdate(obj, table, attrs, buildParam, ver, i) {
523
523
  }
524
524
  }
525
525
  if (colSet.length === 0 || colQuery.length === 0) {
526
- return undefined;
526
+ return { query: "", params: args };
527
527
  }
528
528
  else {
529
529
  var query = "update " + table + " set " + colSet.join(",") + " where " + colQuery.join(" and ");
@@ -533,7 +533,7 @@ function buildToUpdate(obj, table, attrs, buildParam, ver, i) {
533
533
  exports.buildToUpdate = buildToUpdate;
534
534
  function updateBatch(exec, objs, table, attrs, buildParam, notSkipInvalid) {
535
535
  var stmts = buildToUpdateBatch(objs, table, attrs, buildParam, notSkipInvalid);
536
- if (!stmts || stmts.length === 0) {
536
+ if (stmts.length === 0) {
537
537
  return Promise.resolve(0);
538
538
  }
539
539
  else {
@@ -545,7 +545,7 @@ function buildToUpdateBatch(objs, table, attrs, buildParam, notSkipInvalid) {
545
545
  var sts = [];
546
546
  var meta = metadata(attrs);
547
547
  if (!meta.keys || meta.keys.length === 0) {
548
- return undefined;
548
+ return sts;
549
549
  }
550
550
  for (var _i = 0, objs_3 = objs; _i < objs_3.length; _i++) {
551
551
  var obj = objs_3[_i];
@@ -644,7 +644,7 @@ function buildToUpdateBatch(objs, table, attrs, buildParam, notSkipInvalid) {
644
644
  }
645
645
  if (!valid || colSet.length === 0 || colQuery.length === 0) {
646
646
  if (notSkipInvalid) {
647
- return undefined;
647
+ return sts;
648
648
  }
649
649
  }
650
650
  else {
package/lib/client.js CHANGED
@@ -105,7 +105,7 @@ var ProxyClient = (function () {
105
105
  };
106
106
  ProxyClient.prototype.insert = function (table, attrs, obj, buildParam, ver) {
107
107
  var s = build_1.buildToInsert(obj, table, attrs, buildParam, ver);
108
- if (s) {
108
+ if (s.query.length > 0) {
109
109
  return this.exec(s.query, s.params);
110
110
  }
111
111
  else {
@@ -114,7 +114,7 @@ var ProxyClient = (function () {
114
114
  };
115
115
  ProxyClient.prototype.update = function (table, attrs, obj, buildParam, ver) {
116
116
  var s = build_1.buildToUpdate(obj, table, attrs, buildParam, ver);
117
- if (s) {
117
+ if (s.query.length > 0) {
118
118
  return this.exec(s.query, s.params);
119
119
  }
120
120
  else {
@@ -123,7 +123,7 @@ var ProxyClient = (function () {
123
123
  };
124
124
  ProxyClient.prototype.insertBatch = function (table, attrs, objs, buildParam, driver) {
125
125
  var s = build_1.buildToInsertBatch(objs, table, attrs, buildParam);
126
- if (s) {
126
+ if (s.query.length > 0) {
127
127
  return this.exec(s.query, s.params);
128
128
  }
129
129
  else {
@@ -141,7 +141,7 @@ var ProxyClient = (function () {
141
141
  };
142
142
  ProxyClient.prototype.insertWithTx = function (tx, commit, table, attrs, obj, buildParam, ver) {
143
143
  var s = build_1.buildToInsert(obj, table, attrs, buildParam, ver);
144
- if (s) {
144
+ if (s.query.length > 0) {
145
145
  return this.execWithTx(tx, commit, s.query, s.params);
146
146
  }
147
147
  else {
@@ -150,7 +150,7 @@ var ProxyClient = (function () {
150
150
  };
151
151
  ProxyClient.prototype.updateWithTx = function (tx, commit, table, attrs, obj, buildParam, ver) {
152
152
  var s = build_1.buildToUpdate(obj, table, attrs, buildParam, ver);
153
- if (s) {
153
+ if (s.query.length > 0) {
154
154
  return this.execWithTx(tx, commit, s.query, s.params);
155
155
  }
156
156
  else {
@@ -159,7 +159,7 @@ var ProxyClient = (function () {
159
159
  };
160
160
  ProxyClient.prototype.insertBatchWithTx = function (tx, commit, table, attrs, objs, buildParam, driver) {
161
161
  var s = build_1.buildToInsertBatch(objs, table, attrs, buildParam);
162
- if (s) {
162
+ if (s.query.length > 0) {
163
163
  return this.execWithTx(tx, commit, s.query, s.params);
164
164
  }
165
165
  else {
package/lib/index.js CHANGED
@@ -13,9 +13,9 @@ exports.SqlViewService = services_1.SqlLoader;
13
13
  exports.SqlGenericService = services_1.SqlWriter;
14
14
  exports.GenericRepository = services_1.CRUDRepository;
15
15
  exports.SqlGenericRepository = services_1.CRUDRepository;
16
+ exports.Repository = services_1.SqlRepository;
16
17
  var SearchBuilder_1 = require("./SearchBuilder");
17
18
  exports.SearchRepository = SearchBuilder_1.SearchBuilder;
18
- exports.Repository = SearchBuilder_1.SqlRepository;
19
19
  __export(require("./batch"));
20
20
  __export(require("./build"));
21
21
  __export(require("./client"));
package/lib/services.js CHANGED
@@ -14,6 +14,7 @@ var __extends = (this && this.__extends) || (function () {
14
14
  })();
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  var build_1 = require("./build");
17
+ var SearchBuilder_1 = require("./SearchBuilder");
17
18
  function useGet(q, table, attrs, param, fromDB) {
18
19
  var l = new SqlLoader(q, table, attrs, param, fromDB);
19
20
  return l.load;
@@ -426,7 +427,7 @@ var SqlWriter = (function () {
426
427
  obj2 = this.toDB(obj);
427
428
  }
428
429
  var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
429
- if (stmt) {
430
+ if (stmt.query.length > 0) {
430
431
  return this.exec(stmt.query, stmt.params, ctx).catch(function (err) {
431
432
  if (err && err.error === "duplicate") {
432
433
  return 0;
@@ -446,7 +447,7 @@ var SqlWriter = (function () {
446
447
  obj2 = this.toDB(obj);
447
448
  }
448
449
  var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
449
- if (stmt) {
450
+ if (stmt.query.length > 0) {
450
451
  return this.exec(stmt.query, stmt.params, ctx);
451
452
  }
452
453
  else {
@@ -485,7 +486,7 @@ var CRUDRepository = (function (_super) {
485
486
  };
486
487
  CRUDRepository.prototype.load = function (id, ctx) {
487
488
  var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
488
- if (!stmt) {
489
+ if (stmt.query.length === 0) {
489
490
  throw new Error("cannot build query by id");
490
491
  }
491
492
  var fn = this.fromDB;
@@ -507,14 +508,14 @@ var CRUDRepository = (function (_super) {
507
508
  CRUDRepository.prototype.exist = function (id, ctx) {
508
509
  var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
509
510
  var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
510
- if (!stmt) {
511
+ if (stmt.query.length === 0) {
511
512
  throw new Error("cannot build query by id");
512
513
  }
513
514
  return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
514
515
  };
515
516
  CRUDRepository.prototype.delete = function (id, ctx) {
516
517
  var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.param);
517
- if (stmt) {
518
+ if (stmt.query.length > 0) {
518
519
  return this.exec(stmt.query, stmt.params, ctx);
519
520
  }
520
521
  else {
@@ -524,3 +525,174 @@ var CRUDRepository = (function (_super) {
524
525
  return CRUDRepository;
525
526
  }(SqlWriter));
526
527
  exports.CRUDRepository = CRUDRepository;
528
+ var SqlSearchWriter = (function (_super) {
529
+ __extends(SqlSearchWriter, _super);
530
+ function SqlSearchWriter(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
531
+ var _this = _super.call(this, db.query, table, attributes, db.driver, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
532
+ _this.attributes = attributes;
533
+ _this.toDB = toDB;
534
+ _this.exec = db.exec;
535
+ var x = build_1.version(attributes);
536
+ if (x) {
537
+ _this.version = x.name;
538
+ }
539
+ _this.create = _this.create.bind(_this);
540
+ _this.update = _this.update.bind(_this);
541
+ _this.patch = _this.patch.bind(_this);
542
+ return _this;
543
+ }
544
+ SqlSearchWriter.prototype.create = function (obj, ctx) {
545
+ var obj2 = obj;
546
+ if (this.toDB) {
547
+ obj2 = this.toDB(obj);
548
+ }
549
+ var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
550
+ if (stmt.query.length > 0) {
551
+ return this.exec(stmt.query, stmt.params, ctx).catch(function (err) {
552
+ if (err && err.error === "duplicate") {
553
+ return 0;
554
+ }
555
+ else {
556
+ throw err;
557
+ }
558
+ });
559
+ }
560
+ else {
561
+ return Promise.resolve(0);
562
+ }
563
+ };
564
+ SqlSearchWriter.prototype.update = function (obj, ctx) {
565
+ var obj2 = obj;
566
+ if (this.toDB) {
567
+ obj2 = this.toDB(obj);
568
+ }
569
+ var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
570
+ if (stmt.query.length > 0) {
571
+ return this.exec(stmt.query, stmt.params, ctx);
572
+ }
573
+ else {
574
+ return Promise.resolve(0);
575
+ }
576
+ };
577
+ SqlSearchWriter.prototype.patch = function (obj, ctx) {
578
+ return this.update(obj, ctx);
579
+ };
580
+ return SqlSearchWriter;
581
+ }(SearchBuilder_1.SearchBuilder));
582
+ exports.SqlSearchWriter = SqlSearchWriter;
583
+ var SqlRepository = (function (_super) {
584
+ __extends(SqlRepository, _super);
585
+ function SqlRepository(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
586
+ var _this = _super.call(this, db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
587
+ _this.attributes = attributes;
588
+ _this.toDB = toDB;
589
+ _this.metadata = _this.metadata.bind(_this);
590
+ _this.all = _this.all.bind(_this);
591
+ _this.load = _this.load.bind(_this);
592
+ _this.exist = _this.exist.bind(_this);
593
+ _this.delete = _this.delete.bind(_this);
594
+ return _this;
595
+ }
596
+ SqlRepository.prototype.metadata = function () {
597
+ return this.attributes;
598
+ };
599
+ SqlRepository.prototype.all = function () {
600
+ var sql = "select * from " + this.table;
601
+ return this.query(sql, [], this.map);
602
+ };
603
+ SqlRepository.prototype.load = function (id, ctx) {
604
+ var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
605
+ if (!stmt) {
606
+ throw new Error("cannot build query by id");
607
+ }
608
+ var fn = this.fromDB;
609
+ if (fn) {
610
+ return this.query(stmt.query, stmt.params, this.map, ctx).then(function (res) {
611
+ if (!res || res.length === 0) {
612
+ return null;
613
+ }
614
+ else {
615
+ var obj = res[0];
616
+ return fn(obj);
617
+ }
618
+ });
619
+ }
620
+ else {
621
+ return this.query(stmt.query, stmt.params, this.map).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
622
+ }
623
+ };
624
+ SqlRepository.prototype.exist = function (id, ctx) {
625
+ var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
626
+ var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
627
+ if (!stmt) {
628
+ throw new Error("cannot build query by id");
629
+ }
630
+ return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
631
+ };
632
+ SqlRepository.prototype.delete = function (id, ctx) {
633
+ var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.param);
634
+ if (stmt) {
635
+ return this.exec(stmt.query, stmt.params, ctx);
636
+ }
637
+ else {
638
+ return Promise.resolve(0);
639
+ }
640
+ };
641
+ return SqlRepository;
642
+ }(SqlSearchWriter));
643
+ exports.SqlRepository = SqlRepository;
644
+ var Query = (function (_super) {
645
+ __extends(Query, _super);
646
+ function Query(query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total) {
647
+ var _this = _super.call(this, query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
648
+ var m = build_1.metadata(attributes);
649
+ _this.primaryKeys = m.keys;
650
+ _this.map = m.map;
651
+ _this.bools = m.bools;
652
+ if (_this.metadata) {
653
+ _this.metadata = _this.metadata.bind(_this);
654
+ }
655
+ _this.all = _this.all.bind(_this);
656
+ _this.load = _this.load.bind(_this);
657
+ _this.exist = _this.exist.bind(_this);
658
+ return _this;
659
+ }
660
+ Query.prototype.metadata = function () {
661
+ return this.attrs;
662
+ };
663
+ Query.prototype.all = function () {
664
+ var sql = "select * from " + this.table;
665
+ return this.query(sql, [], this.map);
666
+ };
667
+ Query.prototype.load = function (id, ctx) {
668
+ var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
669
+ if (!stmt) {
670
+ throw new Error("cannot build query by id");
671
+ }
672
+ var fn = this.fromDB;
673
+ if (fn) {
674
+ return this.query(stmt.query, stmt.params, this.map, ctx).then(function (res) {
675
+ if (!res || res.length === 0) {
676
+ return null;
677
+ }
678
+ else {
679
+ var obj = res[0];
680
+ return fn(obj);
681
+ }
682
+ });
683
+ }
684
+ else {
685
+ return this.query(stmt.query, stmt.params, this.map).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
686
+ }
687
+ };
688
+ Query.prototype.exist = function (id, ctx) {
689
+ var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
690
+ var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
691
+ if (!stmt) {
692
+ throw new Error("cannot build query by id");
693
+ }
694
+ return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
695
+ };
696
+ return Query;
697
+ }(SearchBuilder_1.SearchBuilder));
698
+ exports.Query = Query;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",