@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.
- package/README.md +2 -0
- package/dist/connection/db.connection.d.ts +1 -1
- package/dist/connection/db.connection.js +41 -46
- package/dist/connection/index.d.ts +2 -2
- package/dist/connection/index.js +2 -18
- package/dist/connection/index.types.js +1 -2
- package/dist/error/db-execute.error.d.ts +1 -1
- package/dist/error/db-execute.error.js +3 -7
- package/dist/error/db-invalid-value.error.d.ts +1 -1
- package/dist/error/db-invalid-value.error.js +3 -7
- package/dist/error/db-not-supported.error.d.ts +1 -1
- package/dist/error/db-not-supported.error.js +3 -7
- package/dist/error/db.error.js +2 -6
- package/dist/error/index.d.ts +4 -4
- package/dist/error/index.js +4 -20
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -20
- package/dist/line/db-lines.impl.d.ts +1 -1
- package/dist/line/db-lines.impl.js +7 -10
- package/dist/line/index.d.ts +2 -2
- package/dist/line/index.js +2 -18
- package/dist/line/index.types.js +1 -2
- package/dist/repo/db.repo.d.ts +5 -3
- package/dist/repo/db.repo.js +655 -665
- package/dist/repo/index.d.ts +2 -2
- package/dist/repo/index.js +2 -18
- package/dist/repo/index.types.d.ts +20 -2
- package/dist/repo/index.types.js +1 -2
- package/package.json +7 -8
package/dist/repo/db.repo.js
CHANGED
|
@@ -1,21 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
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 (
|
|
58
|
-
this._opt =
|
|
56
|
+
if (isBareObject(opt)) {
|
|
57
|
+
this._opt = { ...this._opt, ...opt };
|
|
59
58
|
}
|
|
60
59
|
this._checkOptions();
|
|
61
|
-
this._props =
|
|
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
|
|
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
|
|
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) =>
|
|
118
|
-
return (
|
|
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_' +
|
|
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
|
-
|
|
181
|
-
|
|
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 =
|
|
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
|
-
|
|
418
|
-
const
|
|
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
|
|
420
|
+
if (opt?.silent) {
|
|
424
421
|
if (size < 100) {
|
|
425
|
-
this.logger.warn(
|
|
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(
|
|
427
|
+
this.logger.error(errorCommon.text(err, (opt.name ?? 'Database error'), size));
|
|
431
428
|
}
|
|
432
|
-
if (err instanceof
|
|
429
|
+
if (err instanceof DbError) {
|
|
433
430
|
throw err;
|
|
434
431
|
}
|
|
435
432
|
else {
|
|
436
|
-
throw
|
|
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 } :
|
|
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
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
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
|
-
|
|
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
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
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
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
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
|
-
|
|
531
|
-
|
|
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
|
-
|
|
543
|
-
|
|
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
|
-
|
|
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
|
-
|
|
554
|
-
|
|
555
|
-
if (
|
|
556
|
-
|
|
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
|
-
|
|
564
|
-
|
|
564
|
+
else {
|
|
565
|
+
assertText(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary', queryName: opt.name });
|
|
565
566
|
}
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
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
|
-
|
|
579
|
-
|
|
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
|
-
|
|
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
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
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
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
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
|
-
|
|
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
|
-
|
|
629
|
-
|
|
630
|
-
if (
|
|
631
|
-
|
|
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
|
-
|
|
639
|
-
|
|
633
|
+
else {
|
|
634
|
+
assertText(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary', queryName: opt.name });
|
|
640
635
|
}
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
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
|
-
|
|
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
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
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
|
-
|
|
672
|
-
|
|
673
|
-
|
|
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
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
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
|
-
|
|
700
|
-
|
|
701
|
-
|
|
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
|
-
|
|
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
|
-
|
|
734
|
-
|
|
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
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
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
|
-
|
|
765
|
-
|
|
766
|
-
doc.createdBy = (yield this.userFetcher());
|
|
767
|
-
userId = doc.createdBy;
|
|
746
|
+
else {
|
|
747
|
+
assertText(doc.id, { field: 'doc.id', ...param });
|
|
768
748
|
}
|
|
769
|
-
|
|
770
|
-
|
|
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
|
-
|
|
773
|
-
|
|
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
|
-
|
|
781
|
-
|
|
772
|
+
}
|
|
773
|
+
return this.$insert(doc, opt);
|
|
782
774
|
}
|
|
783
775
|
/** @inheritDoc */
|
|
784
|
-
inserts(docs, p1, ignoreCheck) {
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
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
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
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
|
-
|
|
827
|
-
|
|
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
|
-
|
|
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
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
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
|
-
|
|
865
|
-
|
|
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
|
-
|
|
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
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
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
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
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
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
if (
|
|
937
|
-
|
|
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
|
-
|
|
960
|
-
|
|
921
|
+
else {
|
|
922
|
+
assertText(key, { field: 'key', ...param });
|
|
961
923
|
}
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
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
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
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
|
-
|
|
986
|
-
|
|
963
|
+
}
|
|
964
|
+
return this.update(key, doc, opt, false);
|
|
987
965
|
}
|
|
988
966
|
/** @inheritDoc */
|
|
989
|
-
unset(key, fields, p1, ignoreCheck) {
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
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
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
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
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
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
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
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
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
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
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
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
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
}
|
|
1162
|
-
|
|
1163
|
-
|
|
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
|
-
|
|
1171
|
-
|
|
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
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
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
|
|
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
|
-
|
|
1199
|
-
|
|
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
|
-
|
|
1205
|
-
|
|
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
|
|
1212
|
-
return this.$toDim(doc, 'view');
|
|
1213
|
-
});
|
|
1173
|
+
async toView(doc) {
|
|
1174
|
+
return doc;
|
|
1214
1175
|
}
|
|
1215
1176
|
/** @inheritDoc */
|
|
1216
|
-
getView(key) {
|
|
1217
|
-
|
|
1218
|
-
|
|
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
|
-
|
|
1224
|
-
|
|
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
|
-
|
|
1232
|
-
return
|
|
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
|
-
|
|
1238
|
-
|
|
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
|
-
|
|
1244
|
-
|
|
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;
|