@samet-it/be-db-common 1.3.2 → 1.3.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.
@@ -1,21 +1,10 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.DbRepo = void 0;
13
- const node_crypto_1 = require("node:crypto");
14
- const be_base_common_1 = require("@samet-it/be-base-common");
15
- const common_1 = require("@leyyo/common");
16
- const query_1 = require("@leyyo/query");
17
- const type_1 = require("@leyyo/type");
18
- const error_1 = require("../error");
1
+ import { randomUUID } from "node:crypto";
2
+ import { errorHandler } from "@samet-it/be-base-common";
3
+ import { errorCommon, isFilledObj, isText } from "@leyyo/common";
4
+ import { queryParser } from "@leyyo/query";
5
+ import { assertArray, assertInteger, assertMessage, assertObject, assertText } from "@leyyo/assert";
6
+ import { DbError, DbInvalidValueError, DbNotSupportedError } from "../error/index.js";
7
+ import { isBareObject } from "@leyyo/type";
19
8
  const FORBIDDEN_KEYS = ['id', 'urn', '_trashId', 'createdAt', 'createdBy', 'updatedAt', 'updatedBy', "_rev", '_search', '_alpha', '_irregular'];
20
9
  const STRICT_KEYS = ['id', 'urn', '_trashId', "_rev", '_irregular'];
21
10
  // noinspection JSUnusedGlobalSymbols
@@ -35,7 +24,21 @@ const STRICT_KEYS = ['id', 'urn', '_trashId', "_rev", '_irregular'];
35
24
  * - 9-`KEYS`: keys to autocomplete, def: keys of {@link Entity}
36
25
  * - 10-`DIMS`: dimensions for presentation layer {@link DefDims}
37
26
  * */
38
- class DbRepo {
27
+ export class DbRepo {
28
+ // region protected-property
29
+ /**
30
+ * Logger
31
+ * */
32
+ logger;
33
+ /**
34
+ * Option of in-body usage in place of constructor param
35
+ * */
36
+ _opt = {};
37
+ /**
38
+ * Produced setting from option
39
+ * @see _opt
40
+ * */
41
+ _props;
39
42
  // endregion protected-property
40
43
  /**
41
44
  * Constructor
@@ -44,21 +47,17 @@ class DbRepo {
44
47
  * @param {DbRepoOpt} opt - options
45
48
  * */
46
49
  constructor(conn, opt) {
47
- /**
48
- * Option of in-body usage in place of constructor param
49
- * */
50
- this._opt = {};
51
- if (!(0, common_1.isFilledObj)(conn)) {
52
- throw new error_1.DbInvalidValueError('Connection', { where: 'DbRepo', method: 'constructor', type: typeof conn });
53
- }
54
- if (!(0, type_1.isBareObject)(this._opt)) {
50
+ if (!isFilledObj(conn)) {
51
+ throw new DbInvalidValueError('Connection', { where: 'DbRepo', method: 'constructor', type: typeof conn });
52
+ }
53
+ if (!isBareObject(this._opt)) {
55
54
  this._opt = {};
56
55
  }
57
- if ((0, type_1.isBareObject)(opt)) {
58
- this._opt = Object.assign(Object.assign({}, this._opt), opt);
56
+ if (isBareObject(opt)) {
57
+ this._opt = { ...this._opt, ...opt };
59
58
  }
60
59
  this._checkOptions();
61
- this._props = Object.assign({ conn }, this._opt);
60
+ this._props = { conn, ...this._opt };
62
61
  delete this._opt;
63
62
  }
64
63
  // region protected-method
@@ -72,7 +71,7 @@ class DbRepo {
72
71
  }
73
72
  if (this._opt.useVersion) {
74
73
  if (typeof this._opt.version !== 'number') {
75
- throw new error_1.DbInvalidValueError('Model version', { where: 'DbRepo', method: 'constructor', type: typeof this._opt.version });
74
+ throw new DbInvalidValueError('Model version', { where: 'DbRepo', method: 'constructor', type: typeof this._opt.version });
76
75
  }
77
76
  }
78
77
  else {
@@ -97,7 +96,7 @@ class DbRepo {
97
96
  }
98
97
  }
99
98
  if (!this._opt.urnPrefix) {
100
- throw new error_1.DbInvalidValueError('Urn prefix', { where: 'DbRepo', method: 'constructor', type: typeof this._opt.urnPrefix });
99
+ throw new DbInvalidValueError('Urn prefix', { where: 'DbRepo', method: 'constructor', type: typeof this._opt.urnPrefix });
101
100
  }
102
101
  if (!Array.isArray(this._opt.forbiddenSet)) {
103
102
  this._opt.forbiddenSet = [...FORBIDDEN_KEYS];
@@ -114,9 +113,9 @@ class DbRepo {
114
113
  this.$toUrnTuple = directOpt.$toUrnTuple;
115
114
  }
116
115
  if (typeof directOpt.$toDim === 'function') {
117
- this.$toDim = (doc, dim) => __awaiter(this, void 0, void 0, function* () {
118
- return (yield directOpt.$toDim(doc, dim));
119
- });
116
+ this.$toDim = async (doc, dim) => {
117
+ return (await directOpt.$toDim(doc, dim));
118
+ };
120
119
  }
121
120
  if (!Array.isArray(directOpt.indexedFields)) {
122
121
  directOpt.indexedFields = [];
@@ -139,7 +138,7 @@ class DbRepo {
139
138
  * @return {string}
140
139
  * */
141
140
  get _randomIndexName() {
142
- 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)
141
+ return 'idx_' + randomUUID().match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
143
142
  .map(s => s.toLowerCase())
144
143
  .join('_');
145
144
  }
@@ -177,8 +176,8 @@ class DbRepo {
177
176
  * */
178
177
  _urnFromDoc(doc, method) {
179
178
  const param = { where: 'DbRepo', path: this.path, method };
180
- (0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
181
- (0, type_1.assertText)(doc.urn, Object.assign({ field: 'urn' }, param));
179
+ assertObject(doc, { field: 'doc', ...param });
180
+ assertText(doc.urn, { field: 'urn', ...param });
182
181
  return doc.urn;
183
182
  }
184
183
  /**
@@ -404,9 +403,8 @@ class DbRepo {
404
403
  }
405
404
  /** @inheritDoc */
406
405
  get urnLength() {
407
- var _a, _b;
408
406
  if (!this._props.urnLength) {
409
- this._props.urnLength = (_b = (_a = this._props.urnPrefix) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
407
+ this._props.urnLength = this._props.urnPrefix?.length ?? 0;
410
408
  }
411
409
  return this._props.urnLength;
412
410
  }
@@ -414,26 +412,25 @@ class DbRepo {
414
412
  // region field-value
415
413
  /** {@inheritDoc} */
416
414
  checkError(err, opt) {
417
- var _a, _b, _c;
418
- const size = be_base_common_1.errorHandler.addStat(err);
419
- const ignoredErrors = (_a = opt === null || opt === void 0 ? void 0 : opt.ignoredErrors) !== null && _a !== void 0 ? _a : [];
415
+ const size = errorHandler.addStat(err);
416
+ const ignoredErrors = opt?.ignoredErrors ?? [];
420
417
  if (ignoredErrors.includes(err) || ignoredErrors.includes(err.name)) {
421
418
  return;
422
419
  }
423
- if (opt === null || opt === void 0 ? void 0 : opt.silent) {
420
+ if (opt?.silent) {
424
421
  if (size < 100) {
425
- this.logger.warn(common_1.errorCommon.text(err, ((_b = opt.name) !== null && _b !== void 0 ? _b : 'Database error'), size));
422
+ this.logger.warn(errorCommon.text(err, (opt.name ?? 'Database error'), size));
426
423
  }
427
424
  return;
428
425
  }
429
426
  if (size < 100) {
430
- this.logger.error(common_1.errorCommon.text(err, ((_c = opt.name) !== null && _c !== void 0 ? _c : 'Database error'), size));
427
+ this.logger.error(errorCommon.text(err, (opt.name ?? 'Database error'), size));
431
428
  }
432
- if (err instanceof error_1.DbError) {
429
+ if (err instanceof DbError) {
433
430
  throw err;
434
431
  }
435
432
  else {
436
- throw common_1.errorCommon.forcedCast(error_1.DbError, err, { queryName: opt.name });
433
+ throw errorCommon.forcedCast(DbError, err, { queryName: opt.name });
437
434
  }
438
435
  }
439
436
  /** {@inheritDoc} */
@@ -468,7 +465,7 @@ class DbRepo {
468
465
  // region option
469
466
  /** {@inheritDoc} */
470
467
  buildOpt(p1, name) {
471
- const opt = (typeof p1 === 'string' ? { name: p1 } : Object.assign({}, p1));
468
+ const opt = (typeof p1 === 'string' ? { name: p1 } : { ...p1 });
472
469
  if (typeof opt.name === undefined && typeof name !== undefined) {
473
470
  opt.name = name;
474
471
  }
@@ -478,26 +475,39 @@ class DbRepo {
478
475
  // endregion option
479
476
  // region query
480
477
  /** {@inheritDoc} */
481
- exec(fn, p1) {
482
- return __awaiter(this, void 0, void 0, function* () {
483
- if (!this.isEnabled) {
484
- return undefined;
485
- }
486
- const opt = this.buildOpt(p1);
487
- try {
488
- return yield fn;
489
- }
490
- catch (err) {
491
- this.checkError(err, opt);
492
- return undefined;
493
- }
494
- });
478
+ async exec(fn, p1) {
479
+ if (!this.isEnabled) {
480
+ return undefined;
481
+ }
482
+ const opt = this.buildOpt(p1);
483
+ try {
484
+ return await fn;
485
+ }
486
+ catch (err) {
487
+ this.checkError(err, opt);
488
+ return undefined;
489
+ }
495
490
  }
496
491
  // endregion query
497
492
  // region urn
498
493
  /** @inheritDoc */
499
494
  toUrn(urnRec) {
500
- return `${this.urnPrefix}:${this.$toUrnTuple(urnRec).join(this.urnDelimiter)}`;
495
+ let code = this.$toUrnTuple(urnRec).join(this.urnDelimiter);
496
+ if (code.includes(':')) {
497
+ code = code.split(':').join(';');
498
+ }
499
+ return `${this.urnPrefix}:${code}`;
500
+ }
501
+ /** @inheritDoc */
502
+ toUrnKey(p1) {
503
+ if (isText(p1)) {
504
+ return p1.split(':').pop();
505
+ }
506
+ const doc = p1;
507
+ if (isFilledObj(doc)) {
508
+ return this.toUrnKey(doc.urn);
509
+ }
510
+ return undefined;
501
511
  }
502
512
  /** @inheritDoc */
503
513
  $toUrnTuple(urnRec) {
@@ -506,743 +516,723 @@ class DbRepo {
506
516
  // endregion urn
507
517
  // region get
508
518
  /** @inheritDoc */
509
- get(keyLike, p1) {
510
- return __awaiter(this, void 0, void 0, function* () {
511
- const opt = this.buildOpt(p1, 'get');
512
- const [key, field] = this._checkKey(keyLike);
513
- switch (field) {
514
- case "urn":
515
- return this.getByPrimary(key, opt, true);
516
- case "id":
517
- return this.getBySecondary(key, opt, true);
518
- default:
519
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'get', queryName: opt.name });
520
- }
521
- });
519
+ async get(keyLike, p1) {
520
+ const opt = this.buildOpt(p1, 'get');
521
+ const [key, field] = this._checkKey(keyLike);
522
+ switch (field) {
523
+ case "urn":
524
+ return this.getByPrimary(key, opt, true);
525
+ case "id":
526
+ return this.getBySecondary(key, opt, true);
527
+ default:
528
+ assertMessage('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'get', queryName: opt.name });
529
+ }
522
530
  }
523
531
  /** @inheritDoc */
524
- getByPrimary(key, p1, ignoreCheck) {
525
- return __awaiter(this, void 0, void 0, function* () {
526
- const opt = this.buildOpt(p1, 'getByPrimary');
527
- if (!ignoreCheck) {
528
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'getByPrimary', queryName: opt.name });
532
+ async getByPrimary(key, p1, ignoreCheck) {
533
+ const opt = this.buildOpt(p1, 'getByPrimary');
534
+ if (!ignoreCheck) {
535
+ assertText(key, { field: 'key', where: 'DbRepo', method: 'getByPrimary', queryName: opt.name });
536
+ }
537
+ let found;
538
+ if (!opt.noCache && this.cache.isConnected) {
539
+ try {
540
+ found = await this.cache.getByPrimary(key, opt);
529
541
  }
530
- let found;
531
- if (!opt.noCache && this.cache.isConnected) {
532
- try {
533
- found = yield this.cache.getByPrimary(key, opt);
534
- }
535
- catch (e) {
536
- this.logger.warn(common_1.errorCommon.text(e, 'getByPrimary', 'cache', 'getByPrimary'));
537
- }
538
- if (found) {
539
- return found;
540
- }
542
+ catch (e) {
543
+ this.logger.warn(errorCommon.text(e, 'getByPrimary', 'cache', 'getByPrimary'));
541
544
  }
542
- found = yield this.$getByPrimary(key, opt, true);
543
- if (!opt.noCache && found && this.cache.isConnected) {
544
- this.cache.ingestDoc(found, opt)
545
- .then()
546
- .catch(e => this.logger.warn(common_1.errorCommon.text(e, 'getByPrimary', 'cache', 'ingestDoc')));
545
+ if (found) {
546
+ return found;
547
547
  }
548
- return found;
549
- });
548
+ }
549
+ found = await this.$getByPrimary(key, opt, true);
550
+ if (!opt.noCache && found && this.cache.isConnected) {
551
+ this.cache.ingestDoc(found, opt)
552
+ .then()
553
+ .catch(e => this.logger.warn(errorCommon.text(e, 'getByPrimary', 'cache', 'ingestDoc')));
554
+ }
555
+ return found;
550
556
  }
551
557
  /** @inheritDoc */
552
- getBySecondary(key, p1, ignoreCheck) {
553
- return __awaiter(this, void 0, void 0, function* () {
554
- const opt = this.buildOpt(p1, 'getBySecondary');
555
- if (!ignoreCheck) {
556
- if (this.hasNumericId) {
557
- (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary', queryName: opt.name });
558
- }
559
- else {
560
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary', queryName: opt.name });
561
- }
558
+ async getBySecondary(key, p1, ignoreCheck) {
559
+ const opt = this.buildOpt(p1, 'getBySecondary');
560
+ if (!ignoreCheck) {
561
+ if (this.hasNumericId) {
562
+ assertInteger(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary', queryName: opt.name });
562
563
  }
563
- if (this.isIdSame) {
564
- return this.getByPrimary(this._keyToUrn(key), opt, true);
564
+ else {
565
+ assertText(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary', queryName: opt.name });
565
566
  }
566
- let found;
567
- if (!opt.noCache && this.cache.isConnected) {
568
- try {
569
- found = yield this.cache.getBySecondary(key, opt);
570
- if (found) {
571
- return found;
572
- }
573
- }
574
- catch (e) {
575
- this.logger.warn(common_1.errorCommon.text(e, 'getBySecondary', 'cache', 'getBySecondary'));
567
+ }
568
+ if (this.isIdSame) {
569
+ return this.getByPrimary(this._keyToUrn(key), opt, true);
570
+ }
571
+ let found;
572
+ if (!opt.noCache && this.cache.isConnected) {
573
+ try {
574
+ found = await this.cache.getBySecondary(key, opt);
575
+ if (found) {
576
+ return found;
576
577
  }
577
578
  }
578
- found = yield this.$getBySecondary(key, opt, true);
579
- if (found && this.cache.isConnected) {
580
- this.cache.ingestDoc(found, opt)
581
- .then()
582
- .catch(e => this.logger.warn(common_1.errorCommon.text(e, 'getBySecondary', 'cache', 'ingestDoc')));
579
+ catch (e) {
580
+ this.logger.warn(errorCommon.text(e, 'getBySecondary', 'cache', 'getBySecondary'));
583
581
  }
584
- return found;
585
- });
582
+ }
583
+ found = await this.$getBySecondary(key, opt, true);
584
+ if (found && this.cache.isConnected) {
585
+ this.cache.ingestDoc(found, opt)
586
+ .then()
587
+ .catch(e => this.logger.warn(errorCommon.text(e, 'getBySecondary', 'cache', 'ingestDoc')));
588
+ }
589
+ return found;
586
590
  }
587
591
  // endregion get
588
592
  // region exists
589
593
  /** @inheritDoc */
590
- exists(keyLike, p1) {
591
- return __awaiter(this, void 0, void 0, function* () {
592
- const opt = this.buildOpt(p1, 'exists');
593
- const [key, field] = this._checkKey(keyLike);
594
- switch (field) {
595
- case "urn":
596
- return this.existsByPrimary(key, opt, true);
597
- case "id":
598
- return this.existsBySecondary(key, opt, true);
599
- default:
600
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'exists', queryName: opt.name });
601
- break;
602
- }
603
- });
594
+ async exists(keyLike, p1) {
595
+ const opt = this.buildOpt(p1, 'exists');
596
+ const [key, field] = this._checkKey(keyLike);
597
+ switch (field) {
598
+ case "urn":
599
+ return this.existsByPrimary(key, opt, true);
600
+ case "id":
601
+ return this.existsBySecondary(key, opt, true);
602
+ default:
603
+ assertMessage('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'exists', queryName: opt.name });
604
+ break;
605
+ }
604
606
  }
605
607
  /** @inheritDoc */
606
- existsByPrimary(key, p1, ignoreCheck) {
607
- return __awaiter(this, void 0, void 0, function* () {
608
- const opt = this.buildOpt(p1, 'existsByPrimary');
609
- if (!ignoreCheck) {
610
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'existsByPrimary', queryName: opt.name });
611
- }
612
- if (!opt.noCache && this.cache.isConnected) {
613
- try {
614
- const found = yield this.cache.getByPrimary(key, opt);
615
- if (found) {
616
- return true;
617
- }
618
- }
619
- catch (e) {
620
- this.logger.warn(common_1.errorCommon.text(e, 'existsByPrimary', 'cache', 'getByPrimary'));
608
+ async existsByPrimary(key, p1, ignoreCheck) {
609
+ const opt = this.buildOpt(p1, 'existsByPrimary');
610
+ if (!ignoreCheck) {
611
+ assertText(key, { field: 'key', where: 'DbRepo', method: 'existsByPrimary', queryName: opt.name });
612
+ }
613
+ if (!opt.noCache && this.cache.isConnected) {
614
+ try {
615
+ const found = await this.cache.getByPrimary(key, opt);
616
+ if (found) {
617
+ return true;
621
618
  }
622
619
  }
623
- return this.$existsByPrimary(key, opt, true);
624
- });
620
+ catch (e) {
621
+ this.logger.warn(errorCommon.text(e, 'existsByPrimary', 'cache', 'getByPrimary'));
622
+ }
623
+ }
624
+ return this.$existsByPrimary(key, opt, true);
625
625
  }
626
626
  /** @inheritDoc */
627
- existsBySecondary(key, p1, ignoreCheck) {
628
- return __awaiter(this, void 0, void 0, function* () {
629
- const opt = this.buildOpt(p1, 'existsBySecondary');
630
- if (!ignoreCheck) {
631
- if (this.hasNumericId) {
632
- (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary', queryName: opt.name });
633
- }
634
- else {
635
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary', queryName: opt.name });
636
- }
627
+ async existsBySecondary(key, p1, ignoreCheck) {
628
+ const opt = this.buildOpt(p1, 'existsBySecondary');
629
+ if (!ignoreCheck) {
630
+ if (this.hasNumericId) {
631
+ assertInteger(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary', queryName: opt.name });
637
632
  }
638
- if (!this.isIdSame) {
639
- return this.existsByPrimary(this._keyToUrn(key));
633
+ else {
634
+ assertText(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary', queryName: opt.name });
640
635
  }
641
- if (!opt.noCache && this.cache.isConnected) {
642
- try {
643
- const found = yield this.cache.getBySecondary(key, opt);
644
- if (found) {
645
- return true;
646
- }
647
- }
648
- catch (e) {
649
- this.logger.warn(common_1.errorCommon.text(e, 'existsBySecondary', 'cache', 'getBySecondary'));
636
+ }
637
+ if (!this.isIdSame) {
638
+ return this.existsByPrimary(this._keyToUrn(key));
639
+ }
640
+ if (!opt.noCache && this.cache.isConnected) {
641
+ try {
642
+ const found = await this.cache.getBySecondary(key, opt);
643
+ if (found) {
644
+ return true;
650
645
  }
651
646
  }
652
- return this.$existsBySecondary(key, opt, true);
653
- });
647
+ catch (e) {
648
+ this.logger.warn(errorCommon.text(e, 'existsBySecondary', 'cache', 'getBySecondary'));
649
+ }
650
+ }
651
+ return this.$existsBySecondary(key, opt, true);
654
652
  }
655
653
  // endregion exists
656
654
  // region list
657
655
  /** @inheritDoc */
658
- list(keyLikes, p1, ignoreCheck) {
659
- return __awaiter(this, void 0, void 0, function* () {
660
- const opt = this.buildOpt(p1, 'list');
661
- if (!ignoreCheck) {
662
- (0, type_1.assertArray)(keyLikes, { field: 'keys', where: 'DbRepo', method: 'list', queryName: opt.name });
663
- }
664
- const result = this._checkKeys(keyLikes);
665
- if (result.ordered.length < 1) {
666
- return [];
656
+ async list(keyLikes, p1, ignoreCheck) {
657
+ const opt = this.buildOpt(p1, 'list');
658
+ if (!ignoreCheck) {
659
+ assertArray(keyLikes, { field: 'keys', where: 'DbRepo', method: 'list', queryName: opt.name });
660
+ }
661
+ const result = this._checkKeys(keyLikes);
662
+ if (result.ordered.length < 1) {
663
+ return [];
664
+ }
665
+ const docs = [];
666
+ let founds = [];
667
+ if (result.urns.length > 0) {
668
+ if (!opt.noCache && this.cache.isConnected) {
669
+ try {
670
+ founds = await this.cache.getByPrimaryMore(result.urns, opt);
671
+ }
672
+ catch (e) {
673
+ this.logger.warn(errorCommon.text(e, 'list', 'cache', 'getByPrimary'));
674
+ }
675
+ founds.forEach(val => {
676
+ const index = result.urns.indexOf(val.urn);
677
+ if (index >= 0) {
678
+ result.urns.splice(index, 1);
679
+ }
680
+ });
681
+ docs.push(...founds);
667
682
  }
668
- const docs = [];
669
- let founds = [];
670
683
  if (result.urns.length > 0) {
671
- if (!opt.noCache && this.cache.isConnected) {
672
- try {
673
- founds = yield this.cache.getByPrimaryMore(result.urns, opt);
684
+ founds = await this.$listByPrimary(result.urns, opt, true);
685
+ if (founds.length > 0) {
686
+ if (!opt.noCache && this.cache.isConnected) {
687
+ founds.forEach(found => this.cache.ingestDoc(found, opt)
688
+ .then()
689
+ .catch(e => this.logger.warn(errorCommon.text(e, 'list', 'cache', 'ingestDoc'))));
674
690
  }
675
- catch (e) {
676
- this.logger.warn(common_1.errorCommon.text(e, 'list', 'cache', 'getByPrimary'));
677
- }
678
- founds.forEach(val => {
679
- const index = result.urns.indexOf(val.urn);
680
- if (index >= 0) {
681
- result.urns.splice(index, 1);
682
- }
683
- });
684
691
  docs.push(...founds);
685
692
  }
686
- if (result.urns.length > 0) {
687
- founds = yield this.$listByPrimary(result.urns, opt, true);
688
- if (founds.length > 0) {
689
- if (!opt.noCache && this.cache.isConnected) {
690
- founds.forEach(found => this.cache.ingestDoc(found, opt)
691
- .then()
692
- .catch(e => this.logger.warn(common_1.errorCommon.text(e, 'list', 'cache', 'ingestDoc'))));
693
- }
694
- docs.push(...founds);
695
- }
693
+ }
694
+ }
695
+ if (result.ids.length > 0) {
696
+ if (!opt.noCache && this.cache.isConnected) {
697
+ try {
698
+ founds = await this.cache.getBySecondaryMore(result.ids, opt);
699
+ }
700
+ catch (e) {
701
+ this.logger.warn(errorCommon.text(e, 'list', 'cache', 'getBySecondary'));
696
702
  }
703
+ founds.forEach(val => {
704
+ const index = result.ids.indexOf(val.id);
705
+ if (index >= 0) {
706
+ result.ids.splice(index, 1);
707
+ }
708
+ });
709
+ docs.push(...founds);
697
710
  }
698
711
  if (result.ids.length > 0) {
699
- if (!opt.noCache && this.cache.isConnected) {
700
- try {
701
- founds = yield this.cache.getBySecondaryMore(result.ids, opt);
712
+ founds = await this.$listBySecondary(result.ids, opt, true);
713
+ if (founds.length > 0) {
714
+ if (!opt.noCache && this.cache.isConnected) {
715
+ founds.forEach(found => this.cache.ingestDoc(found, opt)
716
+ .then()
717
+ .catch(e => this.logger.warn(errorCommon.text(e, 'list', 'cache', 'ingestDoc'))));
702
718
  }
703
- catch (e) {
704
- this.logger.warn(common_1.errorCommon.text(e, 'list', 'cache', 'getBySecondary'));
705
- }
706
- founds.forEach(val => {
707
- const index = result.ids.indexOf(val.id);
708
- if (index >= 0) {
709
- result.ids.splice(index, 1);
710
- }
711
- });
712
719
  docs.push(...founds);
713
720
  }
714
- if (result.ids.length > 0) {
715
- founds = yield this.$listBySecondary(result.ids, opt, true);
716
- if (founds.length > 0) {
717
- if (!opt.noCache && this.cache.isConnected) {
718
- founds.forEach(found => this.cache.ingestDoc(found, opt)
719
- .then()
720
- .catch(e => this.logger.warn(common_1.errorCommon.text(e, 'list', 'cache', 'ingestDoc'))));
721
- }
722
- docs.push(...founds);
723
- }
724
- }
725
721
  }
726
- return result.ordered.map(o => docs.find(d => d[o[1]] === o[0]));
727
- });
722
+ }
723
+ return result.ordered.map(o => docs.find(d => d[o[1]] === o[0]));
728
724
  }
729
725
  // endregion list
730
726
  // region filter
731
727
  /** @inheritDoc */
732
- filter(query, p1) {
733
- return __awaiter(this, void 0, void 0, function* () {
734
- const opt = this.buildOpt(p1, 'filter');
735
- return this.$filter(query_1.queryParser.exec(query, [], opt.name), opt);
736
- });
728
+ async filter(query, p1) {
729
+ const opt = this.buildOpt(p1, 'filter');
730
+ return this.$filter(queryParser.exec(query, [], opt.name), opt);
737
731
  }
738
732
  // endregion filter
739
733
  // region insert
740
734
  /** @inheritDoc */
741
- insert(doc, p1, ignoreCheck) {
742
- return __awaiter(this, void 0, void 0, function* () {
743
- const opt = this.buildOpt(p1, 'insert');
744
- const param = { where: 'DbRepo', method: 'insert', queryName: opt.name };
745
- if (this.isNoInsert) {
746
- throw new error_1.DbNotSupportedError('Insert', param);
747
- }
748
- if (!ignoreCheck) {
749
- (0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
750
- if (this.hasNumericId) {
751
- (0, type_1.assertInteger)(doc.id, Object.assign({ field: 'doc.id' }, param));
752
- }
753
- else {
754
- (0, type_1.assertText)(doc.id, Object.assign({ field: 'doc.id' }, param));
755
- }
756
- (0, type_1.assertText)(doc.urn, Object.assign({ field: 'doc.urn' }, param));
757
- }
758
- if (!doc._trashId !== undefined) {
759
- delete doc._trashId;
760
- }
761
- if (this.hasCreatedAt) {
762
- doc.createdAt = this._now;
735
+ async insert(doc, p1, ignoreCheck) {
736
+ const opt = this.buildOpt(p1, 'insert');
737
+ const param = { where: 'DbRepo', method: 'insert', queryName: opt.name };
738
+ if (this.isNoInsert) {
739
+ throw new DbNotSupportedError('Insert', param);
740
+ }
741
+ if (!ignoreCheck) {
742
+ assertObject(doc, { field: 'doc', ...param });
743
+ if (this.hasNumericId) {
744
+ assertInteger(doc.id, { field: 'doc.id', ...param });
763
745
  }
764
- let userId;
765
- if (this.hasCreatedBy && this.userFetcher) {
766
- doc.createdBy = (yield this.userFetcher());
767
- userId = doc.createdBy;
746
+ else {
747
+ assertText(doc.id, { field: 'doc.id', ...param });
768
748
  }
769
- if (this.hasUpdatedAt) {
770
- doc.updatedAt = this._now;
749
+ assertText(doc.urn, { field: 'doc.urn', ...param });
750
+ }
751
+ if (!doc._trashId !== undefined) {
752
+ delete doc._trashId;
753
+ }
754
+ if (this.hasCreatedAt) {
755
+ doc.createdAt = this._now;
756
+ }
757
+ let userId;
758
+ if (this.hasCreatedBy && this.userFetcher) {
759
+ doc.createdBy = await this.userFetcher();
760
+ userId = doc.createdBy;
761
+ }
762
+ if (this.hasUpdatedAt) {
763
+ doc.updatedAt = this._now;
764
+ }
765
+ if (this.hasUpdatedBy && this.userFetcher) {
766
+ if (userId) {
767
+ doc.createdBy = userId;
771
768
  }
772
- if (this.hasUpdatedBy && this.userFetcher) {
773
- if (userId) {
774
- doc.createdBy = userId;
775
- }
776
- else {
777
- doc.createdBy = (yield this.userFetcher());
778
- }
769
+ else {
770
+ doc.createdBy = await this.userFetcher();
779
771
  }
780
- return this.$insert(doc, opt);
781
- });
772
+ }
773
+ return this.$insert(doc, opt);
782
774
  }
783
775
  /** @inheritDoc */
784
- inserts(docs, p1, ignoreCheck) {
785
- return __awaiter(this, void 0, void 0, function* () {
786
- const opt = this.buildOpt(p1, 'inserts');
787
- const param = { where: 'DbRepo', method: 'inserts', queryName: opt.name };
788
- if (this.isNoInsert) {
789
- throw new error_1.DbNotSupportedError('Insert', param);
790
- }
791
- if (!ignoreCheck) {
792
- (0, type_1.assertArray)(docs, Object.assign({ field: 'docs' }, param));
793
- }
794
- const insertedIds = [];
795
- for (const doc of docs) {
796
- insertedIds.push(yield this.insert(doc, opt, true));
797
- }
798
- return insertedIds;
799
- });
776
+ async inserts(docs, p1, ignoreCheck) {
777
+ const opt = this.buildOpt(p1, 'inserts');
778
+ const param = { where: 'DbRepo', method: 'inserts', queryName: opt.name };
779
+ if (this.isNoInsert) {
780
+ throw new DbNotSupportedError('Insert', param);
781
+ }
782
+ if (!ignoreCheck) {
783
+ assertArray(docs, { field: 'docs', ...param });
784
+ }
785
+ const insertedIds = [];
786
+ for (const doc of docs) {
787
+ insertedIds.push(await this.insert(doc, opt, true));
788
+ }
789
+ return insertedIds;
800
790
  }
801
791
  // endregion insert
802
792
  // region replace
803
793
  /** @inheritDoc */
804
- replace(doc, p1, ignoreCheck) {
805
- return __awaiter(this, void 0, void 0, function* () {
806
- const opt = this.buildOpt(p1, 'replace');
807
- if (!ignoreCheck) {
808
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'replace', queryName: opt.name });
809
- (0, type_1.assertText)(doc.urn, { field: 'doc.urn', where: 'DbRepo', method: 'replace', queryName: opt.name });
810
- if (this.hasNumericId) {
811
- (0, type_1.assertInteger)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'replace', queryName: opt.name });
812
- }
813
- else {
814
- (0, type_1.assertText)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'replace', queryName: opt.name });
815
- }
816
- }
817
- if (doc._trashId !== undefined) {
818
- delete doc._trashId;
819
- }
820
- if (this.hasUpdatedAt) {
821
- doc.updatedAt = this._now;
822
- }
823
- if (this.hasUpdatedBy && this.userFetcher) {
824
- doc.createdBy = (yield this.userFetcher());
794
+ async replace(doc, p1, ignoreCheck) {
795
+ const opt = this.buildOpt(p1, 'replace');
796
+ if (!ignoreCheck) {
797
+ assertObject(doc, { field: 'doc', where: 'DbRepo', method: 'replace', queryName: opt.name });
798
+ assertText(doc.urn, { field: 'doc.urn', where: 'DbRepo', method: 'replace', queryName: opt.name });
799
+ if (this.hasNumericId) {
800
+ assertInteger(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'replace', queryName: opt.name });
825
801
  }
826
- const replacedKey = yield this.$replace(doc, opt, true);
827
- if (!opt.noCache && this.cache.isConnected) {
828
- this.cache.clearDoc(replacedKey, opt)
829
- .then()
830
- .catch(e => this.logger.warn(common_1.errorCommon.text(e, 'replace', 'cache', 'clearDoc')));
802
+ else {
803
+ assertText(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'replace', queryName: opt.name });
831
804
  }
832
- return replacedKey;
833
- });
805
+ }
806
+ if (doc._trashId !== undefined) {
807
+ delete doc._trashId;
808
+ }
809
+ if (this.hasUpdatedAt) {
810
+ doc.updatedAt = this._now;
811
+ }
812
+ if (this.hasUpdatedBy && this.userFetcher) {
813
+ doc.createdBy = await this.userFetcher();
814
+ }
815
+ const replacedKey = await this.$replace(doc, opt, true);
816
+ if (!opt.noCache && this.cache.isConnected) {
817
+ this.cache.clearDoc(replacedKey, opt)
818
+ .then()
819
+ .catch(e => this.logger.warn(errorCommon.text(e, 'replace', 'cache', 'clearDoc')));
820
+ }
821
+ return replacedKey;
834
822
  }
835
823
  // endregion replace
836
824
  // region upsert
837
825
  /** @inheritDoc */
838
- upsert(doc, p1, ignoreCheck) {
839
- return __awaiter(this, void 0, void 0, function* () {
840
- const opt = this.buildOpt(p1, 'upsert');
841
- const param = { where: 'DbRepo', method: 'upsert', queryName: opt.name };
842
- if (this.isNoInsert || this.isNoUpdate) {
843
- throw new error_1.DbNotSupportedError('Upsert', param);
844
- }
845
- if (!ignoreCheck) {
846
- (0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
847
- (0, type_1.assertText)(doc.urn, Object.assign({ field: 'doc.urn' }, param));
848
- if (this.hasNumericId) {
849
- (0, type_1.assertInteger)(doc.id, Object.assign({ field: 'doc.id' }, param));
850
- }
851
- else {
852
- (0, type_1.assertText)(doc.id, Object.assign({ field: 'doc.id' }, param));
853
- }
854
- }
855
- if (doc._trashId !== undefined) {
856
- delete doc._trashId;
857
- }
858
- if (this.hasUpdatedAt) {
859
- doc.updatedAt = this._now;
860
- }
861
- if (this.hasUpdatedBy && this.userFetcher) {
862
- doc.createdBy = (yield this.userFetcher());
826
+ async upsert(doc, p1, ignoreCheck) {
827
+ const opt = this.buildOpt(p1, 'upsert');
828
+ const param = { where: 'DbRepo', method: 'upsert', queryName: opt.name };
829
+ if (this.isNoInsert || this.isNoUpdate) {
830
+ throw new DbNotSupportedError('Upsert', param);
831
+ }
832
+ if (!ignoreCheck) {
833
+ assertObject(doc, { field: 'doc', ...param });
834
+ assertText(doc.urn, { field: 'doc.urn', ...param });
835
+ if (this.hasNumericId) {
836
+ assertInteger(doc.id, { field: 'doc.id', ...param });
863
837
  }
864
- const createdKey = yield this.$replace(doc, opt, true);
865
- if (!opt.noCache && this.cache.isConnected) {
866
- this.cache.clearDoc(createdKey, opt)
867
- .then()
868
- .catch(e => this.logger.warn(common_1.errorCommon.text(e, 'upsert', 'cache', 'clearDoc')));
838
+ else {
839
+ assertText(doc.id, { field: 'doc.id', ...param });
869
840
  }
870
- return createdKey;
871
- });
841
+ }
842
+ if (doc._trashId !== undefined) {
843
+ delete doc._trashId;
844
+ }
845
+ if (this.hasUpdatedAt) {
846
+ doc.updatedAt = this._now;
847
+ }
848
+ if (this.hasUpdatedBy && this.userFetcher) {
849
+ doc.createdBy = await this.userFetcher();
850
+ }
851
+ const createdKey = await this.$replace(doc, opt, true);
852
+ if (!opt.noCache && this.cache.isConnected) {
853
+ this.cache.clearDoc(createdKey, opt)
854
+ .then()
855
+ .catch(e => this.logger.warn(errorCommon.text(e, 'upsert', 'cache', 'clearDoc')));
856
+ }
857
+ return createdKey;
872
858
  }
873
859
  // endregion upsert
874
860
  // region update
875
861
  /** @inheritDoc */
876
- update(k1, doc, p1, ignoreCheck) {
877
- return __awaiter(this, void 0, void 0, function* () {
878
- const opt = this.buildOpt(p1, 'update');
879
- if (!ignoreCheck) {
880
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'update', queryName: opt.name });
881
- }
882
- const [key, field] = this._checkKey(k1);
883
- switch (field) {
884
- case "urn":
885
- return this.updateByPrimary(key, doc, opt, true);
886
- case "id":
887
- return this.updateBySecondary(key, doc, opt, true);
888
- default:
889
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'update', queryName: opt.name });
890
- }
891
- });
862
+ async update(k1, doc, p1, ignoreCheck) {
863
+ const opt = this.buildOpt(p1, 'update');
864
+ if (!ignoreCheck) {
865
+ assertObject(doc, { field: 'doc', where: 'DbRepo', method: 'update', queryName: opt.name });
866
+ }
867
+ const [key, field] = this._checkKey(k1);
868
+ switch (field) {
869
+ case "urn":
870
+ return this.updateByPrimary(key, doc, opt, true);
871
+ case "id":
872
+ return this.updateBySecondary(key, doc, opt, true);
873
+ default:
874
+ assertMessage('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'update', queryName: opt.name });
875
+ }
892
876
  }
893
877
  /** @inheritDoc */
894
- updateByPrimary(key, doc, p1, ignoreCheck) {
895
- return __awaiter(this, void 0, void 0, function* () {
896
- const opt = this.buildOpt(p1, 'updateByPrimary');
897
- const param = { where: 'DbRepo', method: 'updateByPrimary', queryName: opt.name };
898
- if (this.isNoUpdate) {
899
- throw new error_1.DbNotSupportedError('Update', param);
900
- }
901
- if (!ignoreCheck) {
902
- (0, type_1.assertText)(key, Object.assign({ field: 'key' }, param));
903
- (0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
904
- }
905
- if (doc._trashId !== undefined) {
906
- delete doc._trashId;
907
- }
908
- this._props.forbiddenSet.forEach(f => {
909
- if (doc[f] !== undefined) {
910
- delete doc[f];
911
- }
912
- });
913
- if (this.hasUpdatedAt) {
914
- doc.updatedAt = this._now;
915
- }
916
- if (this.hasUpdatedBy && this.userFetcher) {
917
- doc.createdBy = (yield this.userFetcher());
918
- }
919
- const updatedKey = yield this.$updateByPrimary(key, doc, opt, true);
920
- if (updatedKey && !opt.noCache && this.cache.isConnected) {
921
- this.cache.clearDoc(updatedKey, opt)
922
- .then()
923
- .catch(e => this.logger.warn(common_1.errorCommon.text(e, 'updateByPrimary', 'cache', 'clearDoc')));
878
+ async updateByPrimary(key, doc, p1, ignoreCheck) {
879
+ const opt = this.buildOpt(p1, 'updateByPrimary');
880
+ const param = { where: 'DbRepo', method: 'updateByPrimary', queryName: opt.name };
881
+ if (this.isNoUpdate) {
882
+ throw new DbNotSupportedError('Update', param);
883
+ }
884
+ if (!ignoreCheck) {
885
+ assertText(key, { field: 'key', ...param });
886
+ assertObject(doc, { field: 'doc', ...param });
887
+ }
888
+ if (doc._trashId !== undefined) {
889
+ delete doc._trashId;
890
+ }
891
+ this._props.forbiddenSet.forEach(f => {
892
+ if (doc[f] !== undefined) {
893
+ delete doc[f];
924
894
  }
925
- return updatedKey;
926
895
  });
896
+ if (this.hasUpdatedAt) {
897
+ doc.updatedAt = this._now;
898
+ }
899
+ if (this.hasUpdatedBy && this.userFetcher) {
900
+ doc.createdBy = await this.userFetcher();
901
+ }
902
+ const updatedKey = await this.$updateByPrimary(key, doc, opt, true);
903
+ if (updatedKey && !opt.noCache && this.cache.isConnected) {
904
+ this.cache.clearDoc(updatedKey, opt)
905
+ .then()
906
+ .catch(e => this.logger.warn(errorCommon.text(e, 'updateByPrimary', 'cache', 'clearDoc')));
907
+ }
908
+ return updatedKey;
927
909
  }
928
910
  /** @inheritDoc */
929
- updateBySecondary(key, doc, p1, ignoreCheck) {
930
- return __awaiter(this, void 0, void 0, function* () {
931
- const opt = this.buildOpt(p1, 'updateBySecondary');
932
- const param = { where: 'DbRepo', method: 'updateBySecondary', queryName: opt.name };
933
- if (this.isNoUpdate) {
934
- throw new error_1.DbNotSupportedError('Update', param);
935
- }
936
- if (!ignoreCheck) {
937
- if (this.hasNumericId) {
938
- (0, type_1.assertInteger)(key, Object.assign({ field: 'key' }, param));
939
- }
940
- else {
941
- (0, type_1.assertText)(key, Object.assign({ field: 'key' }, param));
942
- }
943
- (0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
944
- }
945
- if (this.isIdSame) {
946
- return this.updateBySecondary(this._keyToUrn(key), doc, opt, true);
947
- }
948
- if (doc._trashId !== undefined) {
949
- delete doc._trashId;
950
- }
951
- this._props.forbiddenSet.forEach(f => {
952
- if (doc[f] !== undefined) {
953
- delete doc[f];
954
- }
955
- });
956
- if (this.hasUpdatedAt) {
957
- doc.updatedAt = this._now;
911
+ async updateBySecondary(key, doc, p1, ignoreCheck) {
912
+ const opt = this.buildOpt(p1, 'updateBySecondary');
913
+ const param = { where: 'DbRepo', method: 'updateBySecondary', queryName: opt.name };
914
+ if (this.isNoUpdate) {
915
+ throw new DbNotSupportedError('Update', param);
916
+ }
917
+ if (!ignoreCheck) {
918
+ if (this.hasNumericId) {
919
+ assertInteger(key, { field: 'key', ...param });
958
920
  }
959
- if (this.hasUpdatedBy && this.userFetcher) {
960
- doc.createdBy = (yield this.userFetcher());
921
+ else {
922
+ assertText(key, { field: 'key', ...param });
961
923
  }
962
- const updatedKey = yield this.$updateBySecondary(key, doc, opt, true);
963
- if (updatedKey && !opt.noCache && this.cache.isConnected) {
964
- this.cache.clearDoc(updatedKey, opt)
965
- .then()
966
- .catch(e => this.logger.warn(common_1.errorCommon.text(e, 'updateBySecondary', 'cache', 'clearDoc')));
924
+ assertObject(doc, { field: 'doc', ...param });
925
+ }
926
+ if (this.isIdSame) {
927
+ return this.updateBySecondary(this._keyToUrn(key), doc, opt, true);
928
+ }
929
+ if (doc._trashId !== undefined) {
930
+ delete doc._trashId;
931
+ }
932
+ this._props.forbiddenSet.forEach(f => {
933
+ if (doc[f] !== undefined) {
934
+ delete doc[f];
967
935
  }
968
- return updatedKey;
969
936
  });
937
+ if (this.hasUpdatedAt) {
938
+ doc.updatedAt = this._now;
939
+ }
940
+ if (this.hasUpdatedBy && this.userFetcher) {
941
+ doc.createdBy = await this.userFetcher();
942
+ }
943
+ const updatedKey = await this.$updateBySecondary(key, doc, opt, true);
944
+ if (updatedKey && !opt.noCache && this.cache.isConnected) {
945
+ this.cache.clearDoc(updatedKey, opt)
946
+ .then()
947
+ .catch(e => this.logger.warn(errorCommon.text(e, 'updateBySecondary', 'cache', 'clearDoc')));
948
+ }
949
+ return updatedKey;
970
950
  }
971
951
  // endregion update
972
952
  // region set
973
953
  /** @inheritDoc */
974
- set(key, doc, p1, ignoreCheck) {
975
- return __awaiter(this, void 0, void 0, function* () {
976
- const opt = this.buildOpt(p1, 'set');
977
- if (!ignoreCheck) {
978
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'set', queryName: opt.name });
979
- }
980
- for (const [f, v] of Object.entries(doc)) {
981
- if (v === undefined) { // dont allow undefined
982
- delete doc[f];
983
- }
954
+ async set(key, doc, p1, ignoreCheck) {
955
+ const opt = this.buildOpt(p1, 'set');
956
+ if (!ignoreCheck) {
957
+ assertObject(doc, { field: 'doc', where: 'DbRepo', method: 'set', queryName: opt.name });
958
+ }
959
+ for (const [f, v] of Object.entries(doc)) {
960
+ if (v === undefined) { // dont allow undefined
961
+ delete doc[f];
984
962
  }
985
- return this.update(key, doc, opt, false);
986
- });
963
+ }
964
+ return this.update(key, doc, opt, false);
987
965
  }
988
966
  /** @inheritDoc */
989
- unset(key, fields, p1, ignoreCheck) {
990
- return __awaiter(this, void 0, void 0, function* () {
991
- const opt = this.buildOpt(p1, 'unset');
992
- if (!ignoreCheck) {
993
- (0, type_1.assertArray)(fields, { field: 'fields', where: 'DbRepo', method: 'unset', queryName: opt.name });
994
- fields.forEach((field, index) => {
995
- (0, type_1.assertText)(field, { field: field, where: 'DbRepo', method: 'unset', index, queryName: opt.name });
996
- });
997
- }
998
- const doc = {};
999
- fields.forEach(f => {
1000
- if (!this._props.forbiddenSet.includes(f)) {
1001
- doc[f] = undefined;
1002
- }
967
+ async unset(key, fields, p1, ignoreCheck) {
968
+ const opt = this.buildOpt(p1, 'unset');
969
+ if (!ignoreCheck) {
970
+ assertArray(fields, { field: 'fields', where: 'DbRepo', method: 'unset', queryName: opt.name });
971
+ fields.forEach((field, index) => {
972
+ assertText(field, { field: field, where: 'DbRepo', method: 'unset', index, queryName: opt.name });
1003
973
  });
1004
- return this.update(key, doc, opt, false);
974
+ }
975
+ const doc = {};
976
+ fields.forEach(f => {
977
+ if (!this._props.forbiddenSet.includes(f)) {
978
+ doc[f] = undefined;
979
+ }
1005
980
  });
981
+ return this.update(key, doc, opt, false);
1006
982
  }
1007
983
  // endregion set
1008
984
  // region remove
1009
985
  /** @inheritDoc */
1010
- remove(keyLike, p1) {
1011
- return __awaiter(this, void 0, void 0, function* () {
1012
- const opt = this.buildOpt(p1, 'remove');
1013
- const [key, field] = this._checkKey(keyLike);
1014
- switch (field) {
1015
- case "urn":
1016
- return this.removeByPrimary(key, opt, true);
1017
- case "id":
1018
- return this.removeBySecondary(key, opt, true);
1019
- default:
1020
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'remove', queryName: opt.name });
1021
- }
1022
- });
986
+ async remove(keyLike, p1) {
987
+ const opt = this.buildOpt(p1, 'remove');
988
+ const [key, field] = this._checkKey(keyLike);
989
+ switch (field) {
990
+ case "urn":
991
+ return this.removeByPrimary(key, opt, true);
992
+ case "id":
993
+ return this.removeBySecondary(key, opt, true);
994
+ default:
995
+ assertMessage('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'remove', queryName: opt.name });
996
+ }
1023
997
  }
1024
998
  /** @inheritDoc */
1025
- removeByPrimary(key, p1, ignoreCheck) {
1026
- return __awaiter(this, void 0, void 0, function* () {
1027
- const opt = this.buildOpt(p1, 'removeByPrimary');
1028
- const param = { where: 'DbRepo', method: 'removeByPrimary', queryName: opt.name };
1029
- if (this.isNoRemove) {
1030
- throw new error_1.DbNotSupportedError('Remove', param);
1031
- }
1032
- if (!ignoreCheck) {
1033
- (0, type_1.assertText)(key, Object.assign({ field: 'key' }, param));
1034
- }
1035
- const removedKey = yield this.$removeByPrimary(key, opt, true);
1036
- if (removedKey && !opt.noCache && this.cache.isConnected) {
1037
- this.cache.clearDoc(removedKey, opt)
1038
- .then()
1039
- .catch(e => this.logger.warn(common_1.errorCommon.text(e, 'removeByPrimary', 'cache', 'clearDoc')));
1040
- }
1041
- return removedKey;
1042
- });
999
+ async removeByPrimary(key, p1, ignoreCheck) {
1000
+ const opt = this.buildOpt(p1, 'removeByPrimary');
1001
+ const param = { where: 'DbRepo', method: 'removeByPrimary', queryName: opt.name };
1002
+ if (this.isNoRemove) {
1003
+ throw new DbNotSupportedError('Remove', param);
1004
+ }
1005
+ if (!ignoreCheck) {
1006
+ assertText(key, { field: 'key', ...param });
1007
+ }
1008
+ const removedKey = await this.$removeByPrimary(key, opt, true);
1009
+ if (removedKey && !opt.noCache && this.cache.isConnected) {
1010
+ this.cache.clearDoc(removedKey, opt)
1011
+ .then()
1012
+ .catch(e => this.logger.warn(errorCommon.text(e, 'removeByPrimary', 'cache', 'clearDoc')));
1013
+ }
1014
+ return removedKey;
1043
1015
  }
1044
1016
  /** @inheritDoc */
1045
- removeBySecondary(key, p1, ignoreCheck) {
1046
- return __awaiter(this, void 0, void 0, function* () {
1047
- const opt = this.buildOpt(p1, 'removeBySecondary');
1048
- const param = { where: 'DbRepo', method: 'removeByPrimary', queryName: opt.name };
1049
- if (this.isNoRemove) {
1050
- throw new error_1.DbNotSupportedError('Remove', param);
1051
- }
1052
- if (this.hasNumericId) {
1053
- (0, type_1.assertInteger)(key, { field: 'key', param });
1054
- }
1055
- else {
1056
- (0, type_1.assertText)(key, { field: 'key', param });
1057
- }
1058
- if (this.isIdSame) {
1059
- return this.removeByPrimary(this._keyToUrn(key), opt, ignoreCheck);
1060
- }
1061
- const removedKey = yield this.$removeBySecondary(key, opt, true);
1062
- if (removedKey && !opt.noCache && this.cache.isConnected) {
1063
- this.cache.clearDoc(removedKey, opt)
1064
- .then()
1065
- .catch(e => this.logger.warn(common_1.errorCommon.text(e, 'removeBySecondary', 'cache', 'clearDoc')));
1066
- }
1067
- return removedKey;
1068
- });
1017
+ async removeBySecondary(key, p1, ignoreCheck) {
1018
+ const opt = this.buildOpt(p1, 'removeBySecondary');
1019
+ const param = { where: 'DbRepo', method: 'removeByPrimary', queryName: opt.name };
1020
+ if (this.isNoRemove) {
1021
+ throw new DbNotSupportedError('Remove', param);
1022
+ }
1023
+ if (this.hasNumericId) {
1024
+ assertInteger(key, { field: 'key', param });
1025
+ }
1026
+ else {
1027
+ assertText(key, { field: 'key', param });
1028
+ }
1029
+ if (this.isIdSame) {
1030
+ return this.removeByPrimary(this._keyToUrn(key), opt, ignoreCheck);
1031
+ }
1032
+ const removedKey = await this.$removeBySecondary(key, opt, true);
1033
+ if (removedKey && !opt.noCache && this.cache.isConnected) {
1034
+ this.cache.clearDoc(removedKey, opt)
1035
+ .then()
1036
+ .catch(e => this.logger.warn(errorCommon.text(e, 'removeBySecondary', 'cache', 'clearDoc')));
1037
+ }
1038
+ return removedKey;
1069
1039
  }
1070
1040
  // endregion remove
1071
1041
  // region trash
1072
1042
  /** @inheritDoc */
1073
- trash(keyLike, p1) {
1074
- return __awaiter(this, void 0, void 0, function* () {
1075
- const opt = this.buildOpt(p1, 'trash');
1076
- const [key, field] = this._checkKey(keyLike);
1077
- switch (field) {
1078
- case "urn":
1079
- return this.trashByPrimary(key, opt, true);
1080
- case "id":
1081
- return this.trashBySecondary(key, opt, true);
1082
- default:
1083
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'trash', queryName: opt.name });
1084
- }
1085
- });
1043
+ async trash(keyLike, p1) {
1044
+ const opt = this.buildOpt(p1, 'trash');
1045
+ const [key, field] = this._checkKey(keyLike);
1046
+ switch (field) {
1047
+ case "urn":
1048
+ return this.trashByPrimary(key, opt, true);
1049
+ case "id":
1050
+ return this.trashBySecondary(key, opt, true);
1051
+ default:
1052
+ assertMessage('Invalid key', { field: 'key', value: keyLike, where: 'DbRepo', method: 'trash', queryName: opt.name });
1053
+ }
1086
1054
  }
1087
1055
  /** @inheritDoc */
1088
- trashByPrimary(key, p1, ignoreCheck) {
1089
- return __awaiter(this, void 0, void 0, function* () {
1090
- const opt = this.buildOpt(p1, 'trashByPrimary');
1091
- const param = { where: 'DbRepo', method: 'trashByPrimary', queryName: opt.name };
1092
- if (!this.hasSoftDelete) {
1093
- throw new error_1.DbNotSupportedError('Soft Delete', param);
1094
- }
1095
- if (this.isNoTrash) {
1096
- throw new error_1.DbNotSupportedError('Trash', param);
1097
- }
1098
- if (!ignoreCheck) {
1099
- (0, type_1.assertText)(key, Object.assign({ field: 'urn' }, param));
1100
- }
1101
- if (!(0, common_1.isText)(opt.trashId)) {
1102
- opt.trashId = (0, node_crypto_1.randomUUID)();
1103
- }
1104
- const trashedKey = yield this.$trashByPrimary(key, opt, true);
1105
- if (trashedKey && !opt.noCache && this.cache.isConnected) {
1106
- this.cache.clearDoc(trashedKey, opt)
1107
- .then()
1108
- .catch(e => this.logger.warn(common_1.errorCommon.text(e, 'trashByPrimary', 'cache', 'clearDoc')));
1109
- }
1110
- return trashedKey;
1111
- });
1056
+ async trashByPrimary(key, p1, ignoreCheck) {
1057
+ const opt = this.buildOpt(p1, 'trashByPrimary');
1058
+ const param = { where: 'DbRepo', method: 'trashByPrimary', queryName: opt.name };
1059
+ if (!this.hasSoftDelete) {
1060
+ throw new DbNotSupportedError('Soft Delete', param);
1061
+ }
1062
+ if (this.isNoTrash) {
1063
+ throw new DbNotSupportedError('Trash', param);
1064
+ }
1065
+ if (!ignoreCheck) {
1066
+ assertText(key, { field: 'urn', ...param });
1067
+ }
1068
+ if (!isText(opt.trashId)) {
1069
+ opt.trashId = randomUUID();
1070
+ }
1071
+ const trashedKey = await this.$trashByPrimary(key, opt, true);
1072
+ if (trashedKey && !opt.noCache && this.cache.isConnected) {
1073
+ this.cache.clearDoc(trashedKey, opt)
1074
+ .then()
1075
+ .catch(e => this.logger.warn(errorCommon.text(e, 'trashByPrimary', 'cache', 'clearDoc')));
1076
+ }
1077
+ return trashedKey;
1112
1078
  }
1113
1079
  /** @inheritDoc */
1114
- trashBySecondary(key, p1, ignoreCheck) {
1115
- return __awaiter(this, void 0, void 0, function* () {
1116
- const opt = this.buildOpt(p1, 'trashBySecondary');
1117
- const param = { where: 'DbRepo', method: 'trashBySecondary', queryName: opt.name };
1118
- if (!this.hasSoftDelete) {
1119
- throw new error_1.DbNotSupportedError('Soft Delete', param);
1120
- }
1121
- if (this.isNoTrash) {
1122
- throw new error_1.DbNotSupportedError('Trash', param);
1123
- }
1124
- if (this.hasNumericId) {
1125
- (0, type_1.assertInteger)(key, Object.assign({ field: 'key' }, param));
1126
- }
1127
- else {
1128
- (0, type_1.assertText)(key, Object.assign({ field: 'key' }, param));
1129
- }
1130
- if (this.isIdSame) {
1131
- return this.trashByPrimary(this._keyToUrn(key), opt, ignoreCheck);
1132
- }
1133
- if (!(0, common_1.isText)(opt.trashId)) {
1134
- opt.trashId = (0, node_crypto_1.randomUUID)();
1135
- }
1136
- const trashedKey = yield this.$trashBySecondary(key, opt, true);
1137
- if (trashedKey && !opt.noCache && this.cache.isConnected) {
1138
- this.cache.clearDoc(trashedKey, opt)
1139
- .then()
1140
- .catch(e => this.logger.warn(common_1.errorCommon.text(e, 'trashBySecondary', 'cache', 'clearDoc')));
1141
- }
1142
- return trashedKey;
1143
- });
1080
+ async trashBySecondary(key, p1, ignoreCheck) {
1081
+ const opt = this.buildOpt(p1, 'trashBySecondary');
1082
+ const param = { where: 'DbRepo', method: 'trashBySecondary', queryName: opt.name };
1083
+ if (!this.hasSoftDelete) {
1084
+ throw new DbNotSupportedError('Soft Delete', param);
1085
+ }
1086
+ if (this.isNoTrash) {
1087
+ throw new DbNotSupportedError('Trash', param);
1088
+ }
1089
+ if (this.hasNumericId) {
1090
+ assertInteger(key, { field: 'key', ...param });
1091
+ }
1092
+ else {
1093
+ assertText(key, { field: 'key', ...param });
1094
+ }
1095
+ if (this.isIdSame) {
1096
+ return this.trashByPrimary(this._keyToUrn(key), opt, ignoreCheck);
1097
+ }
1098
+ if (!isText(opt.trashId)) {
1099
+ opt.trashId = randomUUID();
1100
+ }
1101
+ const trashedKey = await this.$trashBySecondary(key, opt, true);
1102
+ if (trashedKey && !opt.noCache && this.cache.isConnected) {
1103
+ this.cache.clearDoc(trashedKey, opt)
1104
+ .then()
1105
+ .catch(e => this.logger.warn(errorCommon.text(e, 'trashBySecondary', 'cache', 'clearDoc')));
1106
+ }
1107
+ return trashedKey;
1144
1108
  }
1145
1109
  // endregion trash
1146
1110
  // region dim
1147
1111
  /** @inheritDoc */
1148
- $toDim(doc, dim) {
1149
- return __awaiter(this, void 0, void 0, function* () {
1150
- if (!doc) {
1151
- return undefined;
1152
- }
1153
- switch (dim) {
1154
- case "view":
1155
- return doc;
1156
- case "pair":
1157
- return { id: doc.id, name: doc.name };
1158
- default: // portion
1159
- if (this.isIdSame) {
1160
- return { id: doc.id };
1161
- }
1162
- else {
1163
- return { id: doc.id, urn: doc.urn };
1164
- }
1165
- }
1166
- });
1112
+ async $toDim(doc, dim) {
1113
+ if (!doc) {
1114
+ return undefined;
1115
+ }
1116
+ switch (dim) {
1117
+ case "view":
1118
+ return await this.toView(doc);
1119
+ case "pair":
1120
+ return await this.toPair(doc);
1121
+ case "portion":
1122
+ return await this.toPortion(doc);
1123
+ default: // portion
1124
+ if (this.isIdSame) {
1125
+ return { id: doc.id };
1126
+ }
1127
+ else {
1128
+ return { id: doc.id, urn: doc.urn };
1129
+ }
1130
+ }
1167
1131
  }
1168
1132
  /** @inheritDoc */
1169
- getDim(key, dim) {
1170
- return __awaiter(this, void 0, void 0, function* () {
1171
- const doc = yield this.get(key, { name: `getDim/${dim !== null && dim !== void 0 ? dim : 'def'}` });
1172
- return doc ? this.$toDim(doc, dim) : undefined;
1173
- });
1133
+ async getDim(key, dim) {
1134
+ const doc = await this.get(key, { name: `getDim/${dim ?? 'def'}` });
1135
+ return doc ? this.$toDim(doc, dim) : undefined;
1174
1136
  }
1175
1137
  /** @inheritDoc */
1176
- listDims(keys, dim, ignoreCheck) {
1177
- return __awaiter(this, void 0, void 0, function* () {
1178
- const docs = yield this.list(keys, { name: `listDims/${dim !== null && dim !== void 0 ? dim : 'def'}` }, ignoreCheck);
1179
- if (docs.length < 1) {
1180
- return [];
1181
- }
1182
- const items = [];
1183
- for (const doc of docs) {
1184
- items.push(yield this.$toDim(doc, dim));
1185
- }
1186
- return items;
1187
- });
1138
+ async listDims(keys, dim, ignoreCheck) {
1139
+ const docs = await this.list(keys, { name: `listDims/${dim ?? 'def'}` }, ignoreCheck);
1140
+ if (docs.length < 1) {
1141
+ return [];
1142
+ }
1143
+ const items = [];
1144
+ for (const doc of docs) {
1145
+ items.push(await this.$toDim(doc, dim));
1146
+ }
1147
+ return items;
1188
1148
  }
1189
1149
  // endregion dim
1190
1150
  // region pair
1191
- toPair(doc) {
1192
- return __awaiter(this, void 0, void 0, function* () {
1193
- return this.$toDim(doc, 'pair');
1194
- });
1151
+ async toPair(doc) {
1152
+ return { id: doc.id, name: doc.name ?? doc.id };
1195
1153
  }
1196
1154
  /** @inheritDoc */
1197
- getPair(key) {
1198
- return __awaiter(this, void 0, void 0, function* () {
1199
- return this.getDim(key, 'pair');
1200
- });
1155
+ async getPair(key) {
1156
+ const doc = await this.get(key, { name: `getPair` });
1157
+ return this.toPair(doc);
1201
1158
  }
1202
1159
  /** @inheritDoc */
1203
- listPairs(keys, ignoreCheck) {
1204
- return __awaiter(this, void 0, void 0, function* () {
1205
- return this.listDims(keys, 'pair', ignoreCheck);
1206
- });
1160
+ async listPairs(keys, ignoreCheck) {
1161
+ const docs = await this.list(keys, { name: `listPairs` }, ignoreCheck);
1162
+ if (docs.length < 1) {
1163
+ return [];
1164
+ }
1165
+ const items = [];
1166
+ for (const doc of docs) {
1167
+ items.push(await this.toPair(doc));
1168
+ }
1169
+ return items;
1207
1170
  }
1208
1171
  // endregion pair
1209
1172
  // region view
1210
- toView(doc) {
1211
- return __awaiter(this, void 0, void 0, function* () {
1212
- return this.$toDim(doc, 'view');
1213
- });
1173
+ async toView(doc) {
1174
+ return doc;
1214
1175
  }
1215
1176
  /** @inheritDoc */
1216
- getView(key) {
1217
- return __awaiter(this, void 0, void 0, function* () {
1218
- return this.getDim(key, 'view');
1219
- });
1177
+ async getView(key) {
1178
+ const doc = await this.get(key, { name: `getView` });
1179
+ return this.toView(doc);
1220
1180
  }
1221
1181
  /** @inheritDoc */
1222
- listViews(keys, ignoreCheck) {
1223
- return __awaiter(this, void 0, void 0, function* () {
1224
- return this.listDims(keys, 'view', ignoreCheck);
1225
- });
1182
+ async listViews(keys, ignoreCheck) {
1183
+ const docs = await this.list(keys, { name: `listViews` }, ignoreCheck);
1184
+ if (docs.length < 1) {
1185
+ return [];
1186
+ }
1187
+ const items = [];
1188
+ for (const doc of docs) {
1189
+ items.push(await this.toView(doc));
1190
+ }
1191
+ return items;
1226
1192
  }
1227
1193
  // endregion view
1228
1194
  // region portion
1229
1195
  /** @inheritDoc */
1230
- toPortion(doc) {
1231
- return __awaiter(this, void 0, void 0, function* () {
1232
- return this.$toDim(doc, 'portion');
1233
- });
1196
+ async toPortion(doc) {
1197
+ if (this.isIdSame) {
1198
+ return { id: doc.id };
1199
+ }
1200
+ else {
1201
+ return { id: doc.id, urn: doc.urn };
1202
+ }
1234
1203
  }
1235
1204
  /** @inheritDoc */
1236
- getPortion(key) {
1237
- return __awaiter(this, void 0, void 0, function* () {
1238
- return this.getDim(key, 'portion');
1239
- });
1205
+ async getPortion(key) {
1206
+ if (this.isIdSame) {
1207
+ const [identifier, field] = this._checkKey(key);
1208
+ if (field === 'id') {
1209
+ return { id: identifier };
1210
+ }
1211
+ }
1212
+ const doc = await this.get(key, { name: `getPortion` });
1213
+ return this.toPortion(doc);
1240
1214
  }
1241
1215
  /** @inheritDoc */
1242
- listPortions(keys, ignoreCheck) {
1243
- return __awaiter(this, void 0, void 0, function* () {
1244
- return this.listDims(keys, 'portion', ignoreCheck);
1245
- });
1216
+ async listPortions(keys, ignoreCheck) {
1217
+ if (this.isIdSame) {
1218
+ const idResult = this._checkKeys(keys);
1219
+ if (!idResult?.urns?.length) {
1220
+ if (Array.isArray(idResult?.ids)) {
1221
+ return idResult.ids.map(id => {
1222
+ return { id };
1223
+ });
1224
+ }
1225
+ return [];
1226
+ }
1227
+ }
1228
+ const docs = await this.list(keys, { name: `listPortions` }, ignoreCheck);
1229
+ if (docs.length < 1) {
1230
+ return [];
1231
+ }
1232
+ const items = [];
1233
+ for (const doc of docs) {
1234
+ items.push(await this.toPortion(doc));
1235
+ }
1236
+ return items;
1246
1237
  }
1247
1238
  }
1248
- exports.DbRepo = DbRepo;