@samet-it/be-db-common 1.1.2 → 1.1.4

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.
@@ -11,22 +11,41 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.DbRepo = void 0;
13
13
  const node_crypto_1 = require("node:crypto");
14
+ const be_base_common_1 = require("@samet-it/be-base-common");
14
15
  const query_1 = require("@leyyo/query");
15
16
  const type_1 = require("@leyyo/type");
16
17
  const error_1 = require("../error");
17
18
  // noinspection JSUnusedGlobalSymbols
18
19
  /**
19
20
  * DB repository abstract class
21
+ *
22
+ * Generics:
23
+ * - 0-`CONN`: db connection {@link DbConnectionBase}
24
+ * - 1-`OPT`: db execution options {@link DbExecOpt}
25
+ * - 2-`ENT`: entity {@link Entity}
26
+ * - 3-`ID`: id type {@link KeyValue}
27
+ * - 4-`URN`: urn interface {@link UrnDocLike}
28
+ * - 5-`VIEW`: view interface for presentation layer {@link View}
29
+ * - 6-`PAIR`: pair interface for presentation layer {@link Pair}
30
+ * - 7-`PORTION`: portion interface for presentation layer {@link Portion}
31
+ * - 8-`KEYS`: keys to autocomplete, def: keys of {@link Entity}
32
+ * - 9-`DIMS`: dimensions for presentation layer {@link DefDims}
20
33
  * */
21
34
  class DbRepo {
22
35
  // endregion protected-property
36
+ /**
37
+ * Constructor
38
+ *
39
+ * @param {DbConnectionBase} conn - db connection
40
+ * @param {DbRepoOpt} opt - options
41
+ * */
23
42
  constructor(conn, opt) {
24
43
  /**
25
44
  * Option of in-body usage in place of constructor param
26
45
  * */
27
46
  this._opt = {};
28
47
  if (!(0, type_1.isObjectFilled)(conn)) {
29
- throw new error_1.DbInvalidValueError('Connection', typeof conn, 'DbRepo', 'constructor');
48
+ throw new error_1.DbInvalidValueError('Connection', { where: 'DbRepo', method: 'constructor', type: typeof conn });
30
49
  }
31
50
  if (!(0, type_1.isObjectBare)(this._opt)) {
32
51
  this._opt = {};
@@ -40,7 +59,7 @@ class DbRepo {
40
59
  }
41
60
  if (this._opt.useVersion) {
42
61
  if (typeof this._opt.version !== 'number') {
43
- throw new error_1.DbInvalidValueError('Model version', typeof conn, 'DbRepo', 'constructor');
62
+ throw new error_1.DbInvalidValueError('Model version', { where: 'DbRepo', method: 'constructor', type: typeof this._opt.version });
44
63
  }
45
64
  }
46
65
  else {
@@ -65,7 +84,7 @@ class DbRepo {
65
84
  }
66
85
  }
67
86
  if (!this._opt.urnPrefix) {
68
- throw new error_1.DbInvalidValueError('Urn prefix', typeof conn, 'DbRepo', 'constructor');
87
+ throw new error_1.DbInvalidValueError('Urn prefix', { where: 'DbRepo', method: 'constructor', type: typeof this._opt.urnPrefix });
69
88
  }
70
89
  this._opt.forbiddenSet = this._checkForbiddenList(this._opt.forbiddenSet, ['id', '_urn', '_trashId', 'createdAt', 'createdBy']);
71
90
  this._opt.forbiddenUnset = this._checkForbiddenList(this._opt.forbiddenUnset, ['id', '_urn', '_trashId', 'createdAt', 'createdBy', 'updatedAt', 'updatedBy']);
@@ -91,7 +110,6 @@ class DbRepo {
91
110
  if (given.length < 1) {
92
111
  given.push(...def);
93
112
  }
94
- this._removeFieldForForbidden(given, 'useSoftDelete', '_trashId');
95
113
  this._removeFieldForForbidden(given, 'useCreatedAt', 'createdAt');
96
114
  this._removeFieldForForbidden(given, 'useCreatedBy', 'createdBy');
97
115
  this._removeFieldForForbidden(given, 'useUpdatedAt', 'updatedAt');
@@ -137,14 +155,10 @@ class DbRepo {
137
155
  * */
138
156
  _urnFromDoc(doc, method) {
139
157
  const { path } = this._props;
140
- (0, type_1.assertObject)(doc, () => {
141
- return { field: 'doc', where: 'DbRepo', path, method };
142
- });
143
- const urn = doc._urn;
144
- (0, type_1.assertObject)(urn, () => {
145
- return { field: 'urn', where: 'DbRepo', path, method };
146
- });
147
- return urn;
158
+ const param = { where: 'DbRepo', path, method };
159
+ (0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
160
+ (0, type_1.assertText)(doc._urn, Object.assign({ field: 'urn' }, param));
161
+ return doc._urn;
148
162
  }
149
163
  _checkKey(value) {
150
164
  const { useNumericId } = this._props;
@@ -226,6 +240,10 @@ class DbRepo {
226
240
  const { urnPrefix } = this._props;
227
241
  return (typeof key === 'string') && key.startsWith(urnPrefix + ':');
228
242
  }
243
+ _keyToUrn(key) {
244
+ const { urnPrefix } = this._props;
245
+ return `${urnPrefix}:${key}`;
246
+ }
229
247
  // endregion protected-method
230
248
  // region getter
231
249
  /** @inheritDoc */
@@ -238,10 +256,6 @@ class DbRepo {
238
256
  }
239
257
  // endregion getter
240
258
  // region urn
241
- _keyToUrn(key) {
242
- const { urnPrefix } = this._props;
243
- return `${urnPrefix}:${key}`;
244
- }
245
259
  /** @inheritDoc */
246
260
  toUrn(urnRec) {
247
261
  const { urnPrefix, urnDelimiter } = this._props;
@@ -256,41 +270,44 @@ class DbRepo {
256
270
  /** @inheritDoc */
257
271
  get(k1, p1) {
258
272
  return __awaiter(this, void 0, void 0, function* () {
273
+ const { conn } = this._props;
274
+ const opt = conn.buildOpt(p1, 'get');
259
275
  const [key, field] = this._checkKey(k1);
260
276
  switch (field) {
261
277
  case "_urn":
262
- return this.getByPrimary(key, p1, true);
278
+ return this.getByPrimary(key, opt, true);
263
279
  case "id":
264
- return this.getBySecondary(key, p1, true);
280
+ return this.getBySecondary(key, opt, true);
265
281
  default:
266
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'get' });
282
+ (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'get', queryName: opt.name });
267
283
  }
268
284
  });
269
285
  }
270
286
  /** @inheritDoc */
271
287
  getByPrimary(key, p1, ignoreCheck) {
272
288
  return __awaiter(this, void 0, void 0, function* () {
273
- const { cache } = this._props;
289
+ const { cache, conn } = this._props;
290
+ const opt = conn.buildOpt(p1, 'getByPrimary');
274
291
  if (!ignoreCheck) {
275
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'getByPrimary' });
292
+ (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'getByPrimary', queryName: opt.name });
276
293
  }
277
294
  let found;
278
- if (cache.props.isConnected) {
295
+ if (!opt.noCache && cache.props.isConnected) {
279
296
  try {
280
297
  found = (yield cache.getByPrimary(key))[0];
281
298
  }
282
299
  catch (e) {
283
- this.logger.warn(`getByPrimary.cache.getByPrimary <${e.name}> ${e.message}`);
300
+ this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'getByPrimary', 'cache', 'getByPrimary'));
284
301
  }
285
302
  if (found) {
286
303
  return found;
287
304
  }
288
305
  }
289
- found = yield this.$getByPrimary(key, p1, true);
290
- if (found && cache.props.isConnected) {
306
+ found = yield this.$getByPrimary(key, opt, true);
307
+ if (!opt.noCache && found && cache.props.isConnected) {
291
308
  cache.ingestDoc(found)
292
309
  .then()
293
- .catch(e => this.logger.warn(`getByPrimary.cache.ingestDoc <${e.name}> ${e.message}`));
310
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'getByPrimary', 'cache', 'ingestDoc')));
294
311
  }
295
312
  return found;
296
313
  });
@@ -298,20 +315,21 @@ class DbRepo {
298
315
  /** @inheritDoc */
299
316
  getBySecondary(key, p1, ignoreCheck) {
300
317
  return __awaiter(this, void 0, void 0, function* () {
301
- const { cache, useNumericId, idSame } = this._props;
318
+ const { cache, useNumericId, idSame, conn } = this._props;
319
+ const opt = conn.buildOpt(p1, 'getBySecondary');
302
320
  if (!ignoreCheck) {
303
321
  if (useNumericId) {
304
- (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary' });
322
+ (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary', queryName: opt.name });
305
323
  }
306
324
  else {
307
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary' });
325
+ (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary', queryName: opt.name });
308
326
  }
309
327
  }
310
328
  if (idSame) {
311
- return this.getByPrimary(this._keyToUrn(key), p1, true);
329
+ return this.getByPrimary(this._keyToUrn(key), opt, true);
312
330
  }
313
331
  let found;
314
- if (cache.props.isConnected) {
332
+ if (!opt.noCache && cache.props.isConnected) {
315
333
  try {
316
334
  found = (yield cache.getBySecondary(key))[0];
317
335
  if (found) {
@@ -319,14 +337,14 @@ class DbRepo {
319
337
  }
320
338
  }
321
339
  catch (e) {
322
- this.logger.warn(`getBySecondary.cache.getBySecondary <${e.name}> ${e.message}`);
340
+ this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'getBySecondary', 'cache', 'getBySecondary'));
323
341
  }
324
342
  }
325
- found = yield this.$getBySecondary(key, p1, true);
343
+ found = yield this.$getBySecondary(key, opt, true);
326
344
  if (found && cache.props.isConnected) {
327
345
  cache.ingestDoc(found)
328
346
  .then()
329
- .catch(e => this.logger.warn(`getBySecondary.cache.ingestDoc <${e.name}> ${e.message}`));
347
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'getBySecondary', 'cache', 'ingestDoc')));
330
348
  }
331
349
  return found;
332
350
  });
@@ -336,14 +354,16 @@ class DbRepo {
336
354
  /** @inheritDoc */
337
355
  exists(k1, p1) {
338
356
  return __awaiter(this, void 0, void 0, function* () {
357
+ const { conn } = this._props;
358
+ const opt = conn.buildOpt(p1, 'exists');
339
359
  const [key, field] = this._checkKey(k1);
340
360
  switch (field) {
341
361
  case "_urn":
342
- return this.existsByPrimary(key, p1, true);
362
+ return this.existsByPrimary(key, opt, true);
343
363
  case "id":
344
- return this.existsBySecondary(key, p1, true);
364
+ return this.existsBySecondary(key, opt, true);
345
365
  default:
346
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'exists' });
366
+ (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'exists', queryName: opt.name });
347
367
  break;
348
368
  }
349
369
  });
@@ -351,11 +371,12 @@ class DbRepo {
351
371
  /** @inheritDoc */
352
372
  existsByPrimary(key, p1, ignoreCheck) {
353
373
  return __awaiter(this, void 0, void 0, function* () {
354
- const { cache } = this._props;
374
+ const { cache, conn } = this._props;
375
+ const opt = conn.buildOpt(p1, 'existsByPrimary');
355
376
  if (!ignoreCheck) {
356
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'existsByPrimary' });
377
+ (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'existsByPrimary', queryName: opt.name });
357
378
  }
358
- if (cache.props.isConnected) {
379
+ if (!opt.noCache && cache.props.isConnected) {
359
380
  try {
360
381
  const found = (yield cache.getByPrimary(key))[0];
361
382
  if (found) {
@@ -363,28 +384,29 @@ class DbRepo {
363
384
  }
364
385
  }
365
386
  catch (e) {
366
- this.logger.warn(`existsByPrimary.cache.existsByPrimary <${e.name}> ${e.message}`);
387
+ this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'existsByPrimary', 'cache', 'getByPrimary'));
367
388
  }
368
389
  }
369
- return this.$existsByPrimary(key, p1, true);
390
+ return this.$existsByPrimary(key, opt, true);
370
391
  });
371
392
  }
372
393
  /** @inheritDoc */
373
394
  existsBySecondary(key, p1, ignoreCheck) {
374
395
  return __awaiter(this, void 0, void 0, function* () {
375
- const { cache, useNumericId, idSame } = this._props;
396
+ const { cache, useNumericId, idSame, conn } = this._props;
397
+ const opt = conn.buildOpt(p1, 'existsBySecondary');
376
398
  if (!ignoreCheck) {
377
399
  if (useNumericId) {
378
- (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary' });
400
+ (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary', queryName: opt.name });
379
401
  }
380
402
  else {
381
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary' });
403
+ (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary', queryName: opt.name });
382
404
  }
383
405
  }
384
406
  if (!idSame) {
385
407
  return this.existsByPrimary(this._keyToUrn(key));
386
408
  }
387
- if (cache.props.isConnected) {
409
+ if (!opt.noCache && cache.props.isConnected) {
388
410
  try {
389
411
  const found = (yield cache.getBySecondary(key))[0];
390
412
  if (found) {
@@ -392,10 +414,10 @@ class DbRepo {
392
414
  }
393
415
  }
394
416
  catch (e) {
395
- this.logger.warn(`existsBySecondary.cache.existsBySecondary <${e.name}> ${e.message}`);
417
+ this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'existsBySecondary', 'cache', 'getBySecondary'));
396
418
  }
397
419
  }
398
- return this.$existsBySecondary(key, p1, true);
420
+ return this.$existsBySecondary(key, opt, true);
399
421
  });
400
422
  }
401
423
  // endregion exists
@@ -403,11 +425,11 @@ class DbRepo {
403
425
  /** @inheritDoc */
404
426
  list(keys, p1, ignoreCheck) {
405
427
  return __awaiter(this, void 0, void 0, function* () {
406
- if (!ignoreCheck) {
407
- (0, type_1.assertArray)(keys, { field: 'keys', where: 'DbRepo', method: 'list' });
408
- }
409
428
  const { conn, cache } = this._props;
410
429
  const opt = conn.buildOpt(p1, 'list');
430
+ if (!ignoreCheck) {
431
+ (0, type_1.assertArray)(keys, { field: 'keys', where: 'DbRepo', method: 'list', queryName: opt.name });
432
+ }
411
433
  const result = this._checkKeys(keys);
412
434
  if (result.ordered.length < 1) {
413
435
  return [];
@@ -415,12 +437,12 @@ class DbRepo {
415
437
  const docs = [];
416
438
  let founds = [];
417
439
  if (result.urns.length > 0) {
418
- if (cache.props.isConnected) {
440
+ if (!opt.noCache && cache.props.isConnected) {
419
441
  try {
420
442
  founds = yield cache.getByPrimary(...result.urns);
421
443
  }
422
444
  catch (e) {
423
- this.logger.warn(`list.cache.getByPrimary <${e.name}> ${e.message}`);
445
+ this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'list', 'cache', 'getByPrimary'));
424
446
  }
425
447
  founds.forEach(val => {
426
448
  const index = result.urns.indexOf(val._urn);
@@ -433,22 +455,22 @@ class DbRepo {
433
455
  if (result.urns.length > 0) {
434
456
  founds = yield this.$listByPrimary(result.urns, opt, true);
435
457
  if (founds.length > 0) {
436
- if (cache.props.isConnected) {
458
+ if (!opt.noCache && cache.props.isConnected) {
437
459
  founds.forEach(found => cache.ingestDoc(found)
438
460
  .then()
439
- .catch(e => this.logger.warn(`list.cache.ingestDoc <${e.name}> ${e.message}`)));
461
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'list', 'cache', 'ingestDoc'))));
440
462
  }
441
463
  docs.push(...founds);
442
464
  }
443
465
  }
444
466
  }
445
467
  if (result.ids.length > 0) {
446
- if (cache.props.isConnected) {
468
+ if (!opt.noCache && cache.props.isConnected) {
447
469
  try {
448
470
  founds = yield cache.getBySecondary(...result.ids);
449
471
  }
450
472
  catch (e) {
451
- this.logger.warn(`list.cache.getBySecondary <${e.name}> ${e.message}`);
473
+ this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'list', 'cache', 'getBySecondary'));
452
474
  }
453
475
  founds.forEach(val => {
454
476
  const index = result.ids.indexOf(val.id);
@@ -461,10 +483,10 @@ class DbRepo {
461
483
  if (result.ids.length > 0) {
462
484
  founds = yield this.$listBySecondary(result.ids, opt, true);
463
485
  if (founds.length > 0) {
464
- if (cache.props.isConnected) {
486
+ if (!opt.noCache && cache.props.isConnected) {
465
487
  founds.forEach(found => cache.ingestDoc(found)
466
488
  .then()
467
- .catch(e => this.logger.warn(`list.cache.ingestDoc <${e.name}> ${e.message}`)));
489
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'list', 'cache', 'ingestDoc'))));
468
490
  }
469
491
  docs.push(...founds);
470
492
  }
@@ -488,16 +510,21 @@ class DbRepo {
488
510
  /** @inheritDoc */
489
511
  insert(doc, p1, ignoreCheck) {
490
512
  return __awaiter(this, void 0, void 0, function* () {
491
- const { useCreatedAt, useNumericId, useCreatedBy, useUpdatedAt, useUpdatedBy } = this._props;
513
+ const { useCreatedAt, conn, noInsert, useNumericId, useCreatedBy, useUpdatedAt, useUpdatedBy } = this._props;
514
+ const opt = conn.buildOpt(p1, 'insert');
515
+ const param = { where: 'DbRepo', method: 'insert', queryName: opt.name };
516
+ if (noInsert) {
517
+ throw new error_1.DbNotSupportedError('Insert', param);
518
+ }
492
519
  if (!ignoreCheck) {
493
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'insert' });
520
+ (0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
494
521
  if (useNumericId) {
495
- (0, type_1.assertInteger)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'insert' });
522
+ (0, type_1.assertInteger)(doc.id, Object.assign({ field: 'doc.id' }, param));
496
523
  }
497
524
  else {
498
- (0, type_1.assertText)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'insert' });
525
+ (0, type_1.assertText)(doc.id, Object.assign({ field: 'doc.id' }, param));
499
526
  }
500
- (0, type_1.assertText)(doc._urn, { field: 'doc._urn', where: 'DbRepo', method: 'insert' });
527
+ (0, type_1.assertText)(doc._urn, Object.assign({ field: 'doc._urn' }, param));
501
528
  }
502
529
  if (!doc._trashId !== undefined) {
503
530
  delete doc._trashId;
@@ -514,18 +541,24 @@ class DbRepo {
514
541
  if (useUpdatedBy) {
515
542
  // todo
516
543
  }
517
- return this.$insert(doc, p1);
544
+ return this.$insert(doc, opt);
518
545
  });
519
546
  }
520
547
  /** @inheritDoc */
521
548
  inserts(docs, p1, ignoreCheck) {
522
549
  return __awaiter(this, void 0, void 0, function* () {
550
+ const { noInsert, conn } = this._props;
551
+ const opt = conn.buildOpt(p1, 'inserts');
552
+ const param = { where: 'DbRepo', method: 'inserts', queryName: opt.name };
553
+ if (noInsert) {
554
+ throw new error_1.DbNotSupportedError('Insert', param);
555
+ }
523
556
  if (!ignoreCheck) {
524
- (0, type_1.assertArray)(docs, { field: 'docs', where: 'DbRepo', method: 'inserts' });
557
+ (0, type_1.assertArray)(docs, Object.assign({ field: 'docs' }, param));
525
558
  }
526
559
  const insertedIds = [];
527
560
  for (const doc of docs) {
528
- insertedIds.push(yield this.insert(doc, p1, true));
561
+ insertedIds.push(yield this.insert(doc, opt, true));
529
562
  }
530
563
  return insertedIds;
531
564
  });
@@ -535,15 +568,16 @@ class DbRepo {
535
568
  /** @inheritDoc */
536
569
  replace(doc, p1, ignoreCheck) {
537
570
  return __awaiter(this, void 0, void 0, function* () {
538
- const { cache, useNumericId, useUpdatedAt, useUpdatedBy } = this._props;
571
+ const { cache, conn, useNumericId, useUpdatedAt, useUpdatedBy } = this._props;
572
+ const opt = conn.buildOpt(p1, 'replace');
539
573
  if (!ignoreCheck) {
540
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'replace' });
541
- (0, type_1.assertText)(doc._urn, { field: 'doc._urn', where: 'DbRepo', method: 'replace' });
574
+ (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'replace', queryName: opt.name });
575
+ (0, type_1.assertText)(doc._urn, { field: 'doc._urn', where: 'DbRepo', method: 'replace', queryName: opt.name });
542
576
  if (useNumericId) {
543
- (0, type_1.assertInteger)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'insert' });
577
+ (0, type_1.assertInteger)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'replace', queryName: opt.name });
544
578
  }
545
579
  else {
546
- (0, type_1.assertText)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'insert' });
580
+ (0, type_1.assertText)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'replace', queryName: opt.name });
547
581
  }
548
582
  }
549
583
  if (doc._trashId !== undefined) {
@@ -555,41 +589,87 @@ class DbRepo {
555
589
  if (useUpdatedBy) {
556
590
  // todo
557
591
  }
558
- const replacedId = yield this.$replace(doc, p1, true);
559
- if (cache.props.isConnected) {
560
- cache.clearDoc(replacedId)
592
+ const replacedKey = yield this.$replace(doc, opt, true);
593
+ if (!opt.noCache && cache.props.isConnected) {
594
+ cache.clearDoc(replacedKey)
561
595
  .then()
562
- .catch(e => this.logger.warn(`replace.cache.clearDoc <${e.name}> ${e.message}`));
596
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'replace', 'cache', 'clearDoc')));
563
597
  }
564
- return replacedId;
598
+ return replacedKey;
565
599
  });
566
600
  }
567
601
  // endregion replace
602
+ // region upsert
603
+ /** @inheritDoc */
604
+ upsert(doc, p1, ignoreCheck) {
605
+ return __awaiter(this, void 0, void 0, function* () {
606
+ const { noInsert, conn, noUpdate, cache, useNumericId, useUpdatedAt, useUpdatedBy } = this._props;
607
+ const opt = conn.buildOpt(p1, 'upsert');
608
+ const param = { where: 'DbRepo', method: 'upsert', queryName: opt.name };
609
+ if (noInsert || noUpdate) {
610
+ throw new error_1.DbNotSupportedError('Upsert', param);
611
+ }
612
+ if (!ignoreCheck) {
613
+ (0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
614
+ (0, type_1.assertText)(doc._urn, Object.assign({ field: 'doc._urn' }, param));
615
+ if (useNumericId) {
616
+ (0, type_1.assertInteger)(doc.id, Object.assign({ field: 'doc.id' }, param));
617
+ }
618
+ else {
619
+ (0, type_1.assertText)(doc.id, Object.assign({ field: 'doc.id' }, param));
620
+ }
621
+ }
622
+ if (doc._trashId !== undefined) {
623
+ delete doc._trashId;
624
+ }
625
+ if (useUpdatedAt) {
626
+ doc.updatedAt = this._now;
627
+ }
628
+ if (useUpdatedBy) {
629
+ // todo
630
+ }
631
+ const createdKey = yield this.$replace(doc, opt, true);
632
+ if (!opt.noCache && cache.props.isConnected) {
633
+ cache.clearDoc(createdKey)
634
+ .then()
635
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'upsert', 'cache', 'clearDoc')));
636
+ }
637
+ return createdKey;
638
+ });
639
+ }
640
+ // endregion upsert
568
641
  // region update
569
642
  /** @inheritDoc */
570
643
  update(k1, doc, p1, ignoreCheck) {
571
644
  return __awaiter(this, void 0, void 0, function* () {
645
+ const { conn } = this._props;
646
+ const opt = conn.buildOpt(p1, 'update');
572
647
  if (!ignoreCheck) {
573
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'update' });
648
+ (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'update', queryName: opt.name });
574
649
  }
575
650
  const [key, field] = this._checkKey(k1);
576
651
  switch (field) {
577
652
  case "_urn":
578
- return this.updateByPrimary(key, doc, p1, true);
653
+ return this.updateByPrimary(key, doc, opt, true);
579
654
  case "id":
580
- return this.updateBySecondary(key, doc, p1, true);
655
+ return this.updateBySecondary(key, doc, opt, true);
581
656
  default:
582
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'update' });
657
+ (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'update', queryName: opt.name });
583
658
  }
584
659
  });
585
660
  }
586
661
  /** @inheritDoc */
587
662
  updateByPrimary(key, doc, p1, ignoreCheck) {
588
663
  return __awaiter(this, void 0, void 0, function* () {
589
- const { forbiddenSet, cache, useUpdatedAt, useUpdatedBy } = this._props;
664
+ const { noUpdate, conn, forbiddenSet, cache, useUpdatedAt, useUpdatedBy } = this._props;
665
+ const opt = conn.buildOpt(p1, 'updateByPrimary');
666
+ const param = { where: 'DbRepo', method: 'updateByPrimary', queryName: opt.name };
667
+ if (noUpdate) {
668
+ throw new error_1.DbNotSupportedError('Update', param);
669
+ }
590
670
  if (!ignoreCheck) {
591
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'updateByPrimary' });
592
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'updateByPrimary' });
671
+ (0, type_1.assertText)(key, Object.assign({ field: 'key' }, param));
672
+ (0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
593
673
  }
594
674
  if (doc._trashId !== undefined) {
595
675
  delete doc._trashId;
@@ -605,11 +685,11 @@ class DbRepo {
605
685
  if (useUpdatedBy) {
606
686
  // todo
607
687
  }
608
- const updatedKey = yield this.$updateByPrimary(key, doc, p1, true);
609
- if (updatedKey && cache.props.isConnected) {
688
+ const updatedKey = yield this.$updateByPrimary(key, doc, opt, true);
689
+ if (updatedKey && !opt.noCache && cache.props.isConnected) {
610
690
  cache.clearDoc(updatedKey)
611
691
  .then()
612
- .catch(e => this.logger.warn(`updateByPrimary.cache.clearDoc <${e.name}> ${e.message}`));
692
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'updateByPrimary', 'cache', 'clearDoc')));
613
693
  }
614
694
  return updatedKey;
615
695
  });
@@ -617,18 +697,23 @@ class DbRepo {
617
697
  /** @inheritDoc */
618
698
  updateBySecondary(key, doc, p1, ignoreCheck) {
619
699
  return __awaiter(this, void 0, void 0, function* () {
620
- const { forbiddenSet, cache, useNumericId, idSame, useUpdatedAt, useUpdatedBy } = this._props;
700
+ const { forbiddenSet, noUpdate, cache, useNumericId, idSame, useUpdatedAt, useUpdatedBy, conn } = this._props;
701
+ const opt = conn.buildOpt(p1, 'updateBySecondary');
702
+ const param = { where: 'DbRepo', method: 'updateBySecondary', queryName: opt.name };
703
+ if (noUpdate) {
704
+ throw new error_1.DbNotSupportedError('Update', param);
705
+ }
621
706
  if (!ignoreCheck) {
622
707
  if (useNumericId) {
623
- (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary' });
708
+ (0, type_1.assertInteger)(key, Object.assign({ field: 'key' }, param));
624
709
  }
625
710
  else {
626
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary' });
711
+ (0, type_1.assertText)(key, Object.assign({ field: 'key' }, param));
627
712
  }
628
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'getBySecondary' });
713
+ (0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
629
714
  }
630
715
  if (idSame) {
631
- return this.updateBySecondary(this._keyToUrn(key), doc, p1, true);
716
+ return this.updateBySecondary(this._keyToUrn(key), doc, opt, true);
632
717
  }
633
718
  if (doc._trashId !== undefined) {
634
719
  delete doc._trashId;
@@ -644,11 +729,11 @@ class DbRepo {
644
729
  if (useUpdatedBy) {
645
730
  // todo
646
731
  }
647
- const updatedKey = yield this.$updateBySecondary(key, doc, p1, true);
648
- if (updatedKey && cache.props.isConnected) {
732
+ const updatedKey = yield this.$updateBySecondary(key, doc, opt, true);
733
+ if (updatedKey && !opt.noCache && cache.props.isConnected) {
649
734
  cache.clearDoc(updatedKey)
650
735
  .then()
651
- .catch(e => this.logger.warn(`updateBySecondary.cache.clearDoc <${e.name}> ${e.message}`));
736
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'updateBySecondary', 'cache', 'clearDoc')));
652
737
  }
653
738
  return updatedKey;
654
739
  });
@@ -658,24 +743,28 @@ class DbRepo {
658
743
  /** @inheritDoc */
659
744
  set(key, doc, p1, ignoreCheck) {
660
745
  return __awaiter(this, void 0, void 0, function* () {
746
+ const { conn } = this._props;
747
+ const opt = conn.buildOpt(p1, 'set');
661
748
  if (!ignoreCheck) {
662
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'set' });
749
+ (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'set', queryName: opt.name });
663
750
  }
664
751
  for (const [f, v] of Object.entries(doc)) {
665
752
  if (v === undefined) { // dont allow undefined
666
753
  delete doc[f];
667
754
  }
668
755
  }
669
- return this.update(key, doc, p1, false);
756
+ return this.update(key, doc, opt, false);
670
757
  });
671
758
  }
672
759
  /** @inheritDoc */
673
760
  unset(key, fields, p1, ignoreCheck) {
674
761
  return __awaiter(this, void 0, void 0, function* () {
762
+ const { conn } = this._props;
763
+ const opt = conn.buildOpt(p1, 'unset');
675
764
  if (!ignoreCheck) {
676
- (0, type_1.assertArray)(fields, { field: 'fields', where: 'DbRepo', method: 'unset' });
765
+ (0, type_1.assertArray)(fields, { field: 'fields', where: 'DbRepo', method: 'unset', queryName: opt.name });
677
766
  fields.forEach((field, index) => {
678
- (0, type_1.assertText)(field, { field: field, where: 'DbRepo', method: 'unset', index });
767
+ (0, type_1.assertText)(field, { field: field, where: 'DbRepo', method: 'unset', index, queryName: opt.name });
679
768
  });
680
769
  }
681
770
  const { forbiddenUnset } = this._props;
@@ -685,7 +774,7 @@ class DbRepo {
685
774
  doc[f] = undefined;
686
775
  }
687
776
  });
688
- return this.update(key, doc, p1, false);
777
+ return this.update(key, doc, opt, false);
689
778
  });
690
779
  }
691
780
  // endregion set
@@ -693,29 +782,36 @@ class DbRepo {
693
782
  /** @inheritDoc */
694
783
  remove(k1, p1) {
695
784
  return __awaiter(this, void 0, void 0, function* () {
785
+ const { conn } = this._props;
786
+ const opt = conn.buildOpt(p1, 'remove');
696
787
  const [key, field] = this._checkKey(k1);
697
788
  switch (field) {
698
789
  case "_urn":
699
- return this.removeByPrimary(key, p1, true);
790
+ return this.removeByPrimary(key, opt, true);
700
791
  case "id":
701
- return this.removeBySecondary(key, p1, true);
792
+ return this.removeBySecondary(key, opt, true);
702
793
  default:
703
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'remove' });
794
+ (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'remove', queryName: opt.name });
704
795
  }
705
796
  });
706
797
  }
707
798
  /** @inheritDoc */
708
799
  removeByPrimary(key, p1, ignoreCheck) {
709
800
  return __awaiter(this, void 0, void 0, function* () {
710
- const { cache } = this._props;
801
+ const { cache, noRemove, conn } = this._props;
802
+ const opt = conn.buildOpt(p1, 'removeByPrimary');
803
+ const param = { where: 'DbRepo', method: 'removeByPrimary', queryName: opt.name };
804
+ if (noRemove) {
805
+ throw new error_1.DbNotSupportedError('Remove', param);
806
+ }
711
807
  if (!ignoreCheck) {
712
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'removeByPrimary' });
808
+ (0, type_1.assertText)(key, Object.assign({ field: 'key' }, param));
713
809
  }
714
- const removedKey = yield this.$removeByPrimary(key, p1, true);
715
- if (removedKey && cache.props.isConnected) {
810
+ const removedKey = yield this.$removeByPrimary(key, opt, true);
811
+ if (removedKey && !opt.noCache && cache.props.isConnected) {
716
812
  cache.clearDoc(removedKey)
717
813
  .then()
718
- .catch(e => this.logger.warn(`removeByPrimary.cache.clearDoc <${e.name}> ${e.message}`));
814
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'removeByPrimary', 'cache', 'clearDoc')));
719
815
  }
720
816
  return removedKey;
721
817
  });
@@ -723,21 +819,26 @@ class DbRepo {
723
819
  /** @inheritDoc */
724
820
  removeBySecondary(key, p1, ignoreCheck) {
725
821
  return __awaiter(this, void 0, void 0, function* () {
726
- const { cache, useNumericId, idSame } = this._props;
822
+ const { cache, noRemove, useNumericId, idSame, conn } = this._props;
823
+ const opt = conn.buildOpt(p1, 'removeBySecondary');
824
+ const param = { where: 'DbRepo', method: 'removeByPrimary', queryName: opt.name };
825
+ if (noRemove) {
826
+ throw new error_1.DbNotSupportedError('Remove', param);
827
+ }
727
828
  if (useNumericId) {
728
- (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'removeBySecondary' });
829
+ (0, type_1.assertInteger)(key, { field: 'key', param });
729
830
  }
730
831
  else {
731
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'removeBySecondary' });
832
+ (0, type_1.assertText)(key, { field: 'key', param });
732
833
  }
733
834
  if (idSame) {
734
- return this.removeByPrimary(this._keyToUrn(key), p1, ignoreCheck);
835
+ return this.removeByPrimary(this._keyToUrn(key), opt, ignoreCheck);
735
836
  }
736
- const removedKey = yield this.$removeBySecondary(key, p1, true);
737
- if (removedKey && cache.props.isConnected) {
837
+ const removedKey = yield this.$removeBySecondary(key, opt, true);
838
+ if (removedKey && !opt.noCache && cache.props.isConnected) {
738
839
  cache.clearDoc(removedKey)
739
840
  .then()
740
- .catch(e => this.logger.warn(`removeBySecondary.cache.clearDoc <${e.name}> ${e.message}`));
841
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'removeBySecondary', 'cache', 'clearDoc')));
741
842
  }
742
843
  return removedKey;
743
844
  });
@@ -747,36 +848,42 @@ class DbRepo {
747
848
  /** @inheritDoc */
748
849
  trash(k1, p1) {
749
850
  return __awaiter(this, void 0, void 0, function* () {
851
+ const { conn } = this._props;
852
+ const opt = conn.buildOpt(p1, 'trash');
750
853
  const [key, field] = this._checkKey(k1);
751
854
  switch (field) {
752
855
  case "_urn":
753
- return this.trashByPrimary(key, p1, true);
856
+ return this.trashByPrimary(key, opt, true);
754
857
  case "id":
755
- return this.trashBySecondary(key, p1, true);
858
+ return this.trashBySecondary(key, opt, true);
756
859
  default:
757
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'trash' });
860
+ (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'trash', queryName: opt.name });
758
861
  }
759
862
  });
760
863
  }
761
864
  /** @inheritDoc */
762
865
  trashByPrimary(key, p1, ignoreCheck) {
763
866
  return __awaiter(this, void 0, void 0, function* () {
764
- const { useSoftDelete, conn, cache } = this._props;
867
+ const { noTrash, useSoftDelete, conn, cache } = this._props;
868
+ const opt = conn.buildOpt(p1, 'trashByPrimary');
869
+ const param = { where: 'DbRepo', method: 'trashByPrimary', queryName: opt.name };
765
870
  if (!useSoftDelete) {
766
- throw new error_1.DbNotSupportedError('Soft delete', 'DbRepo', 'trashByPrimary');
871
+ throw new error_1.DbNotSupportedError('Soft Delete', param);
872
+ }
873
+ if (noTrash) {
874
+ throw new error_1.DbNotSupportedError('Trash', param);
767
875
  }
768
876
  if (!ignoreCheck) {
769
- (0, type_1.assertText)(key, { field: 'urn', where: 'DbRepo', method: 'trashByPrimary' });
877
+ (0, type_1.assertText)(key, Object.assign({ field: 'urn' }, param));
770
878
  }
771
- const opt = conn.buildOpt(p1, 'trashByPrimary');
772
879
  if (!(0, type_1.isText)(opt.trashId)) {
773
880
  opt.trashId = (0, node_crypto_1.randomUUID)();
774
881
  }
775
882
  const trashedKey = yield this.$trashByPrimary(key, opt, true);
776
- if (trashedKey && cache.props.isConnected) {
883
+ if (trashedKey && !opt.noCache && cache.props.isConnected) {
777
884
  cache.clearDoc(trashedKey)
778
885
  .then()
779
- .catch(e => this.logger.warn(`trashByPrimary.cache.clearDoc <${e.name}> ${e.message}`));
886
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'trashByPrimary', 'cache', 'clearDoc')));
780
887
  }
781
888
  return trashedKey;
782
889
  });
@@ -784,28 +891,32 @@ class DbRepo {
784
891
  /** @inheritDoc */
785
892
  trashBySecondary(key, p1, ignoreCheck) {
786
893
  return __awaiter(this, void 0, void 0, function* () {
787
- const { useSoftDelete, useNumericId, idSame, conn, cache } = this._props;
894
+ const { noTrash, useSoftDelete, useNumericId, idSame, conn, cache } = this._props;
895
+ const opt = conn.buildOpt(p1, 'trashBySecondary');
896
+ const param = { where: 'DbRepo', method: 'trashBySecondary', queryName: opt.name };
788
897
  if (!useSoftDelete) {
789
- throw new error_1.DbNotSupportedError('Soft delete', 'DbRepo', 'trashBySecondary');
898
+ throw new error_1.DbNotSupportedError('Soft Delete', param);
899
+ }
900
+ if (noTrash) {
901
+ throw new error_1.DbNotSupportedError('Trash', param);
790
902
  }
791
903
  if (useNumericId) {
792
- (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'trashBySecondary' });
904
+ (0, type_1.assertInteger)(key, Object.assign({ field: 'key' }, param));
793
905
  }
794
906
  else {
795
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'trashBySecondary' });
907
+ (0, type_1.assertText)(key, Object.assign({ field: 'key' }, param));
796
908
  }
797
909
  if (idSame) {
798
- return this.trashByPrimary(this._keyToUrn(key), p1, ignoreCheck);
910
+ return this.trashByPrimary(this._keyToUrn(key), opt, ignoreCheck);
799
911
  }
800
- const opt = conn.buildOpt(p1, 'trashBySecondary');
801
912
  if (!(0, type_1.isText)(opt.trashId)) {
802
913
  opt.trashId = (0, node_crypto_1.randomUUID)();
803
914
  }
804
915
  const trashedKey = yield this.$trashBySecondary(key, opt, true);
805
- if (trashedKey && cache.props.isConnected) {
916
+ if (trashedKey && !opt.noCache && cache.props.isConnected) {
806
917
  cache.clearDoc(trashedKey)
807
918
  .then()
808
- .catch(e => this.logger.warn(`trashBySecondary.cache.clearDoc <${e.name}> ${e.message}`));
919
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'trashBySecondary', 'cache', 'clearDoc')));
809
920
  }
810
921
  return trashedKey;
811
922
  });