query-core 0.2.7 → 0.3.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.
- package/lib/SearchBuilder.js +129 -9
- package/lib/index.js +3 -2
- package/lib/query.js +2 -15
- package/lib/services.js +72 -150
- package/package.json +1 -1
- package/src/SearchBuilder.ts +177 -18
- package/src/index.ts +4 -4
- package/src/query.ts +4 -14
- package/src/services.ts +76 -12
package/lib/SearchBuilder.js
CHANGED
|
@@ -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,
|
|
24
|
+
function SearchBuilder(query, table, attrs, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) {
|
|
25
25
|
this.query = query;
|
|
26
26
|
this.table = table;
|
|
27
|
-
this.
|
|
27
|
+
this.attrs = attrs;
|
|
28
28
|
this.provider = provider;
|
|
29
29
|
this.fromDB = fromDB;
|
|
30
30
|
this.sort = sort;
|
|
31
|
-
if (
|
|
32
|
-
this.
|
|
33
|
-
var meta = build_1.metadata(
|
|
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
|
|
73
|
-
var q2 = this.buildQuery(filter,
|
|
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,9 +97,125 @@ 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, provider, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
|
|
103
|
+
var _this = _super.call(this, manager.query, table, attributes, provider, buildQ, 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, provider, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
|
|
158
|
+
var _this = _super.call(this, manager, table, attributes, provider, buildQ, 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,
|
|
218
|
+
function Query(query, table, attributes, buildQ, provider, fromDB, sort, q, excluding, buildSort, buildParam, total) {
|
|
99
219
|
var _this = _super.call(this, query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
|
|
100
220
|
var m = build_1.metadata(attributes);
|
|
101
221
|
_this.primaryKeys = m.keys;
|
|
@@ -110,7 +230,7 @@ var Query = (function (_super) {
|
|
|
110
230
|
return _this;
|
|
111
231
|
}
|
|
112
232
|
Query.prototype.metadata = function () {
|
|
113
|
-
return this.
|
|
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,
|
|
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 (
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
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
|
-
|
|
417
|
+
this.version = x.name;
|
|
550
418
|
}
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
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
|
|
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
|
|
603
|
-
}(
|
|
604
|
-
exports.
|
|
524
|
+
return CRUDRepository;
|
|
525
|
+
}(SqlWriter));
|
|
526
|
+
exports.CRUDRepository = CRUDRepository;
|
package/package.json
CHANGED
package/src/SearchBuilder.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
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,13 @@ export class SearchBuilder<T, S> {
|
|
|
28
31
|
param: (i: number) => string
|
|
29
32
|
total?: string
|
|
30
33
|
constructor(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
protected query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
|
|
35
|
+
protected table: string,
|
|
36
|
+
protected attrs?: Attributes,
|
|
37
|
+
protected provider?: string,
|
|
35
38
|
buildQ?: (
|
|
36
39
|
s: S,
|
|
37
|
-
|
|
40
|
+
param: (i: number) => string,
|
|
38
41
|
sort?: string,
|
|
39
42
|
buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
|
|
40
43
|
attrs?: Attributes,
|
|
@@ -42,20 +45,24 @@ export class SearchBuilder<T, S> {
|
|
|
42
45
|
fields?: string[],
|
|
43
46
|
sq?: string,
|
|
44
47
|
strExcluding?: string,
|
|
48
|
+
likeType?: LikeType
|
|
45
49
|
) => Statement | undefined,
|
|
46
|
-
|
|
47
|
-
|
|
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 (
|
|
55
|
-
this.
|
|
56
|
-
const meta = metadata(
|
|
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
|
|
92
|
-
const q2 = this.buildQuery(filter,
|
|
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,157 @@ 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
|
+
provider?: string,
|
|
126
|
+
buildQ?: (
|
|
127
|
+
s: S,
|
|
128
|
+
param: (i: number) => string,
|
|
129
|
+
sort?: string,
|
|
130
|
+
buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
|
|
131
|
+
attrs?: Attributes,
|
|
132
|
+
table?: string,
|
|
133
|
+
fields?: string[],
|
|
134
|
+
sq?: string,
|
|
135
|
+
strExcluding?: string,
|
|
136
|
+
likeType?: LikeType
|
|
137
|
+
) => Statement | undefined,
|
|
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, provider, buildQ, 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
|
+
provider?: string,
|
|
198
|
+
buildQ?: (
|
|
199
|
+
s: S,
|
|
200
|
+
param: (i: number) => string,
|
|
201
|
+
sort?: string,
|
|
202
|
+
buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
|
|
203
|
+
attrs?: Attributes,
|
|
204
|
+
table?: string,
|
|
205
|
+
fields?: string[],
|
|
206
|
+
sq?: string,
|
|
207
|
+
strExcluding?: string,
|
|
208
|
+
likeType?: LikeType
|
|
209
|
+
) => Statement | undefined,
|
|
210
|
+
protected toDB?: (v: T) => T,
|
|
211
|
+
fromDB?: (v: T) => T,
|
|
212
|
+
sort?: string,
|
|
213
|
+
q?: string,
|
|
214
|
+
excluding?: string,
|
|
215
|
+
buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
|
|
216
|
+
buildParam?: (i: number) => string,
|
|
217
|
+
total?: string,
|
|
218
|
+
) {
|
|
219
|
+
super(manager, table, attributes, provider, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total)
|
|
220
|
+
this.metadata = this.metadata.bind(this)
|
|
221
|
+
this.all = this.all.bind(this)
|
|
222
|
+
this.load = this.load.bind(this)
|
|
223
|
+
this.exist = this.exist.bind(this)
|
|
224
|
+
this.delete = this.delete.bind(this)
|
|
225
|
+
}
|
|
226
|
+
metadata(): Attributes {
|
|
227
|
+
return this.attributes
|
|
228
|
+
}
|
|
229
|
+
all(): Promise<T[]> {
|
|
230
|
+
const sql = `select * from ${this.table}`
|
|
231
|
+
return this.query(sql, [], this.map)
|
|
232
|
+
}
|
|
233
|
+
load(id: ID, ctx?: any): Promise<T | null> {
|
|
234
|
+
const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
|
|
235
|
+
if (!stmt) {
|
|
236
|
+
throw new Error("cannot build query by id")
|
|
237
|
+
}
|
|
238
|
+
const fn = this.fromDB
|
|
239
|
+
if (fn) {
|
|
240
|
+
return this.query<T>(stmt.query, stmt.params, this.map, ctx).then((res) => {
|
|
241
|
+
if (!res || res.length === 0) {
|
|
242
|
+
return null
|
|
243
|
+
} else {
|
|
244
|
+
const obj = res[0]
|
|
245
|
+
return fn(obj)
|
|
246
|
+
}
|
|
247
|
+
})
|
|
248
|
+
} else {
|
|
249
|
+
return this.query<T>(stmt.query, stmt.params, this.map).then((res) => (!res || res.length === 0 ? null : res[0]))
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
exist(id: ID, ctx?: any): Promise<boolean> {
|
|
253
|
+
const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
|
|
254
|
+
const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
|
|
255
|
+
if (!stmt) {
|
|
256
|
+
throw new Error("cannot build query by id")
|
|
257
|
+
}
|
|
258
|
+
return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then((res) => (!res || res.length === 0 ? false : true))
|
|
259
|
+
}
|
|
260
|
+
delete(id: ID, ctx?: any): Promise<number> {
|
|
261
|
+
const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param)
|
|
262
|
+
if (stmt) {
|
|
263
|
+
return this.exec(stmt.query, stmt.params, ctx)
|
|
264
|
+
} else {
|
|
265
|
+
return Promise.resolve(0)
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
111
269
|
// tslint:disable-next-line:max-classes-per-file
|
|
112
270
|
export class Query<T, ID, S> extends SearchBuilder<T, S> {
|
|
113
271
|
primaryKeys: Attribute[]
|
|
@@ -118,10 +276,9 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
|
|
|
118
276
|
query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
|
|
119
277
|
table: string,
|
|
120
278
|
attributes: Attributes,
|
|
121
|
-
provider?: string,
|
|
122
279
|
buildQ?: (
|
|
123
280
|
s: S,
|
|
124
|
-
|
|
281
|
+
param: (i: number) => string,
|
|
125
282
|
sort?: string,
|
|
126
283
|
buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
|
|
127
284
|
attrs?: Attributes,
|
|
@@ -129,7 +286,9 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
|
|
|
129
286
|
fields?: string[],
|
|
130
287
|
sq?: string,
|
|
131
288
|
strExcluding?: string,
|
|
289
|
+
likeType?: LikeType
|
|
132
290
|
) => Statement | undefined,
|
|
291
|
+
provider?: string,
|
|
133
292
|
fromDB?: (v: T) => T,
|
|
134
293
|
sort?: string,
|
|
135
294
|
q?: string,
|
|
@@ -151,7 +310,7 @@ export class Query<T, ID, S> extends SearchBuilder<T, S> {
|
|
|
151
310
|
this.exist = this.exist.bind(this)
|
|
152
311
|
}
|
|
153
312
|
metadata?(): Attributes | undefined {
|
|
154
|
-
return this.
|
|
313
|
+
return this.attrs
|
|
155
314
|
}
|
|
156
315
|
all(): Promise<T[]> {
|
|
157
316
|
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 {
|
|
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
|
-
|
|
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
|
-
|
|
30
|
-
|
|
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
|
-
|
|
33
|
-
|
|
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
|
|
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(
|
|
636
|
-
super(manager.query, table, attrs, manager.param, fromDB)
|
|
637
|
-
const x = version(
|
|
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) {
|