query-core 0.2.7 → 0.2.8

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.
@@ -21,18 +21,22 @@ exports.mssql = "mssql";
21
21
  exports.mysql = "mysql";
22
22
  exports.sqlite = "sqlite";
23
23
  var SearchBuilder = (function () {
24
- function SearchBuilder(query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) {
24
+ function SearchBuilder(query, table, attrs, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total) {
25
25
  this.query = query;
26
26
  this.table = table;
27
- this.attributes = attributes;
27
+ this.attrs = attrs;
28
28
  this.provider = provider;
29
29
  this.fromDB = fromDB;
30
30
  this.sort = sort;
31
- if (attributes) {
32
- this.attributes = attributes;
33
- var meta = build_1.metadata(attributes);
31
+ if (attrs) {
32
+ this.attrs = attrs;
33
+ var meta = build_1.metadata(attrs);
34
34
  this.map = meta.map;
35
35
  this.bools = meta.bools;
36
+ this.primaryKeys = meta.keys;
37
+ }
38
+ else {
39
+ this.primaryKeys = [];
36
40
  }
37
41
  this.deleteSort = buildQ ? undefined : true;
38
42
  this.buildQuery = buildQ ? buildQ : query_1.buildQuery;
@@ -69,8 +73,8 @@ var SearchBuilder = (function () {
69
73
  if (this.deleteSort) {
70
74
  delete filter[st];
71
75
  }
72
- var x = this.provider === exports.postgres ? "ilike" : this.param;
73
- var q2 = this.buildQuery(filter, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding);
76
+ var likeType = this.provider === exports.postgres ? "ilike" : "like";
77
+ var q2 = this.buildQuery(filter, this.param, sn, this.buildSort, this.attrs, this.table, fields, this.q, this.excluding, likeType);
74
78
  if (!q2) {
75
79
  throw new Error("Cannot build query");
76
80
  }
@@ -93,10 +97,126 @@ var SearchBuilder = (function () {
93
97
  return SearchBuilder;
94
98
  }());
95
99
  exports.SearchBuilder = SearchBuilder;
100
+ var SqlSearchWriter = (function (_super) {
101
+ __extends(SqlSearchWriter, _super);
102
+ function SqlSearchWriter(manager, table, attributes, buildQ, provider, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
103
+ var _this = _super.call(this, manager.query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
104
+ _this.attributes = attributes;
105
+ _this.toDB = toDB;
106
+ _this.exec = manager.exec;
107
+ var x = build_1.version(attributes);
108
+ if (x) {
109
+ _this.version = x.name;
110
+ }
111
+ _this.create = _this.create.bind(_this);
112
+ _this.update = _this.update.bind(_this);
113
+ _this.patch = _this.patch.bind(_this);
114
+ return _this;
115
+ }
116
+ SqlSearchWriter.prototype.create = function (obj, ctx) {
117
+ var obj2 = obj;
118
+ if (this.toDB) {
119
+ obj2 = this.toDB(obj);
120
+ }
121
+ var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
122
+ if (stmt) {
123
+ return this.exec(stmt.query, stmt.params, ctx).catch(function (err) {
124
+ if (err && err.error === "duplicate") {
125
+ return 0;
126
+ }
127
+ else {
128
+ throw err;
129
+ }
130
+ });
131
+ }
132
+ else {
133
+ return Promise.resolve(0);
134
+ }
135
+ };
136
+ SqlSearchWriter.prototype.update = function (obj, ctx) {
137
+ var obj2 = obj;
138
+ if (this.toDB) {
139
+ obj2 = this.toDB(obj);
140
+ }
141
+ var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
142
+ if (stmt) {
143
+ return this.exec(stmt.query, stmt.params, ctx);
144
+ }
145
+ else {
146
+ return Promise.resolve(0);
147
+ }
148
+ };
149
+ SqlSearchWriter.prototype.patch = function (obj, ctx) {
150
+ return this.update(obj, ctx);
151
+ };
152
+ return SqlSearchWriter;
153
+ }(SearchBuilder));
154
+ exports.SqlSearchWriter = SqlSearchWriter;
155
+ var SqlRepository = (function (_super) {
156
+ __extends(SqlRepository, _super);
157
+ function SqlRepository(manager, table, attributes, buildQ, provider, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
158
+ var _this = _super.call(this, manager, table, attributes, buildQ, provider, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
159
+ _this.attributes = attributes;
160
+ _this.toDB = toDB;
161
+ _this.metadata = _this.metadata.bind(_this);
162
+ _this.all = _this.all.bind(_this);
163
+ _this.load = _this.load.bind(_this);
164
+ _this.exist = _this.exist.bind(_this);
165
+ _this.delete = _this.delete.bind(_this);
166
+ return _this;
167
+ }
168
+ SqlRepository.prototype.metadata = function () {
169
+ return this.attributes;
170
+ };
171
+ SqlRepository.prototype.all = function () {
172
+ var sql = "select * from " + this.table;
173
+ return this.query(sql, [], this.map);
174
+ };
175
+ SqlRepository.prototype.load = function (id, ctx) {
176
+ var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
177
+ if (!stmt) {
178
+ throw new Error("cannot build query by id");
179
+ }
180
+ var fn = this.fromDB;
181
+ if (fn) {
182
+ return this.query(stmt.query, stmt.params, this.map, ctx).then(function (res) {
183
+ if (!res || res.length === 0) {
184
+ return null;
185
+ }
186
+ else {
187
+ var obj = res[0];
188
+ return fn(obj);
189
+ }
190
+ });
191
+ }
192
+ else {
193
+ return this.query(stmt.query, stmt.params, this.map).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
194
+ }
195
+ };
196
+ SqlRepository.prototype.exist = function (id, ctx) {
197
+ var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
198
+ var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
199
+ if (!stmt) {
200
+ throw new Error("cannot build query by id");
201
+ }
202
+ return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
203
+ };
204
+ SqlRepository.prototype.delete = function (id, ctx) {
205
+ var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.param);
206
+ if (stmt) {
207
+ return this.exec(stmt.query, stmt.params, ctx);
208
+ }
209
+ else {
210
+ return Promise.resolve(0);
211
+ }
212
+ };
213
+ return SqlRepository;
214
+ }(SqlSearchWriter));
215
+ exports.SqlRepository = SqlRepository;
96
216
  var Query = (function (_super) {
97
217
  __extends(Query, _super);
98
- function Query(query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) {
99
- var _this = _super.call(this, query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
218
+ function Query(query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total) {
219
+ var _this = _super.call(this, query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
100
220
  var m = build_1.metadata(attributes);
101
221
  _this.primaryKeys = m.keys;
102
222
  _this.map = m.map;
@@ -110,7 +230,7 @@ var Query = (function (_super) {
110
230
  return _this;
111
231
  }
112
232
  Query.prototype.metadata = function () {
113
- return this.attributes;
233
+ return this.attrs;
114
234
  };
115
235
  Query.prototype.all = function () {
116
236
  var sql = "select * from " + this.table;
package/lib/index.js CHANGED
@@ -10,11 +10,12 @@ var services_1 = require("./services");
10
10
  exports.SqlViewRepository = services_1.SqlLoader;
11
11
  exports.SqlLoadService = services_1.SqlLoader;
12
12
  exports.SqlViewService = services_1.SqlLoader;
13
- exports.Repository = services_1.SqlWriter;
14
- exports.SqlGenericRepository = services_1.SqlWriter;
15
13
  exports.SqlGenericService = services_1.SqlWriter;
14
+ exports.GenericRepository = services_1.CRUDRepository;
15
+ exports.SqlGenericRepository = services_1.CRUDRepository;
16
16
  var SearchBuilder_1 = require("./SearchBuilder");
17
17
  exports.SearchRepository = SearchBuilder_1.SearchBuilder;
18
+ exports.Repository = SearchBuilder_1.SqlRepository;
18
19
  __export(require("./batch"));
19
20
  __export(require("./build"));
20
21
  __export(require("./client"));
package/lib/query.js CHANGED
@@ -56,25 +56,12 @@ function buildDollarParam(i) {
56
56
  return "$" + i;
57
57
  }
58
58
  exports.buildDollarParam = buildDollarParam;
59
- function buildQuery(filter, bparam, sort, buildSort3, attrs, table, fields, sq, strExcluding) {
59
+ function buildQuery(filter, param, sort, buildSort3, attrs, table, fields, sq, strExcluding, likeType) {
60
60
  if (!table || !attrs) {
61
61
  return undefined;
62
62
  }
63
63
  var s = filter;
64
- var like = "like";
65
- var param;
66
- if (typeof bparam === "string") {
67
- if (bparam === "ilike") {
68
- like = bparam;
69
- }
70
- param = buildDollarParam;
71
- }
72
- else {
73
- param = bparam;
74
- }
75
- if (!like) {
76
- like = "like";
77
- }
64
+ var like = likeType ? likeType : "like";
78
65
  var filters = [];
79
66
  var q;
80
67
  var excluding;
package/lib/services.js CHANGED
@@ -113,139 +113,6 @@ var QueryRepository = (function () {
113
113
  return QueryRepository;
114
114
  }());
115
115
  exports.QueryRepository = QueryRepository;
116
- var SqlLoadRepository = (function () {
117
- function SqlLoadRepository(query, table, attrs, param, id1Field, id2Field, fromDB, id1Col, id2Col) {
118
- this.query = query;
119
- this.table = table;
120
- this.param = param;
121
- this.id1Field = id1Field;
122
- this.id2Field = id2Field;
123
- this.fromDB = fromDB;
124
- var m = build_1.metadata(attrs);
125
- this.attributes = attrs;
126
- this.map = m.map;
127
- this.bools = m.bools;
128
- if (this.metadata) {
129
- this.metadata = this.metadata.bind(this);
130
- }
131
- this.all = this.all.bind(this);
132
- this.load = this.load.bind(this);
133
- this.exist = this.exist.bind(this);
134
- if (id1Col && id1Col.length > 0) {
135
- this.id1Col = id1Col;
136
- }
137
- else {
138
- var c = attrs[this.id1Field];
139
- if (c) {
140
- this.id1Col = c.column && c.column.length > 0 ? c.column : this.id1Field;
141
- }
142
- else {
143
- this.id1Col = this.id1Field;
144
- }
145
- }
146
- if (id2Col && id2Col.length > 0) {
147
- this.id2Col = id2Col;
148
- }
149
- else {
150
- var c = attrs[this.id2Field];
151
- if (c) {
152
- this.id2Col = c.column && c.column.length > 0 ? c.column : this.id2Field;
153
- }
154
- else {
155
- this.id2Col = this.id2Field;
156
- }
157
- }
158
- }
159
- SqlLoadRepository.prototype.metadata = function () {
160
- return this.attributes;
161
- };
162
- SqlLoadRepository.prototype.all = function () {
163
- var sql = "select * from " + this.table;
164
- return this.query(sql, [], this.map);
165
- };
166
- SqlLoadRepository.prototype.load = function (id1, id2, ctx) {
167
- var _this = this;
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) {
169
- if (!objs || objs.length === 0) {
170
- return null;
171
- }
172
- else {
173
- var fn = _this.fromDB;
174
- if (fn) {
175
- return fn(objs[0]);
176
- }
177
- else {
178
- return objs[0];
179
- }
180
- }
181
- });
182
- };
183
- SqlLoadRepository.prototype.exist = function (id1, id2, ctx) {
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) {
185
- return objs && objs.length > 0 ? true : false;
186
- });
187
- };
188
- return SqlLoadRepository;
189
- }());
190
- exports.SqlLoadRepository = SqlLoadRepository;
191
- var GenericRepository = (function (_super) {
192
- __extends(GenericRepository, _super);
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;
195
- _this.toDB = toDB;
196
- var x = build_1.version(attrs);
197
- _this.exec = manager.exec;
198
- if (x) {
199
- _this.version = x.name;
200
- }
201
- _this.create = _this.create.bind(_this);
202
- _this.update = _this.update.bind(_this);
203
- _this.patch = _this.patch.bind(_this);
204
- _this.delete = _this.delete.bind(_this);
205
- return _this;
206
- }
207
- GenericRepository.prototype.create = function (obj, ctx) {
208
- var obj2 = obj;
209
- if (this.toDB) {
210
- obj2 = this.toDB(obj);
211
- }
212
- var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
213
- if (stmt) {
214
- return this.exec(stmt.query, stmt.params, ctx).catch(function (err) {
215
- if (err && err.error === "duplicate") {
216
- return 0;
217
- }
218
- else {
219
- throw err;
220
- }
221
- });
222
- }
223
- else {
224
- return Promise.resolve(0);
225
- }
226
- };
227
- GenericRepository.prototype.update = function (obj, ctx) {
228
- var obj2 = obj;
229
- if (this.toDB) {
230
- obj2 = this.toDB(obj);
231
- }
232
- var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
233
- if (stmt) {
234
- return this.exec(stmt.query, stmt.params, ctx);
235
- }
236
- else {
237
- return Promise.resolve(0);
238
- }
239
- };
240
- GenericRepository.prototype.patch = function (obj, ctx) {
241
- return this.update(obj, ctx);
242
- };
243
- GenericRepository.prototype.delete = function (id1, id2, ctx) {
244
- return this.exec("delete from " + this.table + " where " + this.id1Col + " = " + this.param(1) + " and " + this.id2Col + " = " + this.param(2), [id1, id2], ctx);
245
- };
246
- return GenericRepository;
247
- }(SqlLoadRepository));
248
- exports.GenericRepository = GenericRepository;
249
116
  function log(db, isLog, logger, q, result, r, duration) {
250
117
  if (!isLog) {
251
118
  return db;
@@ -538,21 +405,20 @@ function diff(d1) {
538
405
  return d2.getTime() - d1.getTime();
539
406
  }
540
407
  exports.diff = diff;
541
- var SqlWriter = (function (_super) {
542
- __extends(SqlWriter, _super);
543
- function SqlWriter(manager, table, attrs, toDB, fromDB) {
544
- var _this = _super.call(this, manager.query, table, attrs, manager.param, fromDB) || this;
545
- _this.toDB = toDB;
546
- var x = build_1.version(attrs);
547
- _this.exec = manager.exec;
408
+ var SqlWriter = (function () {
409
+ function SqlWriter(exec, param, table, attributes, toDB) {
410
+ this.exec = exec;
411
+ this.param = param;
412
+ this.table = table;
413
+ this.attributes = attributes;
414
+ this.toDB = toDB;
415
+ var x = build_1.version(attributes);
548
416
  if (x) {
549
- _this.version = x.name;
417
+ this.version = x.name;
550
418
  }
551
- _this.create = _this.create.bind(_this);
552
- _this.update = _this.update.bind(_this);
553
- _this.patch = _this.patch.bind(_this);
554
- _this.delete = _this.delete.bind(_this);
555
- return _this;
419
+ this.create = this.create.bind(this);
420
+ this.update = this.update.bind(this);
421
+ this.patch = this.patch.bind(this);
556
422
  }
557
423
  SqlWriter.prototype.create = function (obj, ctx) {
558
424
  var obj2 = obj;
@@ -590,7 +456,63 @@ var SqlWriter = (function (_super) {
590
456
  SqlWriter.prototype.patch = function (obj, ctx) {
591
457
  return this.update(obj, ctx);
592
458
  };
593
- SqlWriter.prototype.delete = function (id, ctx) {
459
+ return SqlWriter;
460
+ }());
461
+ exports.SqlWriter = SqlWriter;
462
+ var CRUDRepository = (function (_super) {
463
+ __extends(CRUDRepository, _super);
464
+ function CRUDRepository(manager, table, attributes, toDB, fromDB) {
465
+ var _this = _super.call(this, manager.exec, manager.param, table, attributes, toDB) || this;
466
+ _this.fromDB = fromDB;
467
+ _this.query = manager.query;
468
+ var m = build_1.metadata(attributes);
469
+ _this.primaryKeys = m.keys;
470
+ _this.map = m.map;
471
+ _this.bools = m.bools;
472
+ _this.metadata = _this.metadata.bind(_this);
473
+ _this.all = _this.all.bind(_this);
474
+ _this.load = _this.load.bind(_this);
475
+ _this.exist = _this.exist.bind(_this);
476
+ _this.delete = _this.delete.bind(_this);
477
+ return _this;
478
+ }
479
+ CRUDRepository.prototype.metadata = function () {
480
+ return this.attributes;
481
+ };
482
+ CRUDRepository.prototype.all = function () {
483
+ var sql = "select * from " + this.table;
484
+ return this.query(sql, [], this.map);
485
+ };
486
+ CRUDRepository.prototype.load = function (id, ctx) {
487
+ var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
488
+ if (!stmt) {
489
+ throw new Error("cannot build query by id");
490
+ }
491
+ var fn = this.fromDB;
492
+ if (fn) {
493
+ return this.query(stmt.query, stmt.params, this.map, ctx).then(function (res) {
494
+ if (!res || res.length === 0) {
495
+ return null;
496
+ }
497
+ else {
498
+ var obj = res[0];
499
+ return fn(obj);
500
+ }
501
+ });
502
+ }
503
+ else {
504
+ return this.query(stmt.query, stmt.params, this.map).then(function (res) { return (!res || res.length === 0 ? null : res[0]); });
505
+ }
506
+ };
507
+ CRUDRepository.prototype.exist = function (id, ctx) {
508
+ var field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name;
509
+ var stmt = build_1.exist(id, this.table, this.primaryKeys, this.param, field);
510
+ if (!stmt) {
511
+ throw new Error("cannot build query by id");
512
+ }
513
+ return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(function (res) { return (!res || res.length === 0 ? false : true); });
514
+ };
515
+ CRUDRepository.prototype.delete = function (id, ctx) {
594
516
  var stmt = build_1.buildToDelete(id, this.table, this.primaryKeys, this.param);
595
517
  if (stmt) {
596
518
  return this.exec(stmt.query, stmt.params, ctx);
@@ -599,6 +521,6 @@ var SqlWriter = (function (_super) {
599
521
  return Promise.resolve(0);
600
522
  }
601
523
  };
602
- return SqlWriter;
603
- }(SqlLoader));
604
- exports.SqlWriter = SqlWriter;
524
+ return CRUDRepository;
525
+ }(SqlWriter));
526
+ exports.CRUDRepository = CRUDRepository;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.2.7",
3
+ "version": "0.2.8",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -1,4 +1,5 @@
1
- import { exist, metadata, param, select } from "./build"
1
+ import { Manager } from "./services"
2
+ import { buildToDelete, buildToInsert, buildToUpdate, exist, metadata, param, select, version } from "./build"
2
3
  import { Attribute, Attributes, Statement, StringMap } from "./metadata"
3
4
  import { buildSort as bs, buildDollarParam, buildMsSQLParam, buildOracleParam, buildQuery, LikeType } from "./query"
4
5
  import { buildFromQuery, oracle, SearchResult } from "./search"
@@ -10,10 +11,11 @@ export const sqlite = "sqlite"
10
11
  export class SearchBuilder<T, S> {
11
12
  map?: StringMap
12
13
  bools?: Attribute[]
14
+ primaryKeys: Attribute[]
13
15
  protected deleteSort?: boolean
14
16
  buildQuery: (
15
17
  s: S,
16
- bparam: LikeType | ((i: number) => string),
18
+ param: (i: number) => string,
17
19
  sort?: string,
18
20
  buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
19
21
  attrs?: Attributes,
@@ -21,6 +23,7 @@ export class SearchBuilder<T, S> {
21
23
  fields?: string[],
22
24
  sq?: string,
23
25
  strExcluding?: string,
26
+ likeType?: LikeType
24
27
  ) => Statement | undefined
25
28
  q?: string
26
29
  excluding?: string
@@ -28,13 +31,12 @@ export class SearchBuilder<T, S> {
28
31
  param: (i: number) => string
29
32
  total?: string
30
33
  constructor(
31
- public query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
32
- public table: string,
33
- public attributes?: Attributes,
34
- public provider?: string,
34
+ protected query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
35
+ protected table: string,
36
+ protected attrs?: Attributes,
35
37
  buildQ?: (
36
38
  s: S,
37
- bparam: LikeType | ((i: number) => string),
39
+ bparam: (i: number) => string,
38
40
  sort?: string,
39
41
  buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
40
42
  attrs?: Attributes,
@@ -42,20 +44,25 @@ export class SearchBuilder<T, S> {
42
44
  fields?: string[],
43
45
  sq?: string,
44
46
  strExcluding?: string,
47
+ likeType?: LikeType
45
48
  ) => Statement | undefined,
46
- public fromDB?: (v: T) => T,
47
- public sort?: string,
49
+ protected provider?: string,
50
+ protected fromDB?: (v: T) => T,
51
+ protected sort?: string,
48
52
  q?: string,
49
53
  excluding?: string,
50
54
  buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
51
55
  buildParam?: (i: number) => string,
52
56
  total?: string,
53
57
  ) {
54
- if (attributes) {
55
- this.attributes = attributes
56
- const meta = metadata(attributes)
58
+ if (attrs) {
59
+ this.attrs = attrs
60
+ const meta = metadata(attrs)
57
61
  this.map = meta.map
58
62
  this.bools = meta.bools
63
+ this.primaryKeys = meta.keys
64
+ } else {
65
+ this.primaryKeys = []
59
66
  }
60
67
  this.deleteSort = buildQ ? undefined : true
61
68
  this.buildQuery = buildQ ? buildQ : buildQuery
@@ -86,10 +93,10 @@ export class SearchBuilder<T, S> {
86
93
  const st = this.sort ? this.sort : "sort"
87
94
  const sn = (filter as any)[st] as string
88
95
  if (this.deleteSort) {
89
- delete (filter as any)[st]
96
+ delete (filter as any)[st]
90
97
  }
91
- const x = this.provider === postgres ? "ilike" : this.param
92
- const q2 = this.buildQuery(filter, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding)
98
+ const likeType = this.provider === postgres ? "ilike" : "like"
99
+ const q2 = this.buildQuery(filter, this.param, sn, this.buildSort, this.attrs, this.table, fields, this.q, this.excluding, likeType)
93
100
  if (!q2) {
94
101
  throw new Error("Cannot build query")
95
102
  }
@@ -108,6 +115,156 @@ export class SearchBuilder<T, S> {
108
115
  }
109
116
  }
110
117
  }
118
+ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
119
+ protected version?: string
120
+ protected exec: (sql: string, args?: any[], ctx?: any) => Promise<number>
121
+ constructor(
122
+ manager: Manager,
123
+ table: string,
124
+ protected attributes: Attributes,
125
+ buildQ?: (
126
+ s: S,
127
+ param: (i: number) => string,
128
+ sort?: string,
129
+ buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
130
+ attrs?: Attributes,
131
+ table?: string,
132
+ fields?: string[],
133
+ sq?: string,
134
+ strExcluding?: string,
135
+ likeType?: LikeType
136
+ ) => Statement | undefined,
137
+ provider?: string,
138
+ protected toDB?: (v: T) => T,
139
+ fromDB?: (v: T) => T,
140
+ sort?: string,
141
+ q?: string,
142
+ excluding?: string,
143
+ buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
144
+ buildParam?: (i: number) => string,
145
+ total?: string,
146
+ ) {
147
+ super(manager.query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total)
148
+ this.exec = manager.exec
149
+ const x = version(attributes)
150
+ if (x) {
151
+ this.version = x.name
152
+ }
153
+ this.create = this.create.bind(this)
154
+ this.update = this.update.bind(this)
155
+ this.patch = this.patch.bind(this)
156
+ }
157
+ create(obj: T, ctx?: any): Promise<number> {
158
+ let obj2 = obj
159
+ if (this.toDB) {
160
+ obj2 = this.toDB(obj)
161
+ }
162
+ const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version)
163
+ if (stmt) {
164
+ return this.exec(stmt.query, stmt.params, ctx).catch((err) => {
165
+ if (err && err.error === "duplicate") {
166
+ return 0
167
+ } else {
168
+ throw err
169
+ }
170
+ })
171
+ } else {
172
+ return Promise.resolve(0)
173
+ }
174
+ }
175
+ update(obj: T, ctx?: any): Promise<number> {
176
+ let obj2 = obj
177
+ if (this.toDB) {
178
+ obj2 = this.toDB(obj)
179
+ }
180
+ const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version)
181
+ if (stmt) {
182
+ return this.exec(stmt.query, stmt.params, ctx)
183
+ } else {
184
+ return Promise.resolve(0)
185
+ }
186
+ }
187
+ patch(obj: Partial<T>, ctx?: any): Promise<number> {
188
+ return this.update(obj as any, ctx)
189
+ }
190
+ }
191
+ // tslint:disable-next-line:max-classes-per-file
192
+ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
193
+ constructor(
194
+ manager: Manager,
195
+ table: string,
196
+ protected attributes: Attributes,
197
+ buildQ?: (
198
+ s: S,
199
+ bparam: LikeType | ((i: number) => string),
200
+ sort?: string,
201
+ buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
202
+ attrs?: Attributes,
203
+ table?: string,
204
+ fields?: string[],
205
+ sq?: string,
206
+ strExcluding?: string,
207
+ ) => Statement | undefined,
208
+ provider?: string,
209
+ protected toDB?: (v: T) => T,
210
+ fromDB?: (v: T) => T,
211
+ sort?: string,
212
+ q?: string,
213
+ excluding?: string,
214
+ buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
215
+ buildParam?: (i: number) => string,
216
+ total?: string,
217
+ ) {
218
+ super(manager, table, attributes, buildQ, provider, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total)
219
+ this.metadata = this.metadata.bind(this)
220
+ this.all = this.all.bind(this)
221
+ this.load = this.load.bind(this)
222
+ this.exist = this.exist.bind(this)
223
+ this.delete = this.delete.bind(this)
224
+ }
225
+ metadata(): Attributes {
226
+ return this.attributes
227
+ }
228
+ all(): Promise<T[]> {
229
+ const sql = `select * from ${this.table}`
230
+ return this.query(sql, [], this.map)
231
+ }
232
+ load(id: ID, ctx?: any): Promise<T | null> {
233
+ const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
234
+ if (!stmt) {
235
+ throw new Error("cannot build query by id")
236
+ }
237
+ const fn = this.fromDB
238
+ if (fn) {
239
+ return this.query<T>(stmt.query, stmt.params, this.map, ctx).then((res) => {
240
+ if (!res || res.length === 0) {
241
+ return null
242
+ } else {
243
+ const obj = res[0]
244
+ return fn(obj)
245
+ }
246
+ })
247
+ } else {
248
+ return this.query<T>(stmt.query, stmt.params, this.map).then((res) => (!res || res.length === 0 ? null : res[0]))
249
+ }
250
+ }
251
+ exist(id: ID, ctx?: any): Promise<boolean> {
252
+ const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
253
+ const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
254
+ if (!stmt) {
255
+ throw new Error("cannot build query by id")
256
+ }
257
+ return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then((res) => (!res || res.length === 0 ? false : true))
258
+ }
259
+ delete(id: ID, ctx?: any): Promise<number> {
260
+ const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param)
261
+ if (stmt) {
262
+ return this.exec(stmt.query, stmt.params, ctx)
263
+ } else {
264
+ return Promise.resolve(0)
265
+ }
266
+ }
267
+ }
111
268
  // tslint:disable-next-line:max-classes-per-file
112
269
  export class Query<T, ID, S> extends SearchBuilder<T, S> {
113
270
  primaryKeys: Attribute[]
@@ -118,7 +275,6 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
118
275
  query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
119
276
  table: string,
120
277
  attributes: Attributes,
121
- provider?: string,
122
278
  buildQ?: (
123
279
  s: S,
124
280
  bparam: LikeType | ((i: number) => string),
@@ -130,6 +286,7 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
130
286
  sq?: string,
131
287
  strExcluding?: string,
132
288
  ) => Statement | undefined,
289
+ provider?: string,
133
290
  fromDB?: (v: T) => T,
134
291
  sort?: string,
135
292
  q?: string,
@@ -138,7 +295,7 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
138
295
  buildParam?: (i: number) => string,
139
296
  total?: string,
140
297
  ) {
141
- super(query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total)
298
+ super(query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total)
142
299
  const m = metadata(attributes)
143
300
  this.primaryKeys = m.keys
144
301
  this.map = m.map
@@ -151,7 +308,7 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
151
308
  this.exist = this.exist.bind(this)
152
309
  }
153
310
  metadata?(): Attributes | undefined {
154
- return this.attributes
311
+ return this.attrs
155
312
  }
156
313
  all(): Promise<T[]> {
157
314
  const sql = `select * from ${this.table}`
package/src/index.ts CHANGED
@@ -3,14 +3,14 @@ export { Checker as SqlChecker }
3
3
 
4
4
  import { resource } from "./build"
5
5
  import { Attribute, StringMap } from "./metadata"
6
- import { SqlLoader, SqlWriter } from "./services"
6
+ import { SqlLoader, SqlWriter, CRUDRepository } from "./services"
7
7
  // export {SqlLoader as SqlLoadRepository};
8
- export { SqlWriter as Repository, SqlWriter as SqlGenericRepository, SqlLoader as SqlViewRepository }
8
+ export { CRUDRepository as GenericRepository, CRUDRepository as SqlGenericRepository, SqlLoader as SqlViewRepository }
9
9
 
10
10
  export { SqlWriter as SqlGenericService, SqlLoader as SqlLoadService, SqlLoader as SqlViewService }
11
11
 
12
- import { SearchBuilder } from "./SearchBuilder"
13
- export { SearchBuilder as SearchRepository}
12
+ import { SearchBuilder, SqlRepository } from "./SearchBuilder"
13
+ export { SearchBuilder as SearchRepository, SqlRepository as Repository }
14
14
 
15
15
  export * from "./batch"
16
16
  export * from "./build"
package/src/query.ts CHANGED
@@ -54,7 +54,7 @@ export function buildDollarParam(i: number): string {
54
54
  }
55
55
  export function buildQuery<S>(
56
56
  filter: S,
57
- bparam: LikeType | ((i: number) => string),
57
+ param: ((i: number) => string),
58
58
  sort?: string,
59
59
  buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
60
60
  attrs?: Attributes,
@@ -62,24 +62,14 @@ export function buildQuery<S>(
62
62
  fields?: string[],
63
63
  sq?: string,
64
64
  strExcluding?: string,
65
+ likeType?: LikeType
65
66
  ): Statement | undefined {
66
67
  if (!table || !attrs) {
67
68
  return undefined
68
69
  }
69
70
  const s: any = filter
70
- let like = "like"
71
- let param: (i: number) => string
72
- if (typeof bparam === "string") {
73
- if (bparam === "ilike") {
74
- like = bparam
75
- }
76
- param = buildDollarParam
77
- } else {
78
- param = bparam
79
- }
80
- if (!like) {
81
- like = "like"
82
- }
71
+ let like = likeType ? likeType : "like"
72
+ // let param: (i: number) => string
83
73
  const filters: string[] = []
84
74
  let q: string | undefined
85
75
  let excluding: string[] | number[] | undefined
package/src/services.ts CHANGED
@@ -26,11 +26,11 @@ export class SqlLoader<T, ID> {
26
26
  attributes: Attributes
27
27
  bools?: Attribute[]
28
28
  constructor(
29
- public query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
30
- public table: string,
29
+ protected query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
30
+ protected table: string,
31
31
  attrs: Attributes | string[],
32
- public param: (i: number) => string,
33
- public fromDB?: (v: T) => T,
32
+ protected param: (i: number) => string,
33
+ protected fromDB?: (v: T) => T,
34
34
  ) {
35
35
  if (Array.isArray(attrs)) {
36
36
  this.primaryKeys = attributes(attrs)
@@ -112,6 +112,7 @@ export class QueryRepository<T, ID> {
112
112
  return this.db.query<T>(sql, ids, this.map, this.bools)
113
113
  }
114
114
  }
115
+ /*
115
116
  // tslint:disable-next-line:max-classes-per-file
116
117
  export class SqlLoadRepository<T, K1, K2> {
117
118
  map?: StringMap
@@ -266,6 +267,7 @@ export class GenericRepository<T, K1, K2> extends SqlLoadRepository<T, K1, K2> {
266
267
  return this.exec(`delete from ${this.table} where ${this.id1Col} = ${this.param(1)} and ${this.id2Col} = ${this.param(2)}`, [id1, id2], ctx)
267
268
  }
268
269
  }
270
+ */
269
271
  /*
270
272
  // tslint:disable-next-line:max-classes-per-file
271
273
  export class SqlSearchLoader<T, ID, S extends Filter> extends SqlLoader<T, ID> {
@@ -628,14 +630,12 @@ const getDurationInMilliseconds = (start: [number, number] | undefined) => {
628
630
  };
629
631
  */
630
632
  // tslint:disable-next-line:max-classes-per-file
631
- export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
632
- version?: string
633
- exec: (sql: string, args?: any[], ctx?: any) => Promise<number>
633
+ export class SqlWriter<T> {
634
+ protected version?: string
634
635
  // execBatch: (statements: Statement[], firstSuccess?: boolean, ctx?: any) => Promise<number>
635
- constructor(manager: Manager, table: string, attrs: Attributes, public toDB?: (v: T) => T, fromDB?: (v: T) => T) {
636
- super(manager.query, table, attrs, manager.param, fromDB)
637
- const x = version(attrs)
638
- this.exec = manager.exec
636
+ constructor(protected exec: (sql: string, args?: any[], ctx?: any) => Promise<number>, protected param: (i: number) => string, protected table: string, protected attributes: Attributes, public toDB?: (v: T) => T) {
637
+ // super(manager.query, table, attrs, manager.param, fromDB)
638
+ const x = version(attributes)
639
639
  // this.execBatch = manager.execBatch
640
640
  if (x) {
641
641
  this.version = x.name
@@ -643,7 +643,7 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
643
643
  this.create = this.create.bind(this)
644
644
  this.update = this.update.bind(this)
645
645
  this.patch = this.patch.bind(this)
646
- this.delete = this.delete.bind(this)
646
+ // this.delete = this.delete.bind(this)
647
647
  }
648
648
  create(obj: T, ctx?: any): Promise<number> {
649
649
  let obj2 = obj
@@ -678,6 +678,70 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
678
678
  patch(obj: Partial<T>, ctx?: any): Promise<number> {
679
679
  return this.update(obj as any, ctx)
680
680
  }
681
+ /*
682
+ delete(id: ID, ctx?: any): Promise<number> {
683
+ const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param)
684
+ if (stmt) {
685
+ return this.exec(stmt.query, stmt.params, ctx)
686
+ } else {
687
+ return Promise.resolve(0)
688
+ }
689
+ }
690
+ */
691
+ }
692
+ export class CRUDRepository<T, ID> extends SqlWriter<T> {
693
+ protected primaryKeys: Attribute[]
694
+ protected map?: StringMap
695
+ // attributes: Attributes;
696
+ protected bools?: Attribute[]
697
+ protected query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>
698
+ constructor(manager: Manager, table: string, attributes: Attributes, toDB?: (v: T) => T, protected fromDB?: (v: T) => T) {
699
+ super(manager.exec, manager.param, table, attributes, toDB)
700
+ this.query = manager.query
701
+ const m = metadata(attributes)
702
+ this.primaryKeys = m.keys
703
+ this.map = m.map
704
+ this.bools = m.bools
705
+ this.metadata = this.metadata.bind(this)
706
+ this.all = this.all.bind(this)
707
+ this.load = this.load.bind(this)
708
+ this.exist = this.exist.bind(this)
709
+ this.delete = this.delete.bind(this)
710
+ }
711
+ metadata(): Attributes {
712
+ return this.attributes
713
+ }
714
+ all(): Promise<T[]> {
715
+ const sql = `select * from ${this.table}`
716
+ return this.query(sql, [], this.map)
717
+ }
718
+ load(id: ID, ctx?: any): Promise<T | null> {
719
+ const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
720
+ if (!stmt) {
721
+ throw new Error("cannot build query by id")
722
+ }
723
+ const fn = this.fromDB
724
+ if (fn) {
725
+ return this.query<T>(stmt.query, stmt.params, this.map, ctx).then((res) => {
726
+ if (!res || res.length === 0) {
727
+ return null
728
+ } else {
729
+ const obj = res[0]
730
+ return fn(obj)
731
+ }
732
+ })
733
+ } else {
734
+ return this.query<T>(stmt.query, stmt.params, this.map).then((res) => (!res || res.length === 0 ? null : res[0]))
735
+ }
736
+ }
737
+ exist(id: ID, ctx?: any): Promise<boolean> {
738
+ const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
739
+ const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
740
+ if (!stmt) {
741
+ throw new Error("cannot build query by id")
742
+ }
743
+ return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then((res) => (!res || res.length === 0 ? false : true))
744
+ }
681
745
  delete(id: ID, ctx?: any): Promise<number> {
682
746
  const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param)
683
747
  if (stmt) {