@samet-it/be-db-common 1.1.1 → 1.1.3

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,15 +11,34 @@ 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
@@ -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');
@@ -226,6 +244,10 @@ class DbRepo {
226
244
  const { urnPrefix } = this._props;
227
245
  return (typeof key === 'string') && key.startsWith(urnPrefix + ':');
228
246
  }
247
+ _keyToUrn(key) {
248
+ const { urnPrefix } = this._props;
249
+ return `${urnPrefix}:${key}`;
250
+ }
229
251
  // endregion protected-method
230
252
  // region getter
231
253
  /** @inheritDoc */
@@ -233,19 +255,11 @@ class DbRepo {
233
255
  return this._props;
234
256
  }
235
257
  /** @inheritDoc */
236
- get $def() {
237
- return this;
238
- }
239
- /** @inheritDoc */
240
258
  $cast() {
241
259
  return this;
242
260
  }
243
261
  // endregion getter
244
262
  // region urn
245
- _keyToUrn(key) {
246
- const { urnPrefix } = this._props;
247
- return `${urnPrefix}:${key}`;
248
- }
249
263
  /** @inheritDoc */
250
264
  toUrn(urnRec) {
251
265
  const { urnPrefix, urnDelimiter } = this._props;
@@ -284,7 +298,7 @@ class DbRepo {
284
298
  found = (yield cache.getByPrimary(key))[0];
285
299
  }
286
300
  catch (e) {
287
- this.logger.warn(`getByPrimary.cache.getByPrimary <${e.name}> ${e.message}`);
301
+ this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'getByPrimary', 'cache', 'getByPrimary'));
288
302
  }
289
303
  if (found) {
290
304
  return found;
@@ -294,7 +308,7 @@ class DbRepo {
294
308
  if (found && cache.props.isConnected) {
295
309
  cache.ingestDoc(found)
296
310
  .then()
297
- .catch(e => this.logger.warn(`getByPrimary.cache.ingestDoc <${e.name}> ${e.message}`));
311
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'getByPrimary', 'cache', 'ingestDoc')));
298
312
  }
299
313
  return found;
300
314
  });
@@ -323,14 +337,14 @@ class DbRepo {
323
337
  }
324
338
  }
325
339
  catch (e) {
326
- 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'));
327
341
  }
328
342
  }
329
343
  found = yield this.$getBySecondary(key, p1, true);
330
344
  if (found && cache.props.isConnected) {
331
345
  cache.ingestDoc(found)
332
346
  .then()
333
- .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')));
334
348
  }
335
349
  return found;
336
350
  });
@@ -367,7 +381,7 @@ class DbRepo {
367
381
  }
368
382
  }
369
383
  catch (e) {
370
- this.logger.warn(`existsByPrimary.cache.existsByPrimary <${e.name}> ${e.message}`);
384
+ this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'existsByPrimary', 'cache', 'getByPrimary'));
371
385
  }
372
386
  }
373
387
  return this.$existsByPrimary(key, p1, true);
@@ -396,7 +410,7 @@ class DbRepo {
396
410
  }
397
411
  }
398
412
  catch (e) {
399
- this.logger.warn(`existsBySecondary.cache.existsBySecondary <${e.name}> ${e.message}`);
413
+ this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'existsBySecondary', 'cache', 'getBySecondary'));
400
414
  }
401
415
  }
402
416
  return this.$existsBySecondary(key, p1, true);
@@ -424,7 +438,7 @@ class DbRepo {
424
438
  founds = yield cache.getByPrimary(...result.urns);
425
439
  }
426
440
  catch (e) {
427
- this.logger.warn(`list.cache.getByPrimary <${e.name}> ${e.message}`);
441
+ this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'list', 'cache', 'getByPrimary'));
428
442
  }
429
443
  founds.forEach(val => {
430
444
  const index = result.urns.indexOf(val._urn);
@@ -440,7 +454,7 @@ class DbRepo {
440
454
  if (cache.props.isConnected) {
441
455
  founds.forEach(found => cache.ingestDoc(found)
442
456
  .then()
443
- .catch(e => this.logger.warn(`list.cache.ingestDoc <${e.name}> ${e.message}`)));
457
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'list', 'cache', 'ingestDoc'))));
444
458
  }
445
459
  docs.push(...founds);
446
460
  }
@@ -452,7 +466,7 @@ class DbRepo {
452
466
  founds = yield cache.getBySecondary(...result.ids);
453
467
  }
454
468
  catch (e) {
455
- this.logger.warn(`list.cache.getBySecondary <${e.name}> ${e.message}`);
469
+ this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'list', 'cache', 'getBySecondary'));
456
470
  }
457
471
  founds.forEach(val => {
458
472
  const index = result.ids.indexOf(val.id);
@@ -468,7 +482,7 @@ class DbRepo {
468
482
  if (cache.props.isConnected) {
469
483
  founds.forEach(found => cache.ingestDoc(found)
470
484
  .then()
471
- .catch(e => this.logger.warn(`list.cache.ingestDoc <${e.name}> ${e.message}`)));
485
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'list', 'cache', 'ingestDoc'))));
472
486
  }
473
487
  docs.push(...founds);
474
488
  }
@@ -492,7 +506,10 @@ class DbRepo {
492
506
  /** @inheritDoc */
493
507
  insert(doc, p1, ignoreCheck) {
494
508
  return __awaiter(this, void 0, void 0, function* () {
495
- const { useCreatedAt, useNumericId, useCreatedBy, useUpdatedAt, useUpdatedBy } = this._props;
509
+ const { useCreatedAt, noInsert, useNumericId, useCreatedBy, useUpdatedAt, useUpdatedBy } = this._props;
510
+ if (noInsert) {
511
+ throw new error_1.DbNotSupportedError('Insert', 'DbRepo', 'insert');
512
+ }
496
513
  if (!ignoreCheck) {
497
514
  (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'insert' });
498
515
  if (useNumericId) {
@@ -524,6 +541,10 @@ class DbRepo {
524
541
  /** @inheritDoc */
525
542
  inserts(docs, p1, ignoreCheck) {
526
543
  return __awaiter(this, void 0, void 0, function* () {
544
+ const { noInsert } = this._props;
545
+ if (noInsert) {
546
+ throw new error_1.DbNotSupportedError('Insert', 'DbRepo', 'inserts');
547
+ }
527
548
  if (!ignoreCheck) {
528
549
  (0, type_1.assertArray)(docs, { field: 'docs', where: 'DbRepo', method: 'inserts' });
529
550
  }
@@ -544,10 +565,10 @@ class DbRepo {
544
565
  (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'replace' });
545
566
  (0, type_1.assertText)(doc._urn, { field: 'doc._urn', where: 'DbRepo', method: 'replace' });
546
567
  if (useNumericId) {
547
- (0, type_1.assertInteger)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'insert' });
568
+ (0, type_1.assertInteger)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'replace' });
548
569
  }
549
570
  else {
550
- (0, type_1.assertText)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'insert' });
571
+ (0, type_1.assertText)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'replace' });
551
572
  }
552
573
  }
553
574
  if (doc._trashId !== undefined) {
@@ -559,16 +580,53 @@ class DbRepo {
559
580
  if (useUpdatedBy) {
560
581
  // todo
561
582
  }
562
- const replacedId = yield this.$replace(doc, p1, true);
583
+ const replacedKey = yield this.$replace(doc, p1, true);
563
584
  if (cache.props.isConnected) {
564
- cache.clearDoc(replacedId)
585
+ cache.clearDoc(replacedKey)
565
586
  .then()
566
- .catch(e => this.logger.warn(`replace.cache.clearDoc <${e.name}> ${e.message}`));
587
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'replace', 'cache', 'clearDoc')));
567
588
  }
568
- return replacedId;
589
+ return replacedKey;
569
590
  });
570
591
  }
571
592
  // endregion replace
593
+ // region upsert
594
+ /** @inheritDoc */
595
+ upsert(doc, p1, ignoreCheck) {
596
+ return __awaiter(this, void 0, void 0, function* () {
597
+ const { noInsert, noUpdate, cache, useNumericId, useUpdatedAt, useUpdatedBy } = this._props;
598
+ if (noInsert || noUpdate) {
599
+ throw new error_1.DbNotSupportedError('Upsert', 'DbRepo', 'upsert');
600
+ }
601
+ if (!ignoreCheck) {
602
+ (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'upsert' });
603
+ (0, type_1.assertText)(doc._urn, { field: 'doc._urn', where: 'DbRepo', method: 'upsert' });
604
+ if (useNumericId) {
605
+ (0, type_1.assertInteger)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'upsert' });
606
+ }
607
+ else {
608
+ (0, type_1.assertText)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'upsert' });
609
+ }
610
+ }
611
+ if (doc._trashId !== undefined) {
612
+ delete doc._trashId;
613
+ }
614
+ if (useUpdatedAt) {
615
+ doc.updatedAt = this._now;
616
+ }
617
+ if (useUpdatedBy) {
618
+ // todo
619
+ }
620
+ const createdKey = yield this.$replace(doc, p1, true);
621
+ if (cache.props.isConnected) {
622
+ cache.clearDoc(createdKey)
623
+ .then()
624
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'upsert', 'cache', 'clearDoc')));
625
+ }
626
+ return createdKey;
627
+ });
628
+ }
629
+ // endregion upsert
572
630
  // region update
573
631
  /** @inheritDoc */
574
632
  update(k1, doc, p1, ignoreCheck) {
@@ -590,7 +648,10 @@ class DbRepo {
590
648
  /** @inheritDoc */
591
649
  updateByPrimary(key, doc, p1, ignoreCheck) {
592
650
  return __awaiter(this, void 0, void 0, function* () {
593
- const { forbiddenSet, cache, useUpdatedAt, useUpdatedBy } = this._props;
651
+ const { noUpdate, forbiddenSet, cache, useUpdatedAt, useUpdatedBy } = this._props;
652
+ if (noUpdate) {
653
+ throw new error_1.DbNotSupportedError('Update', 'DbRepo', 'updateByPrimary');
654
+ }
594
655
  if (!ignoreCheck) {
595
656
  (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'updateByPrimary' });
596
657
  (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'updateByPrimary' });
@@ -613,7 +674,7 @@ class DbRepo {
613
674
  if (updatedKey && cache.props.isConnected) {
614
675
  cache.clearDoc(updatedKey)
615
676
  .then()
616
- .catch(e => this.logger.warn(`updateByPrimary.cache.clearDoc <${e.name}> ${e.message}`));
677
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'updateByPrimary', 'cache', 'clearDoc')));
617
678
  }
618
679
  return updatedKey;
619
680
  });
@@ -621,7 +682,10 @@ class DbRepo {
621
682
  /** @inheritDoc */
622
683
  updateBySecondary(key, doc, p1, ignoreCheck) {
623
684
  return __awaiter(this, void 0, void 0, function* () {
624
- const { forbiddenSet, cache, useNumericId, idSame, useUpdatedAt, useUpdatedBy } = this._props;
685
+ const { forbiddenSet, noUpdate, cache, useNumericId, idSame, useUpdatedAt, useUpdatedBy } = this._props;
686
+ if (noUpdate) {
687
+ throw new error_1.DbNotSupportedError('Update', 'DbRepo', 'updateBySecondary');
688
+ }
625
689
  if (!ignoreCheck) {
626
690
  if (useNumericId) {
627
691
  (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary' });
@@ -652,7 +716,7 @@ class DbRepo {
652
716
  if (updatedKey && cache.props.isConnected) {
653
717
  cache.clearDoc(updatedKey)
654
718
  .then()
655
- .catch(e => this.logger.warn(`updateBySecondary.cache.clearDoc <${e.name}> ${e.message}`));
719
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'updateBySecondary', 'cache', 'clearDoc')));
656
720
  }
657
721
  return updatedKey;
658
722
  });
@@ -711,7 +775,10 @@ class DbRepo {
711
775
  /** @inheritDoc */
712
776
  removeByPrimary(key, p1, ignoreCheck) {
713
777
  return __awaiter(this, void 0, void 0, function* () {
714
- const { cache } = this._props;
778
+ const { cache, noRemove } = this._props;
779
+ if (noRemove) {
780
+ throw new error_1.DbNotSupportedError('Remove', 'DbRepo', 'removeByPrimary');
781
+ }
715
782
  if (!ignoreCheck) {
716
783
  (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'removeByPrimary' });
717
784
  }
@@ -719,7 +786,7 @@ class DbRepo {
719
786
  if (removedKey && cache.props.isConnected) {
720
787
  cache.clearDoc(removedKey)
721
788
  .then()
722
- .catch(e => this.logger.warn(`removeByPrimary.cache.clearDoc <${e.name}> ${e.message}`));
789
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'removeByPrimary', 'cache', 'clearDoc')));
723
790
  }
724
791
  return removedKey;
725
792
  });
@@ -727,7 +794,10 @@ class DbRepo {
727
794
  /** @inheritDoc */
728
795
  removeBySecondary(key, p1, ignoreCheck) {
729
796
  return __awaiter(this, void 0, void 0, function* () {
730
- const { cache, useNumericId, idSame } = this._props;
797
+ const { cache, noRemove, useNumericId, idSame } = this._props;
798
+ if (noRemove) {
799
+ throw new error_1.DbNotSupportedError('Remove', 'DbRepo', 'removeByPrimary');
800
+ }
731
801
  if (useNumericId) {
732
802
  (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'removeBySecondary' });
733
803
  }
@@ -741,7 +811,7 @@ class DbRepo {
741
811
  if (removedKey && cache.props.isConnected) {
742
812
  cache.clearDoc(removedKey)
743
813
  .then()
744
- .catch(e => this.logger.warn(`removeBySecondary.cache.clearDoc <${e.name}> ${e.message}`));
814
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'removeBySecondary', 'cache', 'clearDoc')));
745
815
  }
746
816
  return removedKey;
747
817
  });
@@ -765,9 +835,9 @@ class DbRepo {
765
835
  /** @inheritDoc */
766
836
  trashByPrimary(key, p1, ignoreCheck) {
767
837
  return __awaiter(this, void 0, void 0, function* () {
768
- const { useSoftDelete, conn, cache } = this._props;
769
- if (!useSoftDelete) {
770
- throw new error_1.DbNotSupportedError('Soft delete', 'DbRepo', 'trashByPrimary');
838
+ const { noTrash, conn, cache } = this._props;
839
+ if (noTrash) {
840
+ throw new error_1.DbNotSupportedError('Trash', 'DbRepo', 'trashByPrimary');
771
841
  }
772
842
  if (!ignoreCheck) {
773
843
  (0, type_1.assertText)(key, { field: 'urn', where: 'DbRepo', method: 'trashByPrimary' });
@@ -780,7 +850,7 @@ class DbRepo {
780
850
  if (trashedKey && cache.props.isConnected) {
781
851
  cache.clearDoc(trashedKey)
782
852
  .then()
783
- .catch(e => this.logger.warn(`trashByPrimary.cache.clearDoc <${e.name}> ${e.message}`));
853
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'trashByPrimary', 'cache', 'clearDoc')));
784
854
  }
785
855
  return trashedKey;
786
856
  });
@@ -788,9 +858,9 @@ class DbRepo {
788
858
  /** @inheritDoc */
789
859
  trashBySecondary(key, p1, ignoreCheck) {
790
860
  return __awaiter(this, void 0, void 0, function* () {
791
- const { useSoftDelete, useNumericId, idSame, conn, cache } = this._props;
792
- if (!useSoftDelete) {
793
- throw new error_1.DbNotSupportedError('Soft delete', 'DbRepo', 'trashBySecondary');
861
+ const { noTrash, useNumericId, idSame, conn, cache } = this._props;
862
+ if (noTrash) {
863
+ throw new error_1.DbNotSupportedError('Trash', 'DbRepo', 'trashBySecondary');
794
864
  }
795
865
  if (useNumericId) {
796
866
  (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'trashBySecondary' });
@@ -809,7 +879,7 @@ class DbRepo {
809
879
  if (trashedKey && cache.props.isConnected) {
810
880
  cache.clearDoc(trashedKey)
811
881
  .then()
812
- .catch(e => this.logger.warn(`trashBySecondary.cache.clearDoc <${e.name}> ${e.message}`));
882
+ .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'trashBySecondary', 'cache', 'clearDoc')));
813
883
  }
814
884
  return trashedKey;
815
885
  });