@samet-it/be-db-common 1.1.5 → 1.1.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.
- package/README.md +6 -0
- package/dist/connection/db.connection.d.ts +18 -26
- package/dist/connection/db.connection.js +22 -83
- package/dist/connection/index.types.d.ts +40 -180
- package/dist/repo/db.repo.d.ts +117 -41
- package/dist/repo/db.repo.js +392 -250
- package/dist/repo/index.types.d.ts +389 -99
- package/package.json +5 -4
package/dist/repo/db.repo.js
CHANGED
|
@@ -15,28 +15,31 @@ const be_base_common_1 = require("@samet-it/be-base-common");
|
|
|
15
15
|
const query_1 = require("@leyyo/query");
|
|
16
16
|
const type_1 = require("@leyyo/type");
|
|
17
17
|
const error_1 = require("../error");
|
|
18
|
+
const FORBIDDEN_KEYS = ['id', 'urn', '_trashId', 'createdAt', 'createdBy', 'updatedAt', 'updatedBy', "_rev", '_search', '_alpha', '_irregular'];
|
|
19
|
+
const STRICT_KEYS = ['id', 'urn', '_trashId', "_rev", '_irregular'];
|
|
18
20
|
// noinspection JSUnusedGlobalSymbols
|
|
19
21
|
/**
|
|
20
22
|
* DB repository abstract class
|
|
21
23
|
*
|
|
22
24
|
* Generics:
|
|
23
|
-
* - 0-`CONN`: db connection {@link
|
|
25
|
+
* - 0-`CONN`: db connection {@link DbConnectionLike}
|
|
24
26
|
* - 1-`OPT`: db execution options {@link DbExecOpt}
|
|
25
|
-
* - 2-`
|
|
26
|
-
* - 3-`
|
|
27
|
-
* - 4-`
|
|
28
|
-
* - 5-`
|
|
29
|
-
* - 6-`
|
|
30
|
-
* - 7-`
|
|
31
|
-
* - 8-`
|
|
32
|
-
* - 9-`
|
|
27
|
+
* - 2-`META`: query result metadata {@link DbMeta}
|
|
28
|
+
* - 3-`ENT`: entity {@link Entity}
|
|
29
|
+
* - 4-`ID`: id type {@link KeyValue}
|
|
30
|
+
* - 5-`URN`: urn interface {@link UrnDocLike}
|
|
31
|
+
* - 6-`VIEW`: view interface for presentation layer {@link View}
|
|
32
|
+
* - 7-`PAIR`: pair interface for presentation layer {@link Pair}
|
|
33
|
+
* - 8-`PORTION`: portion interface for presentation layer {@link Portion}
|
|
34
|
+
* - 9-`KEYS`: keys to autocomplete, def: keys of {@link Entity}
|
|
35
|
+
* - 10-`DIMS`: dimensions for presentation layer {@link DefDims}
|
|
33
36
|
* */
|
|
34
37
|
class DbRepo {
|
|
35
38
|
// endregion protected-property
|
|
36
39
|
/**
|
|
37
40
|
* Constructor
|
|
38
41
|
*
|
|
39
|
-
* @param {
|
|
42
|
+
* @param {DbConnectionLike} conn - db connection
|
|
40
43
|
* @param {DbRepoOpt} opt - options
|
|
41
44
|
* */
|
|
42
45
|
constructor(conn, opt) {
|
|
@@ -86,41 +89,22 @@ class DbRepo {
|
|
|
86
89
|
if (!this._opt.urnPrefix) {
|
|
87
90
|
throw new error_1.DbInvalidValueError('Urn prefix', { where: 'DbRepo', method: 'constructor', type: typeof this._opt.urnPrefix });
|
|
88
91
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
this._props = Object.assign({ conn }, this._opt);
|
|
92
|
-
delete this._opt;
|
|
93
|
-
}
|
|
94
|
-
// region protected-method
|
|
95
|
-
_removeFieldForForbidden(fields, flag, field) {
|
|
96
|
-
if (this._opt[flag] !== true) {
|
|
97
|
-
const index = fields.indexOf(field);
|
|
98
|
-
if (index >= 0) {
|
|
99
|
-
fields.splice(index, 0);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
_checkForbiddenList(given, def) {
|
|
104
|
-
if (!Array.isArray(given)) {
|
|
105
|
-
given = [];
|
|
92
|
+
if (!Array.isArray(this._opt.forbiddenSet)) {
|
|
93
|
+
this._opt.forbiddenSet = [...FORBIDDEN_KEYS];
|
|
106
94
|
}
|
|
107
95
|
else {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
96
|
+
STRICT_KEYS.forEach(k => {
|
|
97
|
+
if (!this._opt.forbiddenSet.includes(k)) {
|
|
98
|
+
this._opt.forbiddenSet.push(k);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
112
101
|
}
|
|
113
|
-
this.
|
|
114
|
-
this.
|
|
115
|
-
this._removeFieldForForbidden(given, 'useUpdatedAt', 'updatedAt');
|
|
116
|
-
this._removeFieldForForbidden(given, 'useUpdatedBy', 'updatedBy');
|
|
117
|
-
this._removeFieldForForbidden(given, 'useRevision', '_rev');
|
|
118
|
-
this._removeFieldForForbidden(given, 'useVersion', '_ver');
|
|
119
|
-
return given;
|
|
102
|
+
this._props = Object.assign({ conn }, this._opt);
|
|
103
|
+
delete this._opt;
|
|
120
104
|
}
|
|
105
|
+
// region protected-method
|
|
121
106
|
get _now() {
|
|
122
|
-
|
|
123
|
-
return useIsoDate ? new Date().toISOString() : Date.now();
|
|
107
|
+
return this.hasIsoDate ? new Date().toISOString() : Date.now();
|
|
124
108
|
}
|
|
125
109
|
get _randomIndexName() {
|
|
126
110
|
return 'idx_' + (0, node_crypto_1.randomUUID)().match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
|
|
@@ -151,27 +135,25 @@ class DbRepo {
|
|
|
151
135
|
return name;
|
|
152
136
|
}
|
|
153
137
|
/**
|
|
154
|
-
* Fetch urn from doc as doc.
|
|
138
|
+
* Fetch urn from doc as doc.urn
|
|
155
139
|
* */
|
|
156
140
|
_urnFromDoc(doc, method) {
|
|
157
|
-
const {
|
|
158
|
-
const param = { where: 'DbRepo', path, method };
|
|
141
|
+
const param = { where: 'DbRepo', path: this.path, method };
|
|
159
142
|
(0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
|
|
160
|
-
(0, type_1.assertText)(doc.
|
|
161
|
-
return doc.
|
|
143
|
+
(0, type_1.assertText)(doc.urn, Object.assign({ field: 'urn' }, param));
|
|
144
|
+
return doc.urn;
|
|
162
145
|
}
|
|
163
146
|
_checkKey(value) {
|
|
164
|
-
const { useNumericId } = this._props;
|
|
165
147
|
let id;
|
|
166
148
|
switch (typeof value) {
|
|
167
149
|
case "string":
|
|
168
150
|
const str = value.trim();
|
|
169
151
|
if (str) {
|
|
170
152
|
if (this._isUrn(str)) {
|
|
171
|
-
return [str, '
|
|
153
|
+
return [str, 'urn'];
|
|
172
154
|
}
|
|
173
155
|
else {
|
|
174
|
-
if (
|
|
156
|
+
if (this.hasNumericId) {
|
|
175
157
|
try {
|
|
176
158
|
id = parseInt(str);
|
|
177
159
|
}
|
|
@@ -189,7 +171,7 @@ class DbRepo {
|
|
|
189
171
|
}
|
|
190
172
|
break;
|
|
191
173
|
case "number":
|
|
192
|
-
if (
|
|
174
|
+
if (this.hasNumericId) {
|
|
193
175
|
return [value, 'id'];
|
|
194
176
|
}
|
|
195
177
|
else {
|
|
@@ -200,14 +182,11 @@ class DbRepo {
|
|
|
200
182
|
return [undefined, undefined];
|
|
201
183
|
}
|
|
202
184
|
const obj = value;
|
|
203
|
-
if (obj.id !== undefined) {
|
|
204
|
-
return this._checkKey(obj.id);
|
|
205
|
-
}
|
|
206
185
|
if (obj.urn !== undefined) {
|
|
207
186
|
return this._checkKey(obj.urn);
|
|
208
187
|
}
|
|
209
|
-
if (obj.
|
|
210
|
-
return this._checkKey(obj.
|
|
188
|
+
if (obj.id !== undefined) {
|
|
189
|
+
return this._checkKey(obj.id);
|
|
211
190
|
}
|
|
212
191
|
break;
|
|
213
192
|
}
|
|
@@ -224,7 +203,7 @@ class DbRepo {
|
|
|
224
203
|
for (const item of keys) {
|
|
225
204
|
const [key, field] = this._checkKey(item);
|
|
226
205
|
if (field) {
|
|
227
|
-
if ((field === '
|
|
206
|
+
if ((field === 'urn') && !result.urns.includes(key)) {
|
|
228
207
|
result.urns.push(key);
|
|
229
208
|
result.ordered.push([key, field]);
|
|
230
209
|
}
|
|
@@ -237,12 +216,10 @@ class DbRepo {
|
|
|
237
216
|
return result;
|
|
238
217
|
}
|
|
239
218
|
_isUrn(key) {
|
|
240
|
-
|
|
241
|
-
return (typeof key === 'string') && key.startsWith(urnPrefix + ':');
|
|
219
|
+
return (typeof key === 'string') && key.startsWith(this.urnPrefix + ':');
|
|
242
220
|
}
|
|
243
221
|
_keyToUrn(key) {
|
|
244
|
-
|
|
245
|
-
return `${urnPrefix}:${key}`;
|
|
222
|
+
return `${this.urnPrefix}:${key}`;
|
|
246
223
|
}
|
|
247
224
|
// endregion protected-method
|
|
248
225
|
// region getter
|
|
@@ -254,12 +231,214 @@ class DbRepo {
|
|
|
254
231
|
$cast() {
|
|
255
232
|
return this;
|
|
256
233
|
}
|
|
234
|
+
/** @inheritDoc */
|
|
235
|
+
get isEnabled() {
|
|
236
|
+
return this._props.conn.isEnabled;
|
|
237
|
+
}
|
|
238
|
+
/** @inheritDoc */
|
|
239
|
+
get userFetcher() {
|
|
240
|
+
return this._props.conn.props.userFetcher;
|
|
241
|
+
}
|
|
242
|
+
/** @inheritDoc */
|
|
243
|
+
get conn() {
|
|
244
|
+
return this._props.conn;
|
|
245
|
+
}
|
|
246
|
+
/** @inheritDoc */
|
|
247
|
+
get isConnected() {
|
|
248
|
+
return this._props.conn.isConnected;
|
|
249
|
+
}
|
|
250
|
+
/** @inheritDoc */
|
|
251
|
+
get cache() {
|
|
252
|
+
return this._props.cache;
|
|
253
|
+
}
|
|
254
|
+
/** @inheritDoc */
|
|
255
|
+
get modelVersion() {
|
|
256
|
+
return this._props.version;
|
|
257
|
+
}
|
|
258
|
+
/** @inheritDoc */
|
|
259
|
+
get urnDelimiter() {
|
|
260
|
+
return this._props.urnDelimiter;
|
|
261
|
+
}
|
|
262
|
+
/** @inheritDoc */
|
|
263
|
+
get urnPrefix() {
|
|
264
|
+
return this._props.urnPrefix;
|
|
265
|
+
}
|
|
266
|
+
/** @inheritDoc */
|
|
267
|
+
get hasNumericId() {
|
|
268
|
+
return this._props.useNumericId;
|
|
269
|
+
}
|
|
270
|
+
/** @inheritDoc */
|
|
271
|
+
get hasIsoDate() {
|
|
272
|
+
return this._props.useIsoDate;
|
|
273
|
+
}
|
|
274
|
+
/** @inheritDoc */
|
|
275
|
+
get isIdSame() {
|
|
276
|
+
return this._props.idSame;
|
|
277
|
+
}
|
|
278
|
+
/** @inheritDoc */
|
|
279
|
+
get hasSoftDelete() {
|
|
280
|
+
return this._props.useSoftDelete;
|
|
281
|
+
}
|
|
282
|
+
/** @inheritDoc */
|
|
283
|
+
get hasCreatedAt() {
|
|
284
|
+
return this._props.useCreatedAt;
|
|
285
|
+
}
|
|
286
|
+
/** @inheritDoc */
|
|
287
|
+
get hasCreatedBy() {
|
|
288
|
+
return this._props.useCreatedBy;
|
|
289
|
+
}
|
|
290
|
+
/** @inheritDoc */
|
|
291
|
+
get hasUpdatedAt() {
|
|
292
|
+
return this._props.useUpdatedAt;
|
|
293
|
+
}
|
|
294
|
+
/** @inheritDoc */
|
|
295
|
+
get hasUpdatedBy() {
|
|
296
|
+
return this._props.useUpdatedBy;
|
|
297
|
+
}
|
|
298
|
+
/** @inheritDoc */
|
|
299
|
+
get hasVersion() {
|
|
300
|
+
return this._props.useVersion;
|
|
301
|
+
}
|
|
302
|
+
/** @inheritDoc */
|
|
303
|
+
get hasRevision() {
|
|
304
|
+
return this._props.useRevision;
|
|
305
|
+
}
|
|
306
|
+
/** @inheritDoc */
|
|
307
|
+
get isNoInsert() {
|
|
308
|
+
return this._props.noInsert;
|
|
309
|
+
}
|
|
310
|
+
/** @inheritDoc */
|
|
311
|
+
get isNoUpdate() {
|
|
312
|
+
return this._props.noUpdate;
|
|
313
|
+
}
|
|
314
|
+
/** @inheritDoc */
|
|
315
|
+
get isNoTrash() {
|
|
316
|
+
return this._props.noTrash;
|
|
317
|
+
}
|
|
318
|
+
/** @inheritDoc */
|
|
319
|
+
get isNoRemove() {
|
|
320
|
+
return this._props.noRemove;
|
|
321
|
+
}
|
|
322
|
+
/** @inheritDoc */
|
|
323
|
+
get alphaFn() {
|
|
324
|
+
return this._props.alphaFn;
|
|
325
|
+
}
|
|
326
|
+
/** @inheritDoc */
|
|
327
|
+
get searchFn() {
|
|
328
|
+
return this._props.searchFn;
|
|
329
|
+
}
|
|
330
|
+
/** @inheritDoc */
|
|
331
|
+
get irregularFn() {
|
|
332
|
+
return this._props.irregularFn;
|
|
333
|
+
}
|
|
334
|
+
/** @inheritDoc */
|
|
335
|
+
get forbiddenSet() {
|
|
336
|
+
return [...this._props.forbiddenSet];
|
|
337
|
+
}
|
|
338
|
+
/** @inheritDoc */
|
|
339
|
+
get path() {
|
|
340
|
+
return this._props.path;
|
|
341
|
+
}
|
|
342
|
+
/** @inheritDoc */
|
|
343
|
+
get table() {
|
|
344
|
+
return this._props.table;
|
|
345
|
+
}
|
|
346
|
+
/** @inheritDoc */
|
|
347
|
+
get urnLength() {
|
|
348
|
+
var _a, _b;
|
|
349
|
+
if (!this._props.urnLength) {
|
|
350
|
+
this._props.urnLength = (_b = (_a = this._props.urnPrefix) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
|
|
351
|
+
}
|
|
352
|
+
return this._props.urnLength;
|
|
353
|
+
}
|
|
257
354
|
// endregion getter
|
|
355
|
+
// region field-value
|
|
356
|
+
/** {@inheritDoc} */
|
|
357
|
+
checkError(err, opt) {
|
|
358
|
+
var _a, _b, _c;
|
|
359
|
+
const size = be_base_common_1.errorHandler.addStat(err);
|
|
360
|
+
const ignoredErrors = (_a = opt === null || opt === void 0 ? void 0 : opt.ignoredErrors) !== null && _a !== void 0 ? _a : [];
|
|
361
|
+
if (ignoredErrors.includes(err) || ignoredErrors.includes(err.name)) {
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (opt === null || opt === void 0 ? void 0 : opt.silent) {
|
|
365
|
+
if (size < 100) {
|
|
366
|
+
this.logger.warn(be_base_common_1.errorHandler.common.logText(err, ((_b = opt.name) !== null && _b !== void 0 ? _b : 'Database error'), size));
|
|
367
|
+
}
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (size < 100) {
|
|
371
|
+
this.logger.error(be_base_common_1.errorHandler.common.logText(err, ((_c = opt.name) !== null && _c !== void 0 ? _c : 'Database error'), size));
|
|
372
|
+
}
|
|
373
|
+
if (err instanceof error_1.DbError) {
|
|
374
|
+
throw err;
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
throw be_base_common_1.errorHandler.common.castForClass(error_1.DbError, err, { queryName: opt.name });
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
/** {@inheritDoc} */
|
|
381
|
+
f(field) {
|
|
382
|
+
return this.field(field);
|
|
383
|
+
}
|
|
384
|
+
v(value) {
|
|
385
|
+
return this.value(value);
|
|
386
|
+
}
|
|
387
|
+
/** {@inheritDoc} */
|
|
388
|
+
rows(rows) {
|
|
389
|
+
if (rows && Array.isArray(rows) && (rows.length > 0)) {
|
|
390
|
+
return rows.filter(row => row['_trashId'] === undefined);
|
|
391
|
+
}
|
|
392
|
+
return [];
|
|
393
|
+
}
|
|
394
|
+
/** {@inheritDoc} */
|
|
395
|
+
row(row) {
|
|
396
|
+
if (row && row['_trashId'] === undefined) {
|
|
397
|
+
return row;
|
|
398
|
+
}
|
|
399
|
+
return undefined;
|
|
400
|
+
}
|
|
401
|
+
/** {@inheritDoc} */
|
|
402
|
+
first(rows) {
|
|
403
|
+
if (rows && Array.isArray(rows) && (rows.length > 0)) {
|
|
404
|
+
return rows[0];
|
|
405
|
+
}
|
|
406
|
+
return undefined;
|
|
407
|
+
}
|
|
408
|
+
// endregion field-value
|
|
409
|
+
// region option
|
|
410
|
+
/** {@inheritDoc} */
|
|
411
|
+
buildOpt(p1, name) {
|
|
412
|
+
const opt = (typeof p1 === 'string' ? { name: p1 } : Object.assign({}, p1));
|
|
413
|
+
if (typeof opt.name === undefined && typeof name !== undefined) {
|
|
414
|
+
opt.name = name;
|
|
415
|
+
}
|
|
416
|
+
opt.name = opt.name ? `[${opt.name}]` : '';
|
|
417
|
+
return opt;
|
|
418
|
+
}
|
|
419
|
+
// endregion option
|
|
420
|
+
// region query
|
|
421
|
+
/** {@inheritDoc} */
|
|
422
|
+
exec(fn, p1) {
|
|
423
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
424
|
+
if (!this.isEnabled) {
|
|
425
|
+
return undefined;
|
|
426
|
+
}
|
|
427
|
+
const opt = this.buildOpt(p1);
|
|
428
|
+
try {
|
|
429
|
+
return yield fn;
|
|
430
|
+
}
|
|
431
|
+
catch (err) {
|
|
432
|
+
this.checkError(err, opt);
|
|
433
|
+
return undefined;
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
// endregion query
|
|
258
438
|
// region urn
|
|
259
439
|
/** @inheritDoc */
|
|
260
440
|
toUrn(urnRec) {
|
|
261
|
-
|
|
262
|
-
return `${urnPrefix}:${this.$toUrnTuple(urnRec).join(urnDelimiter)}`;
|
|
441
|
+
return `${this.urnPrefix}:${this.$toUrnTuple(urnRec).join(this.urnDelimiter)}`;
|
|
263
442
|
}
|
|
264
443
|
/** @inheritDoc */
|
|
265
444
|
$toUrnTuple(urnRec) {
|
|
@@ -268,33 +447,31 @@ class DbRepo {
|
|
|
268
447
|
// endregion urn
|
|
269
448
|
// region get
|
|
270
449
|
/** @inheritDoc */
|
|
271
|
-
get(
|
|
450
|
+
get(keyLike, p1) {
|
|
272
451
|
return __awaiter(this, void 0, void 0, function* () {
|
|
273
|
-
const
|
|
274
|
-
const
|
|
275
|
-
const [key, field] = this._checkKey(k1);
|
|
452
|
+
const opt = this.buildOpt(p1, 'get');
|
|
453
|
+
const [key, field] = this._checkKey(keyLike);
|
|
276
454
|
switch (field) {
|
|
277
|
-
case "
|
|
455
|
+
case "urn":
|
|
278
456
|
return this.getByPrimary(key, opt, true);
|
|
279
457
|
case "id":
|
|
280
458
|
return this.getBySecondary(key, opt, true);
|
|
281
459
|
default:
|
|
282
|
-
(0, type_1.assertMessage)('Invalid key', { field: 'key', value:
|
|
460
|
+
(0, type_1.assertMessage)('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'get', queryName: opt.name });
|
|
283
461
|
}
|
|
284
462
|
});
|
|
285
463
|
}
|
|
286
464
|
/** @inheritDoc */
|
|
287
465
|
getByPrimary(key, p1, ignoreCheck) {
|
|
288
466
|
return __awaiter(this, void 0, void 0, function* () {
|
|
289
|
-
const
|
|
290
|
-
const opt = conn.buildOpt(p1, 'getByPrimary');
|
|
467
|
+
const opt = this.buildOpt(p1, 'getByPrimary');
|
|
291
468
|
if (!ignoreCheck) {
|
|
292
469
|
(0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'getByPrimary', queryName: opt.name });
|
|
293
470
|
}
|
|
294
471
|
let found;
|
|
295
|
-
if (!opt.noCache && cache.
|
|
472
|
+
if (!opt.noCache && this.cache.isConnected) {
|
|
296
473
|
try {
|
|
297
|
-
found = yield cache.getByPrimary(key, opt);
|
|
474
|
+
found = yield this.cache.getByPrimary(key, opt);
|
|
298
475
|
}
|
|
299
476
|
catch (e) {
|
|
300
477
|
this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'getByPrimary', 'cache', 'getByPrimary'));
|
|
@@ -304,8 +481,8 @@ class DbRepo {
|
|
|
304
481
|
}
|
|
305
482
|
}
|
|
306
483
|
found = yield this.$getByPrimary(key, opt, true);
|
|
307
|
-
if (!opt.noCache && found && cache.
|
|
308
|
-
cache.ingestDoc(found, opt)
|
|
484
|
+
if (!opt.noCache && found && this.cache.isConnected) {
|
|
485
|
+
this.cache.ingestDoc(found, opt)
|
|
309
486
|
.then()
|
|
310
487
|
.catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'getByPrimary', 'cache', 'ingestDoc')));
|
|
311
488
|
}
|
|
@@ -315,23 +492,22 @@ class DbRepo {
|
|
|
315
492
|
/** @inheritDoc */
|
|
316
493
|
getBySecondary(key, p1, ignoreCheck) {
|
|
317
494
|
return __awaiter(this, void 0, void 0, function* () {
|
|
318
|
-
const
|
|
319
|
-
const opt = conn.buildOpt(p1, 'getBySecondary');
|
|
495
|
+
const opt = this.buildOpt(p1, 'getBySecondary');
|
|
320
496
|
if (!ignoreCheck) {
|
|
321
|
-
if (
|
|
497
|
+
if (this.hasNumericId) {
|
|
322
498
|
(0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary', queryName: opt.name });
|
|
323
499
|
}
|
|
324
500
|
else {
|
|
325
501
|
(0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary', queryName: opt.name });
|
|
326
502
|
}
|
|
327
503
|
}
|
|
328
|
-
if (
|
|
504
|
+
if (this.isIdSame) {
|
|
329
505
|
return this.getByPrimary(this._keyToUrn(key), opt, true);
|
|
330
506
|
}
|
|
331
507
|
let found;
|
|
332
|
-
if (!opt.noCache && cache.
|
|
508
|
+
if (!opt.noCache && this.cache.isConnected) {
|
|
333
509
|
try {
|
|
334
|
-
found = yield cache.getBySecondary(key, opt);
|
|
510
|
+
found = yield this.cache.getBySecondary(key, opt);
|
|
335
511
|
if (found) {
|
|
336
512
|
return found;
|
|
337
513
|
}
|
|
@@ -341,8 +517,8 @@ class DbRepo {
|
|
|
341
517
|
}
|
|
342
518
|
}
|
|
343
519
|
found = yield this.$getBySecondary(key, opt, true);
|
|
344
|
-
if (found && cache.
|
|
345
|
-
cache.ingestDoc(found, opt)
|
|
520
|
+
if (found && this.cache.isConnected) {
|
|
521
|
+
this.cache.ingestDoc(found, opt)
|
|
346
522
|
.then()
|
|
347
523
|
.catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'getBySecondary', 'cache', 'ingestDoc')));
|
|
348
524
|
}
|
|
@@ -352,18 +528,17 @@ class DbRepo {
|
|
|
352
528
|
// endregion get
|
|
353
529
|
// region exists
|
|
354
530
|
/** @inheritDoc */
|
|
355
|
-
exists(
|
|
531
|
+
exists(keyLike, p1) {
|
|
356
532
|
return __awaiter(this, void 0, void 0, function* () {
|
|
357
|
-
const
|
|
358
|
-
const
|
|
359
|
-
const [key, field] = this._checkKey(k1);
|
|
533
|
+
const opt = this.buildOpt(p1, 'exists');
|
|
534
|
+
const [key, field] = this._checkKey(keyLike);
|
|
360
535
|
switch (field) {
|
|
361
|
-
case "
|
|
536
|
+
case "urn":
|
|
362
537
|
return this.existsByPrimary(key, opt, true);
|
|
363
538
|
case "id":
|
|
364
539
|
return this.existsBySecondary(key, opt, true);
|
|
365
540
|
default:
|
|
366
|
-
(0, type_1.assertMessage)('Invalid key', { field: 'key', value:
|
|
541
|
+
(0, type_1.assertMessage)('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'exists', queryName: opt.name });
|
|
367
542
|
break;
|
|
368
543
|
}
|
|
369
544
|
});
|
|
@@ -371,14 +546,13 @@ class DbRepo {
|
|
|
371
546
|
/** @inheritDoc */
|
|
372
547
|
existsByPrimary(key, p1, ignoreCheck) {
|
|
373
548
|
return __awaiter(this, void 0, void 0, function* () {
|
|
374
|
-
const
|
|
375
|
-
const opt = conn.buildOpt(p1, 'existsByPrimary');
|
|
549
|
+
const opt = this.buildOpt(p1, 'existsByPrimary');
|
|
376
550
|
if (!ignoreCheck) {
|
|
377
551
|
(0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'existsByPrimary', queryName: opt.name });
|
|
378
552
|
}
|
|
379
|
-
if (!opt.noCache && cache.
|
|
553
|
+
if (!opt.noCache && this.cache.isConnected) {
|
|
380
554
|
try {
|
|
381
|
-
const found = yield cache.getByPrimary(key, opt);
|
|
555
|
+
const found = yield this.cache.getByPrimary(key, opt);
|
|
382
556
|
if (found) {
|
|
383
557
|
return true;
|
|
384
558
|
}
|
|
@@ -393,22 +567,21 @@ class DbRepo {
|
|
|
393
567
|
/** @inheritDoc */
|
|
394
568
|
existsBySecondary(key, p1, ignoreCheck) {
|
|
395
569
|
return __awaiter(this, void 0, void 0, function* () {
|
|
396
|
-
const
|
|
397
|
-
const opt = conn.buildOpt(p1, 'existsBySecondary');
|
|
570
|
+
const opt = this.buildOpt(p1, 'existsBySecondary');
|
|
398
571
|
if (!ignoreCheck) {
|
|
399
|
-
if (
|
|
572
|
+
if (this.hasNumericId) {
|
|
400
573
|
(0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary', queryName: opt.name });
|
|
401
574
|
}
|
|
402
575
|
else {
|
|
403
576
|
(0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary', queryName: opt.name });
|
|
404
577
|
}
|
|
405
578
|
}
|
|
406
|
-
if (!
|
|
579
|
+
if (!this.isIdSame) {
|
|
407
580
|
return this.existsByPrimary(this._keyToUrn(key));
|
|
408
581
|
}
|
|
409
|
-
if (!opt.noCache && cache.
|
|
582
|
+
if (!opt.noCache && this.cache.isConnected) {
|
|
410
583
|
try {
|
|
411
|
-
const found = yield cache.getBySecondary(key, opt);
|
|
584
|
+
const found = yield this.cache.getBySecondary(key, opt);
|
|
412
585
|
if (found) {
|
|
413
586
|
return true;
|
|
414
587
|
}
|
|
@@ -423,29 +596,28 @@ class DbRepo {
|
|
|
423
596
|
// endregion exists
|
|
424
597
|
// region list
|
|
425
598
|
/** @inheritDoc */
|
|
426
|
-
list(
|
|
599
|
+
list(keyLikes, p1, ignoreCheck) {
|
|
427
600
|
return __awaiter(this, void 0, void 0, function* () {
|
|
428
|
-
const
|
|
429
|
-
const opt = conn.buildOpt(p1, 'list');
|
|
601
|
+
const opt = this.buildOpt(p1, 'list');
|
|
430
602
|
if (!ignoreCheck) {
|
|
431
|
-
(0, type_1.assertArray)(
|
|
603
|
+
(0, type_1.assertArray)(keyLikes, { field: 'keys', where: 'DbRepo', method: 'list', queryName: opt.name });
|
|
432
604
|
}
|
|
433
|
-
const result = this._checkKeys(
|
|
605
|
+
const result = this._checkKeys(keyLikes);
|
|
434
606
|
if (result.ordered.length < 1) {
|
|
435
607
|
return [];
|
|
436
608
|
}
|
|
437
609
|
const docs = [];
|
|
438
610
|
let founds = [];
|
|
439
611
|
if (result.urns.length > 0) {
|
|
440
|
-
if (!opt.noCache && cache.
|
|
612
|
+
if (!opt.noCache && this.cache.isConnected) {
|
|
441
613
|
try {
|
|
442
|
-
founds = yield cache.getByPrimaryMore(result.urns, opt);
|
|
614
|
+
founds = yield this.cache.getByPrimaryMore(result.urns, opt);
|
|
443
615
|
}
|
|
444
616
|
catch (e) {
|
|
445
617
|
this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'list', 'cache', 'getByPrimary'));
|
|
446
618
|
}
|
|
447
619
|
founds.forEach(val => {
|
|
448
|
-
const index = result.urns.indexOf(val.
|
|
620
|
+
const index = result.urns.indexOf(val.urn);
|
|
449
621
|
if (index >= 0) {
|
|
450
622
|
result.urns.splice(index, 1);
|
|
451
623
|
}
|
|
@@ -455,8 +627,8 @@ class DbRepo {
|
|
|
455
627
|
if (result.urns.length > 0) {
|
|
456
628
|
founds = yield this.$listByPrimary(result.urns, opt, true);
|
|
457
629
|
if (founds.length > 0) {
|
|
458
|
-
if (!opt.noCache && cache.
|
|
459
|
-
founds.forEach(found => cache.ingestDoc(found, opt)
|
|
630
|
+
if (!opt.noCache && this.cache.isConnected) {
|
|
631
|
+
founds.forEach(found => this.cache.ingestDoc(found, opt)
|
|
460
632
|
.then()
|
|
461
633
|
.catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'list', 'cache', 'ingestDoc'))));
|
|
462
634
|
}
|
|
@@ -465,9 +637,9 @@ class DbRepo {
|
|
|
465
637
|
}
|
|
466
638
|
}
|
|
467
639
|
if (result.ids.length > 0) {
|
|
468
|
-
if (!opt.noCache && cache.
|
|
640
|
+
if (!opt.noCache && this.cache.isConnected) {
|
|
469
641
|
try {
|
|
470
|
-
founds = yield cache.getBySecondaryMore(result.ids, opt);
|
|
642
|
+
founds = yield this.cache.getBySecondaryMore(result.ids, opt);
|
|
471
643
|
}
|
|
472
644
|
catch (e) {
|
|
473
645
|
this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'list', 'cache', 'getBySecondary'));
|
|
@@ -483,8 +655,8 @@ class DbRepo {
|
|
|
483
655
|
if (result.ids.length > 0) {
|
|
484
656
|
founds = yield this.$listBySecondary(result.ids, opt, true);
|
|
485
657
|
if (founds.length > 0) {
|
|
486
|
-
if (!opt.noCache && cache.
|
|
487
|
-
founds.forEach(found => cache.ingestDoc(found, opt)
|
|
658
|
+
if (!opt.noCache && this.cache.isConnected) {
|
|
659
|
+
founds.forEach(found => this.cache.ingestDoc(found, opt)
|
|
488
660
|
.then()
|
|
489
661
|
.catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'list', 'cache', 'ingestDoc'))));
|
|
490
662
|
}
|
|
@@ -500,8 +672,7 @@ class DbRepo {
|
|
|
500
672
|
/** @inheritDoc */
|
|
501
673
|
filter(query, p1) {
|
|
502
674
|
return __awaiter(this, void 0, void 0, function* () {
|
|
503
|
-
const
|
|
504
|
-
const opt = conn.buildOpt(p1, 'filter');
|
|
675
|
+
const opt = this.buildOpt(p1, 'filter');
|
|
505
676
|
return this.$filter(query_1.queryParser.exec(query, [], opt.name), opt);
|
|
506
677
|
});
|
|
507
678
|
}
|
|
@@ -510,36 +681,42 @@ class DbRepo {
|
|
|
510
681
|
/** @inheritDoc */
|
|
511
682
|
insert(doc, p1, ignoreCheck) {
|
|
512
683
|
return __awaiter(this, void 0, void 0, function* () {
|
|
513
|
-
const
|
|
514
|
-
const opt = conn.buildOpt(p1, 'insert');
|
|
684
|
+
const opt = this.buildOpt(p1, 'insert');
|
|
515
685
|
const param = { where: 'DbRepo', method: 'insert', queryName: opt.name };
|
|
516
|
-
if (
|
|
686
|
+
if (this.isNoInsert) {
|
|
517
687
|
throw new error_1.DbNotSupportedError('Insert', param);
|
|
518
688
|
}
|
|
519
689
|
if (!ignoreCheck) {
|
|
520
690
|
(0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
|
|
521
|
-
if (
|
|
691
|
+
if (this.hasNumericId) {
|
|
522
692
|
(0, type_1.assertInteger)(doc.id, Object.assign({ field: 'doc.id' }, param));
|
|
523
693
|
}
|
|
524
694
|
else {
|
|
525
695
|
(0, type_1.assertText)(doc.id, Object.assign({ field: 'doc.id' }, param));
|
|
526
696
|
}
|
|
527
|
-
(0, type_1.assertText)(doc.
|
|
697
|
+
(0, type_1.assertText)(doc.urn, Object.assign({ field: 'doc.urn' }, param));
|
|
528
698
|
}
|
|
529
699
|
if (!doc._trashId !== undefined) {
|
|
530
700
|
delete doc._trashId;
|
|
531
701
|
}
|
|
532
|
-
if (
|
|
702
|
+
if (this.hasCreatedAt) {
|
|
533
703
|
doc.createdAt = this._now;
|
|
534
704
|
}
|
|
535
|
-
|
|
536
|
-
|
|
705
|
+
let userId;
|
|
706
|
+
if (this.hasCreatedBy && this.userFetcher) {
|
|
707
|
+
doc.createdBy = (yield this.userFetcher());
|
|
708
|
+
userId = doc.createdBy;
|
|
537
709
|
}
|
|
538
|
-
if (
|
|
710
|
+
if (this.hasUpdatedAt) {
|
|
539
711
|
doc.updatedAt = this._now;
|
|
540
712
|
}
|
|
541
|
-
if (
|
|
542
|
-
|
|
713
|
+
if (this.hasUpdatedBy && this.userFetcher) {
|
|
714
|
+
if (userId) {
|
|
715
|
+
doc.createdBy = userId;
|
|
716
|
+
}
|
|
717
|
+
else {
|
|
718
|
+
doc.createdBy = (yield this.userFetcher());
|
|
719
|
+
}
|
|
543
720
|
}
|
|
544
721
|
return this.$insert(doc, opt);
|
|
545
722
|
});
|
|
@@ -547,10 +724,9 @@ class DbRepo {
|
|
|
547
724
|
/** @inheritDoc */
|
|
548
725
|
inserts(docs, p1, ignoreCheck) {
|
|
549
726
|
return __awaiter(this, void 0, void 0, function* () {
|
|
550
|
-
const
|
|
551
|
-
const opt = conn.buildOpt(p1, 'inserts');
|
|
727
|
+
const opt = this.buildOpt(p1, 'inserts');
|
|
552
728
|
const param = { where: 'DbRepo', method: 'inserts', queryName: opt.name };
|
|
553
|
-
if (
|
|
729
|
+
if (this.isNoInsert) {
|
|
554
730
|
throw new error_1.DbNotSupportedError('Insert', param);
|
|
555
731
|
}
|
|
556
732
|
if (!ignoreCheck) {
|
|
@@ -568,12 +744,11 @@ class DbRepo {
|
|
|
568
744
|
/** @inheritDoc */
|
|
569
745
|
replace(doc, p1, ignoreCheck) {
|
|
570
746
|
return __awaiter(this, void 0, void 0, function* () {
|
|
571
|
-
const
|
|
572
|
-
const opt = conn.buildOpt(p1, 'replace');
|
|
747
|
+
const opt = this.buildOpt(p1, 'replace');
|
|
573
748
|
if (!ignoreCheck) {
|
|
574
749
|
(0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'replace', queryName: opt.name });
|
|
575
|
-
(0, type_1.assertText)(doc.
|
|
576
|
-
if (
|
|
750
|
+
(0, type_1.assertText)(doc.urn, { field: 'doc.urn', where: 'DbRepo', method: 'replace', queryName: opt.name });
|
|
751
|
+
if (this.hasNumericId) {
|
|
577
752
|
(0, type_1.assertInteger)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'replace', queryName: opt.name });
|
|
578
753
|
}
|
|
579
754
|
else {
|
|
@@ -583,15 +758,15 @@ class DbRepo {
|
|
|
583
758
|
if (doc._trashId !== undefined) {
|
|
584
759
|
delete doc._trashId;
|
|
585
760
|
}
|
|
586
|
-
if (
|
|
761
|
+
if (this.hasUpdatedAt) {
|
|
587
762
|
doc.updatedAt = this._now;
|
|
588
763
|
}
|
|
589
|
-
if (
|
|
590
|
-
|
|
764
|
+
if (this.hasUpdatedBy && this.userFetcher) {
|
|
765
|
+
doc.createdBy = (yield this.userFetcher());
|
|
591
766
|
}
|
|
592
767
|
const replacedKey = yield this.$replace(doc, opt, true);
|
|
593
|
-
if (!opt.noCache && cache.
|
|
594
|
-
cache.clearDoc(replacedKey, opt)
|
|
768
|
+
if (!opt.noCache && this.cache.isConnected) {
|
|
769
|
+
this.cache.clearDoc(replacedKey, opt)
|
|
595
770
|
.then()
|
|
596
771
|
.catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'replace', 'cache', 'clearDoc')));
|
|
597
772
|
}
|
|
@@ -603,16 +778,15 @@ class DbRepo {
|
|
|
603
778
|
/** @inheritDoc */
|
|
604
779
|
upsert(doc, p1, ignoreCheck) {
|
|
605
780
|
return __awaiter(this, void 0, void 0, function* () {
|
|
606
|
-
const
|
|
607
|
-
const opt = conn.buildOpt(p1, 'upsert');
|
|
781
|
+
const opt = this.buildOpt(p1, 'upsert');
|
|
608
782
|
const param = { where: 'DbRepo', method: 'upsert', queryName: opt.name };
|
|
609
|
-
if (
|
|
783
|
+
if (this.isNoInsert || this.isNoUpdate) {
|
|
610
784
|
throw new error_1.DbNotSupportedError('Upsert', param);
|
|
611
785
|
}
|
|
612
786
|
if (!ignoreCheck) {
|
|
613
787
|
(0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
|
|
614
|
-
(0, type_1.assertText)(doc.
|
|
615
|
-
if (
|
|
788
|
+
(0, type_1.assertText)(doc.urn, Object.assign({ field: 'doc.urn' }, param));
|
|
789
|
+
if (this.hasNumericId) {
|
|
616
790
|
(0, type_1.assertInteger)(doc.id, Object.assign({ field: 'doc.id' }, param));
|
|
617
791
|
}
|
|
618
792
|
else {
|
|
@@ -622,15 +796,15 @@ class DbRepo {
|
|
|
622
796
|
if (doc._trashId !== undefined) {
|
|
623
797
|
delete doc._trashId;
|
|
624
798
|
}
|
|
625
|
-
if (
|
|
799
|
+
if (this.hasUpdatedAt) {
|
|
626
800
|
doc.updatedAt = this._now;
|
|
627
801
|
}
|
|
628
|
-
if (
|
|
629
|
-
|
|
802
|
+
if (this.hasUpdatedBy && this.userFetcher) {
|
|
803
|
+
doc.createdBy = (yield this.userFetcher());
|
|
630
804
|
}
|
|
631
805
|
const createdKey = yield this.$replace(doc, opt, true);
|
|
632
|
-
if (!opt.noCache && cache.
|
|
633
|
-
cache.clearDoc(createdKey, opt)
|
|
806
|
+
if (!opt.noCache && this.cache.isConnected) {
|
|
807
|
+
this.cache.clearDoc(createdKey, opt)
|
|
634
808
|
.then()
|
|
635
809
|
.catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'upsert', 'cache', 'clearDoc')));
|
|
636
810
|
}
|
|
@@ -642,14 +816,13 @@ class DbRepo {
|
|
|
642
816
|
/** @inheritDoc */
|
|
643
817
|
update(k1, doc, p1, ignoreCheck) {
|
|
644
818
|
return __awaiter(this, void 0, void 0, function* () {
|
|
645
|
-
const
|
|
646
|
-
const opt = conn.buildOpt(p1, 'update');
|
|
819
|
+
const opt = this.buildOpt(p1, 'update');
|
|
647
820
|
if (!ignoreCheck) {
|
|
648
821
|
(0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'update', queryName: opt.name });
|
|
649
822
|
}
|
|
650
823
|
const [key, field] = this._checkKey(k1);
|
|
651
824
|
switch (field) {
|
|
652
|
-
case "
|
|
825
|
+
case "urn":
|
|
653
826
|
return this.updateByPrimary(key, doc, opt, true);
|
|
654
827
|
case "id":
|
|
655
828
|
return this.updateBySecondary(key, doc, opt, true);
|
|
@@ -661,10 +834,9 @@ class DbRepo {
|
|
|
661
834
|
/** @inheritDoc */
|
|
662
835
|
updateByPrimary(key, doc, p1, ignoreCheck) {
|
|
663
836
|
return __awaiter(this, void 0, void 0, function* () {
|
|
664
|
-
const
|
|
665
|
-
const opt = conn.buildOpt(p1, 'updateByPrimary');
|
|
837
|
+
const opt = this.buildOpt(p1, 'updateByPrimary');
|
|
666
838
|
const param = { where: 'DbRepo', method: 'updateByPrimary', queryName: opt.name };
|
|
667
|
-
if (
|
|
839
|
+
if (this.isNoUpdate) {
|
|
668
840
|
throw new error_1.DbNotSupportedError('Update', param);
|
|
669
841
|
}
|
|
670
842
|
if (!ignoreCheck) {
|
|
@@ -674,20 +846,20 @@ class DbRepo {
|
|
|
674
846
|
if (doc._trashId !== undefined) {
|
|
675
847
|
delete doc._trashId;
|
|
676
848
|
}
|
|
677
|
-
forbiddenSet.forEach(f => {
|
|
849
|
+
this._props.forbiddenSet.forEach(f => {
|
|
678
850
|
if (doc[f] !== undefined) {
|
|
679
851
|
delete doc[f];
|
|
680
852
|
}
|
|
681
853
|
});
|
|
682
|
-
if (
|
|
854
|
+
if (this.hasUpdatedAt) {
|
|
683
855
|
doc.updatedAt = this._now;
|
|
684
856
|
}
|
|
685
|
-
if (
|
|
686
|
-
|
|
857
|
+
if (this.hasUpdatedBy && this.userFetcher) {
|
|
858
|
+
doc.createdBy = (yield this.userFetcher());
|
|
687
859
|
}
|
|
688
860
|
const updatedKey = yield this.$updateByPrimary(key, doc, opt, true);
|
|
689
|
-
if (updatedKey && !opt.noCache && cache.
|
|
690
|
-
cache.clearDoc(updatedKey, opt)
|
|
861
|
+
if (updatedKey && !opt.noCache && this.cache.isConnected) {
|
|
862
|
+
this.cache.clearDoc(updatedKey, opt)
|
|
691
863
|
.then()
|
|
692
864
|
.catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'updateByPrimary', 'cache', 'clearDoc')));
|
|
693
865
|
}
|
|
@@ -697,14 +869,13 @@ class DbRepo {
|
|
|
697
869
|
/** @inheritDoc */
|
|
698
870
|
updateBySecondary(key, doc, p1, ignoreCheck) {
|
|
699
871
|
return __awaiter(this, void 0, void 0, function* () {
|
|
700
|
-
const
|
|
701
|
-
const opt = conn.buildOpt(p1, 'updateBySecondary');
|
|
872
|
+
const opt = this.buildOpt(p1, 'updateBySecondary');
|
|
702
873
|
const param = { where: 'DbRepo', method: 'updateBySecondary', queryName: opt.name };
|
|
703
|
-
if (
|
|
874
|
+
if (this.isNoUpdate) {
|
|
704
875
|
throw new error_1.DbNotSupportedError('Update', param);
|
|
705
876
|
}
|
|
706
877
|
if (!ignoreCheck) {
|
|
707
|
-
if (
|
|
878
|
+
if (this.hasNumericId) {
|
|
708
879
|
(0, type_1.assertInteger)(key, Object.assign({ field: 'key' }, param));
|
|
709
880
|
}
|
|
710
881
|
else {
|
|
@@ -712,26 +883,26 @@ class DbRepo {
|
|
|
712
883
|
}
|
|
713
884
|
(0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
|
|
714
885
|
}
|
|
715
|
-
if (
|
|
886
|
+
if (this.isIdSame) {
|
|
716
887
|
return this.updateBySecondary(this._keyToUrn(key), doc, opt, true);
|
|
717
888
|
}
|
|
718
889
|
if (doc._trashId !== undefined) {
|
|
719
890
|
delete doc._trashId;
|
|
720
891
|
}
|
|
721
|
-
forbiddenSet.forEach(f => {
|
|
892
|
+
this._props.forbiddenSet.forEach(f => {
|
|
722
893
|
if (doc[f] !== undefined) {
|
|
723
894
|
delete doc[f];
|
|
724
895
|
}
|
|
725
896
|
});
|
|
726
|
-
if (
|
|
897
|
+
if (this.hasUpdatedAt) {
|
|
727
898
|
doc.updatedAt = this._now;
|
|
728
899
|
}
|
|
729
|
-
if (
|
|
730
|
-
|
|
900
|
+
if (this.hasUpdatedBy && this.userFetcher) {
|
|
901
|
+
doc.createdBy = (yield this.userFetcher());
|
|
731
902
|
}
|
|
732
903
|
const updatedKey = yield this.$updateBySecondary(key, doc, opt, true);
|
|
733
|
-
if (updatedKey && !opt.noCache && cache.
|
|
734
|
-
cache.clearDoc(updatedKey, opt)
|
|
904
|
+
if (updatedKey && !opt.noCache && this.cache.isConnected) {
|
|
905
|
+
this.cache.clearDoc(updatedKey, opt)
|
|
735
906
|
.then()
|
|
736
907
|
.catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'updateBySecondary', 'cache', 'clearDoc')));
|
|
737
908
|
}
|
|
@@ -743,8 +914,7 @@ class DbRepo {
|
|
|
743
914
|
/** @inheritDoc */
|
|
744
915
|
set(key, doc, p1, ignoreCheck) {
|
|
745
916
|
return __awaiter(this, void 0, void 0, function* () {
|
|
746
|
-
const
|
|
747
|
-
const opt = conn.buildOpt(p1, 'set');
|
|
917
|
+
const opt = this.buildOpt(p1, 'set');
|
|
748
918
|
if (!ignoreCheck) {
|
|
749
919
|
(0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'set', queryName: opt.name });
|
|
750
920
|
}
|
|
@@ -759,18 +929,16 @@ class DbRepo {
|
|
|
759
929
|
/** @inheritDoc */
|
|
760
930
|
unset(key, fields, p1, ignoreCheck) {
|
|
761
931
|
return __awaiter(this, void 0, void 0, function* () {
|
|
762
|
-
const
|
|
763
|
-
const opt = conn.buildOpt(p1, 'unset');
|
|
932
|
+
const opt = this.buildOpt(p1, 'unset');
|
|
764
933
|
if (!ignoreCheck) {
|
|
765
934
|
(0, type_1.assertArray)(fields, { field: 'fields', where: 'DbRepo', method: 'unset', queryName: opt.name });
|
|
766
935
|
fields.forEach((field, index) => {
|
|
767
936
|
(0, type_1.assertText)(field, { field: field, where: 'DbRepo', method: 'unset', index, queryName: opt.name });
|
|
768
937
|
});
|
|
769
938
|
}
|
|
770
|
-
const { forbiddenUnset } = this._props;
|
|
771
939
|
const doc = {};
|
|
772
940
|
fields.forEach(f => {
|
|
773
|
-
if (!
|
|
941
|
+
if (!this._props.forbiddenSet.includes(f)) {
|
|
774
942
|
doc[f] = undefined;
|
|
775
943
|
}
|
|
776
944
|
});
|
|
@@ -780,36 +948,34 @@ class DbRepo {
|
|
|
780
948
|
// endregion set
|
|
781
949
|
// region remove
|
|
782
950
|
/** @inheritDoc */
|
|
783
|
-
remove(
|
|
951
|
+
remove(keyLike, p1) {
|
|
784
952
|
return __awaiter(this, void 0, void 0, function* () {
|
|
785
|
-
const
|
|
786
|
-
const
|
|
787
|
-
const [key, field] = this._checkKey(k1);
|
|
953
|
+
const opt = this.buildOpt(p1, 'remove');
|
|
954
|
+
const [key, field] = this._checkKey(keyLike);
|
|
788
955
|
switch (field) {
|
|
789
|
-
case "
|
|
956
|
+
case "urn":
|
|
790
957
|
return this.removeByPrimary(key, opt, true);
|
|
791
958
|
case "id":
|
|
792
959
|
return this.removeBySecondary(key, opt, true);
|
|
793
960
|
default:
|
|
794
|
-
(0, type_1.assertMessage)('Invalid key', { field: 'key', value:
|
|
961
|
+
(0, type_1.assertMessage)('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'remove', queryName: opt.name });
|
|
795
962
|
}
|
|
796
963
|
});
|
|
797
964
|
}
|
|
798
965
|
/** @inheritDoc */
|
|
799
966
|
removeByPrimary(key, p1, ignoreCheck) {
|
|
800
967
|
return __awaiter(this, void 0, void 0, function* () {
|
|
801
|
-
const
|
|
802
|
-
const opt = conn.buildOpt(p1, 'removeByPrimary');
|
|
968
|
+
const opt = this.buildOpt(p1, 'removeByPrimary');
|
|
803
969
|
const param = { where: 'DbRepo', method: 'removeByPrimary', queryName: opt.name };
|
|
804
|
-
if (
|
|
970
|
+
if (this.isNoRemove) {
|
|
805
971
|
throw new error_1.DbNotSupportedError('Remove', param);
|
|
806
972
|
}
|
|
807
973
|
if (!ignoreCheck) {
|
|
808
974
|
(0, type_1.assertText)(key, Object.assign({ field: 'key' }, param));
|
|
809
975
|
}
|
|
810
976
|
const removedKey = yield this.$removeByPrimary(key, opt, true);
|
|
811
|
-
if (removedKey && !opt.noCache && cache.
|
|
812
|
-
cache.clearDoc(removedKey, opt)
|
|
977
|
+
if (removedKey && !opt.noCache && this.cache.isConnected) {
|
|
978
|
+
this.cache.clearDoc(removedKey, opt)
|
|
813
979
|
.then()
|
|
814
980
|
.catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'removeByPrimary', 'cache', 'clearDoc')));
|
|
815
981
|
}
|
|
@@ -819,24 +985,23 @@ class DbRepo {
|
|
|
819
985
|
/** @inheritDoc */
|
|
820
986
|
removeBySecondary(key, p1, ignoreCheck) {
|
|
821
987
|
return __awaiter(this, void 0, void 0, function* () {
|
|
822
|
-
const
|
|
823
|
-
const opt = conn.buildOpt(p1, 'removeBySecondary');
|
|
988
|
+
const opt = this.buildOpt(p1, 'removeBySecondary');
|
|
824
989
|
const param = { where: 'DbRepo', method: 'removeByPrimary', queryName: opt.name };
|
|
825
|
-
if (
|
|
990
|
+
if (this.isNoRemove) {
|
|
826
991
|
throw new error_1.DbNotSupportedError('Remove', param);
|
|
827
992
|
}
|
|
828
|
-
if (
|
|
993
|
+
if (this.hasNumericId) {
|
|
829
994
|
(0, type_1.assertInteger)(key, { field: 'key', param });
|
|
830
995
|
}
|
|
831
996
|
else {
|
|
832
997
|
(0, type_1.assertText)(key, { field: 'key', param });
|
|
833
998
|
}
|
|
834
|
-
if (
|
|
999
|
+
if (this.isIdSame) {
|
|
835
1000
|
return this.removeByPrimary(this._keyToUrn(key), opt, ignoreCheck);
|
|
836
1001
|
}
|
|
837
1002
|
const removedKey = yield this.$removeBySecondary(key, opt, true);
|
|
838
|
-
if (removedKey && !opt.noCache && cache.
|
|
839
|
-
cache.clearDoc(removedKey, opt)
|
|
1003
|
+
if (removedKey && !opt.noCache && this.cache.isConnected) {
|
|
1004
|
+
this.cache.clearDoc(removedKey, opt)
|
|
840
1005
|
.then()
|
|
841
1006
|
.catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'removeBySecondary', 'cache', 'clearDoc')));
|
|
842
1007
|
}
|
|
@@ -846,31 +1011,29 @@ class DbRepo {
|
|
|
846
1011
|
// endregion remove
|
|
847
1012
|
// region trash
|
|
848
1013
|
/** @inheritDoc */
|
|
849
|
-
trash(
|
|
1014
|
+
trash(keyLike, p1) {
|
|
850
1015
|
return __awaiter(this, void 0, void 0, function* () {
|
|
851
|
-
const
|
|
852
|
-
const
|
|
853
|
-
const [key, field] = this._checkKey(k1);
|
|
1016
|
+
const opt = this.buildOpt(p1, 'trash');
|
|
1017
|
+
const [key, field] = this._checkKey(keyLike);
|
|
854
1018
|
switch (field) {
|
|
855
|
-
case "
|
|
1019
|
+
case "urn":
|
|
856
1020
|
return this.trashByPrimary(key, opt, true);
|
|
857
1021
|
case "id":
|
|
858
1022
|
return this.trashBySecondary(key, opt, true);
|
|
859
1023
|
default:
|
|
860
|
-
(0, type_1.assertMessage)('Invalid key', { field: 'key', value:
|
|
1024
|
+
(0, type_1.assertMessage)('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'trash', queryName: opt.name });
|
|
861
1025
|
}
|
|
862
1026
|
});
|
|
863
1027
|
}
|
|
864
1028
|
/** @inheritDoc */
|
|
865
1029
|
trashByPrimary(key, p1, ignoreCheck) {
|
|
866
1030
|
return __awaiter(this, void 0, void 0, function* () {
|
|
867
|
-
const
|
|
868
|
-
const opt = conn.buildOpt(p1, 'trashByPrimary');
|
|
1031
|
+
const opt = this.buildOpt(p1, 'trashByPrimary');
|
|
869
1032
|
const param = { where: 'DbRepo', method: 'trashByPrimary', queryName: opt.name };
|
|
870
|
-
if (!
|
|
1033
|
+
if (!this.hasSoftDelete) {
|
|
871
1034
|
throw new error_1.DbNotSupportedError('Soft Delete', param);
|
|
872
1035
|
}
|
|
873
|
-
if (
|
|
1036
|
+
if (this.isNoTrash) {
|
|
874
1037
|
throw new error_1.DbNotSupportedError('Trash', param);
|
|
875
1038
|
}
|
|
876
1039
|
if (!ignoreCheck) {
|
|
@@ -880,8 +1043,8 @@ class DbRepo {
|
|
|
880
1043
|
opt.trashId = (0, node_crypto_1.randomUUID)();
|
|
881
1044
|
}
|
|
882
1045
|
const trashedKey = yield this.$trashByPrimary(key, opt, true);
|
|
883
|
-
if (trashedKey && !opt.noCache && cache.
|
|
884
|
-
cache.clearDoc(trashedKey, opt)
|
|
1046
|
+
if (trashedKey && !opt.noCache && this.cache.isConnected) {
|
|
1047
|
+
this.cache.clearDoc(trashedKey, opt)
|
|
885
1048
|
.then()
|
|
886
1049
|
.catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'trashByPrimary', 'cache', 'clearDoc')));
|
|
887
1050
|
}
|
|
@@ -891,30 +1054,29 @@ class DbRepo {
|
|
|
891
1054
|
/** @inheritDoc */
|
|
892
1055
|
trashBySecondary(key, p1, ignoreCheck) {
|
|
893
1056
|
return __awaiter(this, void 0, void 0, function* () {
|
|
894
|
-
const
|
|
895
|
-
const opt = conn.buildOpt(p1, 'trashBySecondary');
|
|
1057
|
+
const opt = this.buildOpt(p1, 'trashBySecondary');
|
|
896
1058
|
const param = { where: 'DbRepo', method: 'trashBySecondary', queryName: opt.name };
|
|
897
|
-
if (!
|
|
1059
|
+
if (!this.hasSoftDelete) {
|
|
898
1060
|
throw new error_1.DbNotSupportedError('Soft Delete', param);
|
|
899
1061
|
}
|
|
900
|
-
if (
|
|
1062
|
+
if (this.isNoTrash) {
|
|
901
1063
|
throw new error_1.DbNotSupportedError('Trash', param);
|
|
902
1064
|
}
|
|
903
|
-
if (
|
|
1065
|
+
if (this.hasNumericId) {
|
|
904
1066
|
(0, type_1.assertInteger)(key, Object.assign({ field: 'key' }, param));
|
|
905
1067
|
}
|
|
906
1068
|
else {
|
|
907
1069
|
(0, type_1.assertText)(key, Object.assign({ field: 'key' }, param));
|
|
908
1070
|
}
|
|
909
|
-
if (
|
|
1071
|
+
if (this.isIdSame) {
|
|
910
1072
|
return this.trashByPrimary(this._keyToUrn(key), opt, ignoreCheck);
|
|
911
1073
|
}
|
|
912
1074
|
if (!(0, type_1.isText)(opt.trashId)) {
|
|
913
1075
|
opt.trashId = (0, node_crypto_1.randomUUID)();
|
|
914
1076
|
}
|
|
915
1077
|
const trashedKey = yield this.$trashBySecondary(key, opt, true);
|
|
916
|
-
if (trashedKey && !opt.noCache && cache.
|
|
917
|
-
cache.clearDoc(trashedKey, opt)
|
|
1078
|
+
if (trashedKey && !opt.noCache && this.cache.isConnected) {
|
|
1079
|
+
this.cache.clearDoc(trashedKey, opt)
|
|
918
1080
|
.then()
|
|
919
1081
|
.catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'trashBySecondary', 'cache', 'clearDoc')));
|
|
920
1082
|
}
|
|
@@ -930,14 +1092,17 @@ class DbRepo {
|
|
|
930
1092
|
return undefined;
|
|
931
1093
|
}
|
|
932
1094
|
switch (dim) {
|
|
933
|
-
case "portion":
|
|
934
|
-
return yield this.$toPortion(doc);
|
|
935
1095
|
case "view":
|
|
936
|
-
return
|
|
1096
|
+
return doc;
|
|
937
1097
|
case "pair":
|
|
938
|
-
return
|
|
939
|
-
default:
|
|
940
|
-
|
|
1098
|
+
return { id: doc.id, name: doc.name };
|
|
1099
|
+
default: // portion
|
|
1100
|
+
if (this.isIdSame) {
|
|
1101
|
+
return { id: doc.id };
|
|
1102
|
+
}
|
|
1103
|
+
else {
|
|
1104
|
+
return { id: doc.id, urn: doc.urn };
|
|
1105
|
+
}
|
|
941
1106
|
}
|
|
942
1107
|
});
|
|
943
1108
|
}
|
|
@@ -965,15 +1130,6 @@ class DbRepo {
|
|
|
965
1130
|
// endregion dim
|
|
966
1131
|
// region pair
|
|
967
1132
|
/** @inheritDoc */
|
|
968
|
-
$toPair(doc) {
|
|
969
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
970
|
-
if (!doc) {
|
|
971
|
-
return undefined;
|
|
972
|
-
}
|
|
973
|
-
return { id: doc === null || doc === void 0 ? void 0 : doc.id, name: doc === null || doc === void 0 ? void 0 : doc.name };
|
|
974
|
-
});
|
|
975
|
-
}
|
|
976
|
-
/** @inheritDoc */
|
|
977
1133
|
getPair(key) {
|
|
978
1134
|
return __awaiter(this, void 0, void 0, function* () {
|
|
979
1135
|
return this.getDim(key, 'pair');
|
|
@@ -987,44 +1143,30 @@ class DbRepo {
|
|
|
987
1143
|
}
|
|
988
1144
|
// endregion pair
|
|
989
1145
|
// region view
|
|
990
|
-
$toView(doc) {
|
|
991
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
992
|
-
return doc;
|
|
993
|
-
});
|
|
994
|
-
}
|
|
995
1146
|
/** @inheritDoc */
|
|
996
|
-
getView(
|
|
1147
|
+
getView(key) {
|
|
997
1148
|
return __awaiter(this, void 0, void 0, function* () {
|
|
998
|
-
return this.getDim(
|
|
1149
|
+
return this.getDim(key, 'view');
|
|
999
1150
|
});
|
|
1000
1151
|
}
|
|
1001
1152
|
/** @inheritDoc */
|
|
1002
|
-
listViews(
|
|
1153
|
+
listViews(keys, ignoreCheck) {
|
|
1003
1154
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1004
|
-
return this.listDims(
|
|
1155
|
+
return this.listDims(keys, 'view', ignoreCheck);
|
|
1005
1156
|
});
|
|
1006
1157
|
}
|
|
1007
1158
|
// endregion view
|
|
1008
1159
|
// region portion
|
|
1009
1160
|
/** @inheritDoc */
|
|
1010
|
-
|
|
1011
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1012
|
-
if (!doc) {
|
|
1013
|
-
return undefined;
|
|
1014
|
-
}
|
|
1015
|
-
return { id: doc === null || doc === void 0 ? void 0 : doc.id };
|
|
1016
|
-
});
|
|
1017
|
-
}
|
|
1018
|
-
/** @inheritDoc */
|
|
1019
|
-
getPortion(identifier) {
|
|
1161
|
+
getPortion(key) {
|
|
1020
1162
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1021
|
-
return this.getDim(
|
|
1163
|
+
return this.getDim(key, 'portion');
|
|
1022
1164
|
});
|
|
1023
1165
|
}
|
|
1024
1166
|
/** @inheritDoc */
|
|
1025
|
-
listPortions(
|
|
1167
|
+
listPortions(keys, ignoreCheck) {
|
|
1026
1168
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1027
|
-
return this.listDims(
|
|
1169
|
+
return this.listDims(keys, 'portion', ignoreCheck);
|
|
1028
1170
|
});
|
|
1029
1171
|
}
|
|
1030
1172
|
}
|