query-core 0.2.1 → 0.2.2

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.
@@ -16,10 +16,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
16
16
  var build_1 = require("./build");
17
17
  var query_1 = require("./query");
18
18
  var search_1 = require("./search");
19
- exports.postgres = 'postgres';
20
- exports.mssql = 'mssql';
21
- exports.mysql = 'mysql';
22
- exports.sqlite = 'sqlite';
19
+ exports.postgres = "postgres";
20
+ exports.mssql = "mssql";
21
+ exports.mysql = "mysql";
22
+ exports.sqlite = "sqlite";
23
23
  var SearchBuilder = (function () {
24
24
  function SearchBuilder(query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) {
25
25
  this.query = query;
@@ -35,9 +35,9 @@ var SearchBuilder = (function () {
35
35
  this.bools = meta.bools;
36
36
  }
37
37
  this.buildQuery = buildQ ? buildQ : query_1.buildQuery;
38
- this.buildSort = (buildSort ? buildSort : query_1.buildSort);
39
- this.q = (q && q.length > 0 ? q : 'q');
40
- this.excluding = (excluding && excluding.length > 0 ? excluding : 'excluding');
38
+ this.buildSort = buildSort ? buildSort : query_1.buildSort;
39
+ this.q = q && q.length > 0 ? q : "q";
40
+ this.excluding = excluding && excluding.length > 0 ? excluding : "excluding";
41
41
  this.search = this.search.bind(this);
42
42
  if (buildParam) {
43
43
  this.param = buildParam;
@@ -56,20 +56,20 @@ var SearchBuilder = (function () {
56
56
  this.param = build_1.param;
57
57
  }
58
58
  }
59
- this.total = (total && total.length > 0 ? total : 'total');
59
+ this.total = total && total.length > 0 ? total : "total";
60
60
  }
61
- SearchBuilder.prototype.search = function (s, limit, offset, fields) {
61
+ SearchBuilder.prototype.search = function (filter, limit, offset, fields) {
62
62
  var skip = 0;
63
- if (typeof offset === 'number' && offset > 0) {
63
+ if (typeof offset === "number" && offset > 0) {
64
64
  skip = offset;
65
65
  }
66
- var st = (this.sort ? this.sort : 'sort');
67
- var sn = s[st];
68
- delete s[st];
69
- var x = (this.provider === exports.postgres ? 'ilike' : this.param);
70
- var q2 = this.buildQuery(s, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding);
66
+ var st = this.sort ? this.sort : "sort";
67
+ var sn = filter[st];
68
+ delete filter[st];
69
+ var x = this.provider === exports.postgres ? "ilike" : this.param;
70
+ var q2 = this.buildQuery(filter, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding);
71
71
  if (!q2) {
72
- throw new Error('Cannot build query');
72
+ throw new Error("Cannot build query");
73
73
  }
74
74
  var fn = this.fromDB;
75
75
  if (fn) {
@@ -116,7 +116,7 @@ var Query = (function (_super) {
116
116
  Query.prototype.load = function (id, ctx) {
117
117
  var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
118
118
  if (!stmt) {
119
- throw new Error('cannot build query by id');
119
+ throw new Error("cannot build query by id");
120
120
  }
121
121
  var fn = this.fromDB;
122
122
  if (fn) {
@@ -131,16 +131,16 @@ var Query = (function (_super) {
131
131
  });
132
132
  }
133
133
  else {
134
- return this.query(stmt.query, stmt.params, this.map).then(function (res) { return (!res || res.length === 0) ? null : res[0]; });
134
+ return this.query(stmt.query, stmt.params, this.map).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
135
135
  }
136
136
  };
137
137
  Query.prototype.exist = function (id, ctx) {
138
- var field = (this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name);
138
+ var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
139
139
  var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
140
140
  if (!stmt) {
141
- throw new Error('cannot build query by id');
141
+ throw new Error("cannot build query by id");
142
142
  }
143
- return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0) ? false : true; });
143
+ return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
144
144
  };
145
145
  return Query;
146
146
  }(SearchBuilder));
package/lib/services.js CHANGED
@@ -1,30 +1,17 @@
1
1
  "use strict";
2
- var __extends =
3
- (this && this.__extends) ||
4
- (function () {
5
- var extendStatics = function (d, b) {
6
- extendStatics =
7
- Object.setPrototypeOf ||
8
- ({ __proto__: [] } instanceof Array &&
9
- function (d, b) {
10
- d.__proto__ = b;
11
- }) ||
12
- function (d, b) {
13
- for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
14
- };
15
- return extendStatics(d, b);
16
- };
17
- return function (d, b) {
18
- extendStatics(d, b);
19
- function __() {
20
- this.constructor = d;
21
- }
22
- d.prototype =
23
- b === null
24
- ? Object.create(b)
25
- : ((__.prototype = b.prototype), new __());
26
- };
27
- })();
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
+ })();
28
15
  Object.defineProperty(exports, "__esModule", { value: true });
29
16
  var build_1 = require("./build");
30
17
  function useGet(q, table, attrs, param, fromDB) {
@@ -42,7 +29,8 @@ var SqlLoader = (function () {
42
29
  if (Array.isArray(attrs)) {
43
30
  this.primaryKeys = build_1.attributes(attrs);
44
31
  this.attributes = {};
45
- } else {
32
+ }
33
+ else {
46
34
  var m = build_1.metadata(attrs);
47
35
  this.attributes = attrs;
48
36
  this.primaryKeys = m.keys;
@@ -70,44 +58,30 @@ var SqlLoader = (function () {
70
58
  }
71
59
  var fn = this.fromDB;
72
60
  if (fn) {
73
- return this.query(stmt.query, stmt.params, this.map, ctx).then(function (
74
- res
75
- ) {
61
+ return this.query(stmt.query, stmt.params, this.map, ctx).then(function (res) {
76
62
  if (!res || res.length === 0) {
77
63
  return null;
78
- } else {
64
+ }
65
+ else {
79
66
  var obj = res[0];
80
67
  return fn(obj);
81
68
  }
82
69
  });
83
- } else {
84
- return this.query(stmt.query, stmt.params, this.map).then(function (res) {
85
- return !res || res.length === 0 ? null : res[0];
86
- });
70
+ }
71
+ else {
72
+ return this.query(stmt.query, stmt.params, this.map).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
87
73
  }
88
74
  };
89
75
  SqlLoader.prototype.exist = function (id, ctx) {
90
- var field = this.primaryKeys[0].column
91
- ? this.primaryKeys[0].column
92
- : this.primaryKeys[0].name;
93
- var stmt = build_1.exist(
94
- id,
95
- this.table,
96
- this.primaryKeys,
97
- this.param,
98
- field
99
- );
76
+ var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
77
+ var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
100
78
  if (!stmt) {
101
79
  throw new Error("cannot build query by id");
102
80
  }
103
- return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(
104
- function (res) {
105
- return !res || res.length === 0 ? false : true;
106
- }
107
- );
81
+ return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
108
82
  };
109
83
  return SqlLoader;
110
- })();
84
+ }());
111
85
  exports.SqlLoader = SqlLoader;
112
86
  var QueryRepository = (function () {
113
87
  function QueryRepository(db, table, attrs, sort, id) {
@@ -130,34 +104,17 @@ var QueryRepository = (function () {
130
104
  for (var i = 1; i <= length; i++) {
131
105
  ps.push(this.db.param(i));
132
106
  }
133
- var sql =
134
- "select * from " +
135
- this.table +
136
- " where " +
137
- this.id +
138
- " in (" +
139
- ps.join(",") +
140
- ")";
107
+ var sql = "select * from " + this.table + " where " + this.id + " in (" + ps.join(",") + ")";
141
108
  if (this.sort && this.sort.length > 0) {
142
109
  sql = sql + " order by " + this.sort;
143
110
  }
144
111
  return this.db.query(sql, ids, this.map, this.bools);
145
112
  };
146
113
  return QueryRepository;
147
- })();
114
+ }());
148
115
  exports.QueryRepository = QueryRepository;
149
116
  var SqlLoadRepository = (function () {
150
- function SqlLoadRepository(
151
- query,
152
- table,
153
- attrs,
154
- param,
155
- id1Field,
156
- id2Field,
157
- fromDB,
158
- id1Col,
159
- id2Col
160
- ) {
117
+ function SqlLoadRepository(query, table, attrs, param, id1Field, id2Field, fromDB, id1Col, id2Col) {
161
118
  this.query = query;
162
119
  this.table = table;
163
120
  this.param = param;
@@ -176,23 +133,25 @@ var SqlLoadRepository = (function () {
176
133
  this.exist = this.exist.bind(this);
177
134
  if (id1Col && id1Col.length > 0) {
178
135
  this.id1Col = id1Col;
179
- } else {
136
+ }
137
+ else {
180
138
  var c = attrs[this.id1Field];
181
139
  if (c) {
182
- this.id1Col =
183
- c.column && c.column.length > 0 ? c.column : this.id1Field;
184
- } else {
140
+ this.id1Col = c.column && c.column.length > 0 ? c.column : this.id1Field;
141
+ }
142
+ else {
185
143
  this.id1Col = this.id1Field;
186
144
  }
187
145
  }
188
146
  if (id2Col && id2Col.length > 0) {
189
147
  this.id2Col = id2Col;
190
- } else {
148
+ }
149
+ else {
191
150
  var c = attrs[this.id2Field];
192
151
  if (c) {
193
- this.id2Col =
194
- c.column && c.column.length > 0 ? c.column : this.id2Field;
195
- } else {
152
+ this.id2Col = c.column && c.column.length > 0 ? c.column : this.id2Field;
153
+ }
154
+ else {
196
155
  this.id2Col = this.id2Field;
197
156
  }
198
157
  }
@@ -206,85 +165,33 @@ var SqlLoadRepository = (function () {
206
165
  };
207
166
  SqlLoadRepository.prototype.load = function (id1, id2, ctx) {
208
167
  var _this = this;
209
- return this.query(
210
- "select * from " +
211
- this.table +
212
- " where " +
213
- this.id1Col +
214
- " = " +
215
- this.param(1) +
216
- " and " +
217
- this.id2Col +
218
- " = " +
219
- this.param(2),
220
- [id1, id2],
221
- this.map,
222
- undefined,
223
- ctx
224
- ).then(function (objs) {
168
+ return this.query("select * from " + this.table + " where " + this.id1Col + " = " + this.param(1) + " and " + this.id2Col + " = " + this.param(2), [id1, id2], this.map, undefined, ctx).then(function (objs) {
225
169
  if (!objs || objs.length === 0) {
226
170
  return null;
227
- } else {
171
+ }
172
+ else {
228
173
  var fn = _this.fromDB;
229
174
  if (fn) {
230
175
  return fn(objs[0]);
231
- } else {
176
+ }
177
+ else {
232
178
  return objs[0];
233
179
  }
234
180
  }
235
181
  });
236
182
  };
237
183
  SqlLoadRepository.prototype.exist = function (id1, id2, ctx) {
238
- return this.query(
239
- "select " +
240
- this.id1Col +
241
- " from " +
242
- this.table +
243
- " where " +
244
- this.id1Col +
245
- " = " +
246
- this.param(1) +
247
- " and " +
248
- this.id2Col +
249
- " = " +
250
- this.param(2),
251
- [id1, id2],
252
- undefined,
253
- undefined,
254
- ctx
255
- ).then(function (objs) {
184
+ return this.query("select " + this.id1Col + " from " + this.table + " where " + this.id1Col + " = " + this.param(1) + " and " + this.id2Col + " = " + this.param(2), [id1, id2], undefined, undefined, ctx).then(function (objs) {
256
185
  return objs && objs.length > 0 ? true : false;
257
186
  });
258
187
  };
259
188
  return SqlLoadRepository;
260
- })();
189
+ }());
261
190
  exports.SqlLoadRepository = SqlLoadRepository;
262
191
  var GenericRepository = (function (_super) {
263
192
  __extends(GenericRepository, _super);
264
- function GenericRepository(
265
- manager,
266
- table,
267
- attrs,
268
- id1Field,
269
- id2Field,
270
- toDB,
271
- fromDB,
272
- id1Col,
273
- id2Col
274
- ) {
275
- var _this =
276
- _super.call(
277
- this,
278
- manager.query,
279
- table,
280
- attrs,
281
- manager.param,
282
- id1Field,
283
- id2Field,
284
- fromDB,
285
- id1Col,
286
- id2Col
287
- ) || this;
193
+ function GenericRepository(manager, table, attrs, id1Field, id2Field, toDB, fromDB, id1Col, id2Col) {
194
+ var _this = _super.call(this, manager.query, table, attrs, manager.param, id1Field, id2Field, fromDB, id1Col, id2Col) || this;
288
195
  _this.toDB = toDB;
289
196
  var x = build_1.version(attrs);
290
197
  _this.exec = manager.exec;
@@ -303,22 +210,18 @@ var GenericRepository = (function (_super) {
303
210
  if (this.toDB) {
304
211
  obj2 = this.toDB(obj);
305
212
  }
306
- var stmt = build_1.buildToInsert(
307
- obj2,
308
- this.table,
309
- this.attributes,
310
- this.param,
311
- this.version
312
- );
213
+ var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
313
214
  if (stmt) {
314
215
  return this.exec(stmt.query, stmt.params, ctx).catch(function (err) {
315
216
  if (err && err.error === "duplicate") {
316
217
  return 0;
317
- } else {
218
+ }
219
+ else {
318
220
  throw err;
319
221
  }
320
222
  });
321
- } else {
223
+ }
224
+ else {
322
225
  return Promise.resolve(0);
323
226
  }
324
227
  };
@@ -327,16 +230,11 @@ var GenericRepository = (function (_super) {
327
230
  if (this.toDB) {
328
231
  obj2 = this.toDB(obj);
329
232
  }
330
- var stmt = build_1.buildToUpdate(
331
- obj2,
332
- this.table,
333
- this.attributes,
334
- this.param,
335
- this.version
336
- );
233
+ var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
337
234
  if (stmt) {
338
235
  return this.exec(stmt.query, stmt.params, ctx);
339
- } else {
236
+ }
237
+ else {
340
238
  return Promise.resolve(0);
341
239
  }
342
240
  };
@@ -344,23 +242,10 @@ var GenericRepository = (function (_super) {
344
242
  return this.update(obj, ctx);
345
243
  };
346
244
  GenericRepository.prototype.delete = function (id1, id2, ctx) {
347
- return this.exec(
348
- "delete from " +
349
- this.table +
350
- " where " +
351
- this.id1Col +
352
- " = " +
353
- this.param(1) +
354
- " and " +
355
- this.id2Col +
356
- " = " +
357
- this.param(2),
358
- [id1, id2],
359
- ctx
360
- );
245
+ return this.exec("delete from " + this.table + " where " + this.id1Col + " = " + this.param(1) + " and " + this.id2Col + " = " + this.param(2), [id1, id2], ctx);
361
246
  };
362
247
  return GenericRepository;
363
- })(SqlLoadRepository);
248
+ }(SqlLoadRepository));
364
249
  exports.GenericRepository = GenericRepository;
365
250
  var SqlSearchLoader = (function (_super) {
366
251
  __extends(SqlSearchLoader, _super);
@@ -374,7 +259,7 @@ var SqlSearchLoader = (function (_super) {
374
259
  return this.find(s, limit, offset, fields);
375
260
  };
376
261
  return SqlSearchLoader;
377
- })(SqlLoader);
262
+ }(SqlLoader));
378
263
  exports.SqlSearchLoader = SqlSearchLoader;
379
264
  function log(db, isLog, logger, q, result, r, duration) {
380
265
  if (!isLog) {
@@ -384,15 +269,7 @@ function log(db, isLog, logger, q, result, r, duration) {
384
269
  if (!logger.isDebugEnabled()) {
385
270
  return db;
386
271
  }
387
- return new LogManager(
388
- db,
389
- logger.error,
390
- logger.debug,
391
- q,
392
- result,
393
- r,
394
- duration
395
- );
272
+ return new LogManager(db, logger.error, logger.debug, q, result, r, duration);
396
273
  }
397
274
  if (!logger.isInfoEnabled()) {
398
275
  return db;
@@ -437,34 +314,34 @@ var LogManager = (function () {
437
314
  return this.db
438
315
  .exec(sql, args, ctx)
439
316
  .then(function (v) {
440
- setTimeout(function () {
441
- if (_this.log) {
442
- var d = diff(t1);
443
- var obj = {};
444
- if (_this.sql.length > 0) {
445
- obj[_this.sql] = getString(sql, args);
446
- }
447
- if (_this.return.length > 0) {
448
- obj[_this.return] = v;
449
- }
450
- obj[_this.duration] = d;
451
- _this.log("query", obj);
452
- }
453
- }, 0);
454
- return v;
455
- })
456
- .catch(function (er) {
457
- setTimeout(function () {
317
+ setTimeout(function () {
318
+ if (_this.log) {
458
319
  var d = diff(t1);
459
320
  var obj = {};
460
321
  if (_this.sql.length > 0) {
461
322
  obj[_this.sql] = getString(sql, args);
462
323
  }
324
+ if (_this.return.length > 0) {
325
+ obj[_this.return] = v;
326
+ }
463
327
  obj[_this.duration] = d;
464
- _this.error("error query: " + buildString(er));
465
- }, 0);
466
- throw er;
467
- });
328
+ _this.log("query", obj);
329
+ }
330
+ }, 0);
331
+ return v;
332
+ })
333
+ .catch(function (er) {
334
+ setTimeout(function () {
335
+ var d = diff(t1);
336
+ var obj = {};
337
+ if (_this.sql.length > 0) {
338
+ obj[_this.sql] = getString(sql, args);
339
+ }
340
+ obj[_this.duration] = d;
341
+ _this.error("error query: " + buildString(er));
342
+ }, 0);
343
+ throw er;
344
+ });
468
345
  };
469
346
  LogManager.prototype.execBatch = function (statements, firstSuccess, ctx) {
470
347
  var _this = this;
@@ -472,34 +349,34 @@ var LogManager = (function () {
472
349
  return this.db
473
350
  .execBatch(statements, firstSuccess, ctx)
474
351
  .then(function (v) {
475
- setTimeout(function () {
476
- if (_this.log) {
477
- var d = diff(t1);
478
- var obj = {};
479
- if (_this.sql.length > 0) {
480
- obj[_this.sql] = JSON.stringify(statements);
481
- }
482
- if (_this.return.length > 0) {
483
- obj[_this.return] = v;
484
- }
485
- obj[_this.duration] = d;
486
- _this.log("exec batch", obj);
487
- }
488
- }, 0);
489
- return v;
490
- })
491
- .catch(function (er) {
492
- setTimeout(function () {
352
+ setTimeout(function () {
353
+ if (_this.log) {
493
354
  var d = diff(t1);
494
355
  var obj = {};
495
356
  if (_this.sql.length > 0) {
496
357
  obj[_this.sql] = JSON.stringify(statements);
497
358
  }
359
+ if (_this.return.length > 0) {
360
+ obj[_this.return] = v;
361
+ }
498
362
  obj[_this.duration] = d;
499
- _this.error("error exec batch: " + buildString(er));
500
- }, 0);
501
- throw er;
502
- });
363
+ _this.log("exec batch", obj);
364
+ }
365
+ }, 0);
366
+ return v;
367
+ })
368
+ .catch(function (er) {
369
+ setTimeout(function () {
370
+ var d = diff(t1);
371
+ var obj = {};
372
+ if (_this.sql.length > 0) {
373
+ obj[_this.sql] = JSON.stringify(statements);
374
+ }
375
+ obj[_this.duration] = d;
376
+ _this.error("error exec batch: " + buildString(er));
377
+ }, 0);
378
+ throw er;
379
+ });
503
380
  };
504
381
  LogManager.prototype.query = function (sql, args, m, bools, ctx) {
505
382
  var _this = this;
@@ -507,39 +384,39 @@ var LogManager = (function () {
507
384
  return this.db
508
385
  .query(sql, args, m, bools, ctx)
509
386
  .then(function (v) {
510
- setTimeout(function () {
511
- if (_this.log) {
512
- var d = diff(t1);
513
- var obj = {};
514
- if (_this.sql.length > 0) {
515
- obj[_this.sql] = getString(sql, args);
516
- }
517
- if (_this.result.length > 0) {
518
- if (v && v.length > 0) {
519
- obj[_this.result] = JSON.stringify(v);
520
- }
521
- }
522
- if (_this.return.length > 0) {
523
- obj[_this.return] = v ? v.length : 0;
524
- }
525
- obj[_this.duration] = d;
526
- _this.log("query", obj);
527
- }
528
- }, 0);
529
- return v;
530
- })
531
- .catch(function (er) {
532
- setTimeout(function () {
387
+ setTimeout(function () {
388
+ if (_this.log) {
533
389
  var d = diff(t1);
534
390
  var obj = {};
535
391
  if (_this.sql.length > 0) {
536
392
  obj[_this.sql] = getString(sql, args);
537
393
  }
394
+ if (_this.result.length > 0) {
395
+ if (v && v.length > 0) {
396
+ obj[_this.result] = JSON.stringify(v);
397
+ }
398
+ }
399
+ if (_this.return.length > 0) {
400
+ obj[_this.return] = v ? v.length : 0;
401
+ }
538
402
  obj[_this.duration] = d;
539
- _this.error("error query: " + buildString(er));
540
- }, 0);
541
- throw er;
542
- });
403
+ _this.log("query", obj);
404
+ }
405
+ }, 0);
406
+ return v;
407
+ })
408
+ .catch(function (er) {
409
+ setTimeout(function () {
410
+ var d = diff(t1);
411
+ var obj = {};
412
+ if (_this.sql.length > 0) {
413
+ obj[_this.sql] = getString(sql, args);
414
+ }
415
+ obj[_this.duration] = d;
416
+ _this.error("error query: " + buildString(er));
417
+ }, 0);
418
+ throw er;
419
+ });
543
420
  };
544
421
  LogManager.prototype.queryOne = function (sql, args, m, bools, ctx) {
545
422
  var _this = this;
@@ -547,37 +424,37 @@ var LogManager = (function () {
547
424
  return this.db
548
425
  .queryOne(sql, args, m, bools, ctx)
549
426
  .then(function (v) {
550
- setTimeout(function () {
551
- if (_this.log) {
552
- var d = diff(t1);
553
- var obj = {};
554
- if (_this.sql.length > 0) {
555
- obj[_this.sql] = getString(sql, args);
556
- }
557
- if (_this.result.length > 0) {
558
- obj[_this.result] = v ? JSON.stringify(v) : "null";
559
- }
560
- if (_this.return.length > 0) {
561
- obj[_this.return] = v ? 1 : 0;
562
- }
563
- obj[_this.duration] = d;
564
- _this.log("query one", obj);
565
- }
566
- }, 0);
567
- return v;
568
- })
569
- .catch(function (er) {
570
- setTimeout(function () {
427
+ setTimeout(function () {
428
+ if (_this.log) {
571
429
  var d = diff(t1);
572
430
  var obj = {};
573
431
  if (_this.sql.length > 0) {
574
432
  obj[_this.sql] = getString(sql, args);
575
433
  }
434
+ if (_this.result.length > 0) {
435
+ obj[_this.result] = v ? JSON.stringify(v) : "null";
436
+ }
437
+ if (_this.return.length > 0) {
438
+ obj[_this.return] = v ? 1 : 0;
439
+ }
576
440
  obj[_this.duration] = d;
577
- _this.error("error query one: " + buildString(er));
578
- }, 0);
579
- throw er;
580
- });
441
+ _this.log("query one", obj);
442
+ }
443
+ }, 0);
444
+ return v;
445
+ })
446
+ .catch(function (er) {
447
+ setTimeout(function () {
448
+ var d = diff(t1);
449
+ var obj = {};
450
+ if (_this.sql.length > 0) {
451
+ obj[_this.sql] = getString(sql, args);
452
+ }
453
+ obj[_this.duration] = d;
454
+ _this.error("error query one: " + buildString(er));
455
+ }, 0);
456
+ throw er;
457
+ });
581
458
  };
582
459
  LogManager.prototype.execScalar = function (sql, args, ctx) {
583
460
  var _this = this;
@@ -585,37 +462,37 @@ var LogManager = (function () {
585
462
  return this.db
586
463
  .execScalar(sql, args, ctx)
587
464
  .then(function (v) {
588
- setTimeout(function () {
589
- if (_this.log) {
590
- var d = diff(t1);
591
- var obj = {};
592
- if (_this.sql.length > 0) {
593
- obj[_this.sql] = getString(sql, args);
594
- }
595
- if (_this.result.length > 0) {
596
- obj[_this.result] = v ? buildString(v) : "null";
597
- }
598
- if (_this.return.length > 0) {
599
- obj[_this.return] = v ? 1 : 0;
600
- }
601
- obj[_this.duration] = d;
602
- _this.log("exec scalar", obj);
603
- }
604
- }, 0);
605
- return v;
606
- })
607
- .catch(function (er) {
608
- setTimeout(function () {
465
+ setTimeout(function () {
466
+ if (_this.log) {
609
467
  var d = diff(t1);
610
468
  var obj = {};
611
469
  if (_this.sql.length > 0) {
612
470
  obj[_this.sql] = getString(sql, args);
613
471
  }
472
+ if (_this.result.length > 0) {
473
+ obj[_this.result] = v ? buildString(v) : "null";
474
+ }
475
+ if (_this.return.length > 0) {
476
+ obj[_this.return] = v ? 1 : 0;
477
+ }
614
478
  obj[_this.duration] = d;
615
- _this.error("error exec scalar: " + buildString(er));
616
- }, 0);
617
- throw er;
618
- });
479
+ _this.log("exec scalar", obj);
480
+ }
481
+ }, 0);
482
+ return v;
483
+ })
484
+ .catch(function (er) {
485
+ setTimeout(function () {
486
+ var d = diff(t1);
487
+ var obj = {};
488
+ if (_this.sql.length > 0) {
489
+ obj[_this.sql] = getString(sql, args);
490
+ }
491
+ obj[_this.duration] = d;
492
+ _this.error("error exec scalar: " + buildString(er));
493
+ }, 0);
494
+ throw er;
495
+ });
619
496
  };
620
497
  LogManager.prototype.count = function (sql, args, ctx) {
621
498
  var _this = this;
@@ -623,49 +500,51 @@ var LogManager = (function () {
623
500
  return this.db
624
501
  .count(sql, args)
625
502
  .then(function (v) {
626
- setTimeout(function () {
627
- if (_this.log) {
628
- var d = diff(t1);
629
- var obj = {};
630
- if (_this.sql.length > 0) {
631
- obj[_this.sql] = getString(sql, args);
632
- }
633
- if (_this.return.length > 0) {
634
- obj[_this.return] = v;
635
- }
636
- obj[_this.duration] = d;
637
- _this.log("count", obj);
638
- }
639
- }, 0);
640
- return v;
641
- })
642
- .catch(function (er) {
643
- setTimeout(function () {
503
+ setTimeout(function () {
504
+ if (_this.log) {
644
505
  var d = diff(t1);
645
506
  var obj = {};
646
507
  if (_this.sql.length > 0) {
647
508
  obj[_this.sql] = getString(sql, args);
648
509
  }
510
+ if (_this.return.length > 0) {
511
+ obj[_this.return] = v;
512
+ }
649
513
  obj[_this.duration] = d;
650
- _this.error("error count: " + buildString(er));
651
- }, 0);
652
- throw er;
653
- });
514
+ _this.log("count", obj);
515
+ }
516
+ }, 0);
517
+ return v;
518
+ })
519
+ .catch(function (er) {
520
+ setTimeout(function () {
521
+ var d = diff(t1);
522
+ var obj = {};
523
+ if (_this.sql.length > 0) {
524
+ obj[_this.sql] = getString(sql, args);
525
+ }
526
+ obj[_this.duration] = d;
527
+ _this.error("error count: " + buildString(er));
528
+ }, 0);
529
+ throw er;
530
+ });
654
531
  };
655
532
  return LogManager;
656
- })();
533
+ }());
657
534
  exports.LogManager = LogManager;
658
535
  function buildString(v) {
659
536
  if (typeof v === "string") {
660
537
  return v;
661
- } else {
538
+ }
539
+ else {
662
540
  return JSON.stringify(v);
663
541
  }
664
542
  }
665
543
  function getString(sql, args) {
666
544
  if (args && args.length > 0) {
667
545
  return sql + " " + JSON.stringify(args);
668
- } else {
546
+ }
547
+ else {
669
548
  return sql;
670
549
  }
671
550
  }
@@ -677,9 +556,7 @@ exports.diff = diff;
677
556
  var SqlWriter = (function (_super) {
678
557
  __extends(SqlWriter, _super);
679
558
  function SqlWriter(manager, table, attrs, toDB, fromDB) {
680
- var _this =
681
- _super.call(this, manager.query, table, attrs, manager.param, fromDB) ||
682
- this;
559
+ var _this = _super.call(this, manager.query, table, attrs, manager.param, fromDB) || this;
683
560
  _this.toDB = toDB;
684
561
  var x = build_1.version(attrs);
685
562
  _this.exec = manager.exec;
@@ -698,22 +575,18 @@ var SqlWriter = (function (_super) {
698
575
  if (this.toDB) {
699
576
  obj2 = this.toDB(obj);
700
577
  }
701
- var stmt = build_1.buildToInsert(
702
- obj2,
703
- this.table,
704
- this.attributes,
705
- this.param,
706
- this.version
707
- );
578
+ var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
708
579
  if (stmt) {
709
580
  return this.exec(stmt.query, stmt.params, ctx).catch(function (err) {
710
581
  if (err && err.error === "duplicate") {
711
582
  return 0;
712
- } else {
583
+ }
584
+ else {
713
585
  throw err;
714
586
  }
715
587
  });
716
- } else {
588
+ }
589
+ else {
717
590
  return Promise.resolve(0);
718
591
  }
719
592
  };
@@ -722,16 +595,11 @@ var SqlWriter = (function (_super) {
722
595
  if (this.toDB) {
723
596
  obj2 = this.toDB(obj);
724
597
  }
725
- var stmt = build_1.buildToUpdate(
726
- obj2,
727
- this.table,
728
- this.attributes,
729
- this.param,
730
- this.version
731
- );
598
+ var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
732
599
  if (stmt) {
733
600
  return this.exec(stmt.query, stmt.params, ctx);
734
- } else {
601
+ }
602
+ else {
735
603
  return Promise.resolve(0);
736
604
  }
737
605
  };
@@ -739,20 +607,16 @@ var SqlWriter = (function (_super) {
739
607
  return this.update(obj, ctx);
740
608
  };
741
609
  SqlWriter.prototype.delete = function (id, ctx) {
742
- var stmt = build_1.buildToDelete(
743
- id,
744
- this.table,
745
- this.primaryKeys,
746
- this.param
747
- );
610
+ var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.param);
748
611
  if (stmt) {
749
612
  return this.exec(stmt.query, stmt.params, ctx);
750
- } else {
613
+ }
614
+ else {
751
615
  return Promise.resolve(0);
752
616
  }
753
617
  };
754
618
  return SqlWriter;
755
- })(SqlLoader);
619
+ }(SqlLoader));
756
620
  exports.SqlWriter = SqlWriter;
757
621
  var SqlSearchWriter = (function (_super) {
758
622
  __extends(SqlSearchWriter, _super);
@@ -766,5 +630,5 @@ var SqlSearchWriter = (function (_super) {
766
630
  return this.find(s, limit, offset, fields);
767
631
  };
768
632
  return SqlSearchWriter;
769
- })(SqlWriter);
633
+ }(SqlWriter));
770
634
  exports.SqlSearchWriter = SqlSearchWriter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -76,16 +76,16 @@ export class SearchBuilder<T, S> {
76
76
  }
77
77
  this.total = total && total.length > 0 ? total : "total"
78
78
  }
79
- search(s: S, limit?: number, offset?: number | string, fields?: string[]): Promise<SearchResult<T>> {
79
+ search(filter: S, limit: number, offset?: number | string, fields?: string[]): Promise<SearchResult<T>> {
80
80
  let skip = 0
81
81
  if (typeof offset === "number" && offset > 0) {
82
82
  skip = offset
83
83
  }
84
84
  const st = this.sort ? this.sort : "sort"
85
- const sn = (s as any)[st] as string
86
- delete (s as any)[st]
85
+ const sn = (filter as any)[st] as string
86
+ delete (filter as any)[st]
87
87
  const x = this.provider === postgres ? "ilike" : this.param
88
- const q2 = this.buildQuery(s, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding)
88
+ const q2 = this.buildQuery(filter, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding)
89
89
  if (!q2) {
90
90
  throw new Error("Cannot build query")
91
91
  }
package/src/services.ts CHANGED
@@ -269,7 +269,7 @@ export class GenericRepository<T, K1, K2> extends SqlLoadRepository<T, K1, K2> {
269
269
  // tslint:disable-next-line:max-classes-per-file
270
270
  export class SqlSearchLoader<T, ID, S extends Filter> extends SqlLoader<T, ID> {
271
271
  constructor(
272
- protected find: (s: S, limit?: number, offset?: number | string, fields?: string[]) => Promise<SearchResult<T>>,
272
+ protected find: (s: S, limit: number, offset?: number | string, fields?: string[]) => Promise<SearchResult<T>>,
273
273
  query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
274
274
  table: string,
275
275
  attrs: Attributes | string[],
@@ -279,7 +279,7 @@ export class SqlSearchLoader<T, ID, S extends Filter> extends SqlLoader<T, ID> {
279
279
  super(query, table, attrs, param, fromDB)
280
280
  this.search = this.search.bind(this)
281
281
  }
282
- search(s: S, limit?: number, offset?: number | string, fields?: string[]): Promise<SearchResult<T>> {
282
+ search(s: S, limit: number, offset?: number | string, fields?: string[]): Promise<SearchResult<T>> {
283
283
  return this.find(s, limit, offset, fields)
284
284
  }
285
285
  }
@@ -688,7 +688,7 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
688
688
  // tslint:disable-next-line:max-classes-per-file
689
689
  export class SqlSearchWriter<T, ID, S extends Filter> extends SqlWriter<T, ID> {
690
690
  constructor(
691
- protected find: (s: S, limit?: number, offset?: number | string, fields?: string[]) => Promise<SearchResult<T>>,
691
+ protected find: (s: S, limit: number, offset?: number | string, fields?: string[]) => Promise<SearchResult<T>>,
692
692
  manager: Manager,
693
693
  table: string,
694
694
  attrs: Attributes,
@@ -698,7 +698,7 @@ export class SqlSearchWriter<T, ID, S extends Filter> extends SqlWriter<T, ID> {
698
698
  super(manager, table, attrs, toDB, fromDB)
699
699
  this.search = this.search.bind(this)
700
700
  }
701
- search(s: S, limit?: number, offset?: number | string, fields?: string[]): Promise<SearchResult<T>> {
701
+ search(s: S, limit: number, offset?: number | string, fields?: string[]): Promise<SearchResult<T>> {
702
702
  return this.find(s, limit, offset, fields)
703
703
  }
704
704
  }