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