@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.
- 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 +654 -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 +3 -3
package/dist/repo/db.repo.js
CHANGED
|
@@ -1,21 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
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 (
|
|
58
|
-
this._opt =
|
|
55
|
+
if (isBareObject(opt)) {
|
|
56
|
+
this._opt = { ...this._opt, ...opt };
|
|
59
57
|
}
|
|
60
58
|
this._checkOptions();
|
|
61
|
-
this._props =
|
|
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
|
|
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
|
|
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) =>
|
|
118
|
-
return (
|
|
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_' +
|
|
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
|
-
|
|
181
|
-
|
|
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 =
|
|
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
|
-
|
|
418
|
-
const
|
|
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
|
|
419
|
+
if (opt?.silent) {
|
|
424
420
|
if (size < 100) {
|
|
425
|
-
this.logger.warn(
|
|
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(
|
|
426
|
+
this.logger.error(errorCommon.text(err, (opt.name ?? 'Database error'), size));
|
|
431
427
|
}
|
|
432
|
-
if (err instanceof
|
|
428
|
+
if (err instanceof DbError) {
|
|
433
429
|
throw err;
|
|
434
430
|
}
|
|
435
431
|
else {
|
|
436
|
-
throw
|
|
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 } :
|
|
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
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
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
|
-
|
|
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
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
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
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
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
|
-
|
|
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
|
-
}
|
|
541
|
+
catch (e) {
|
|
542
|
+
this.logger.warn(errorCommon.text(e, 'getByPrimary', 'cache', 'getByPrimary'));
|
|
541
543
|
}
|
|
542
|
-
|
|
543
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
}
|
|
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
|
-
|
|
564
|
-
|
|
563
|
+
else {
|
|
564
|
+
assertText(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary', queryName: opt.name });
|
|
565
565
|
}
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
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
|
-
|
|
579
|
-
|
|
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
|
-
|
|
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
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
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
|
-
|
|
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'));
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
}
|
|
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
|
-
|
|
639
|
-
|
|
632
|
+
else {
|
|
633
|
+
assertText(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary', queryName: opt.name });
|
|
640
634
|
}
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
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
|
-
|
|
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
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
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
|
-
|
|
672
|
-
|
|
673
|
-
|
|
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
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
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
|
-
|
|
700
|
-
|
|
701
|
-
|
|
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
|
-
|
|
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
|
-
|
|
734
|
-
|
|
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
|
-
|
|
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;
|
|
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
|
-
|
|
765
|
-
|
|
766
|
-
doc.createdBy = (yield this.userFetcher());
|
|
767
|
-
userId = doc.createdBy;
|
|
745
|
+
else {
|
|
746
|
+
assertText(doc.id, { field: 'doc.id', ...param });
|
|
768
747
|
}
|
|
769
|
-
|
|
770
|
-
|
|
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
|
-
|
|
773
|
-
|
|
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
|
-
|
|
781
|
-
|
|
771
|
+
}
|
|
772
|
+
return this.$insert(doc, opt);
|
|
782
773
|
}
|
|
783
774
|
/** @inheritDoc */
|
|
784
|
-
inserts(docs, p1, ignoreCheck) {
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
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
|
-
|
|
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());
|
|
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
|
-
|
|
827
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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());
|
|
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
|
-
|
|
865
|
-
|
|
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
|
-
|
|
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
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
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
|
-
|
|
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')));
|
|
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
|
-
|
|
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;
|
|
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
|
-
|
|
960
|
-
|
|
920
|
+
else {
|
|
921
|
+
assertText(key, { field: 'key', ...param });
|
|
961
922
|
}
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
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
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
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
|
-
|
|
986
|
-
|
|
962
|
+
}
|
|
963
|
+
return this.update(key, doc, opt, false);
|
|
987
964
|
}
|
|
988
965
|
/** @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
|
-
}
|
|
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
|
-
|
|
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
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
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
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
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
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
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
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
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
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
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
|
-
|
|
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
|
-
});
|
|
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
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
}
|
|
1162
|
-
|
|
1163
|
-
|
|
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
|
-
|
|
1171
|
-
|
|
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
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
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
|
|
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
|
-
|
|
1199
|
-
|
|
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
|
-
|
|
1205
|
-
|
|
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
|
|
1212
|
-
return this.$toDim(doc, 'view');
|
|
1213
|
-
});
|
|
1172
|
+
async toView(doc) {
|
|
1173
|
+
return doc;
|
|
1214
1174
|
}
|
|
1215
1175
|
/** @inheritDoc */
|
|
1216
|
-
getView(key) {
|
|
1217
|
-
|
|
1218
|
-
|
|
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
|
-
|
|
1224
|
-
|
|
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
|
-
|
|
1232
|
-
return
|
|
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
|
-
|
|
1238
|
-
|
|
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
|
-
|
|
1244
|
-
|
|
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;
|