@samet-it/be-db-common 1.1.3 → 1.1.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.
@@ -44,6 +44,10 @@ export interface DbExecOpt {
44
44
  * Name of SQL
45
45
  * */
46
46
  name?: string;
47
+ /**
48
+ * do not use cache
49
+ * */
50
+ noCache?: boolean;
47
51
  /**
48
52
  * Ignored errors
49
53
  * */
@@ -1,4 +1,4 @@
1
- import type { BasicType } from "@leyyo/common";
1
+ import type { Opt } from "@leyyo/common";
2
2
  import { DbError } from "./db.error";
3
3
  /**
4
4
  * Cache validation error
@@ -6,9 +6,7 @@ import { DbError } from "./db.error";
6
6
  export declare class DbInvalidValueError extends DbError {
7
7
  /**
8
8
  * @param {string} field - invalid field
9
- * @param {string} expected - expected type
10
- * @param {string} where - class name
11
- * @param {string} method - method name
9
+ * @param {Opt} params - parameters
12
10
  * */
13
- constructor(field: string, expected: BasicType, where: string, method: string);
11
+ constructor(field: string, params: Opt);
14
12
  }
@@ -9,12 +9,10 @@ const db_error_1 = require("./db.error");
9
9
  class DbInvalidValueError extends db_error_1.DbError {
10
10
  /**
11
11
  * @param {string} field - invalid field
12
- * @param {string} expected - expected type
13
- * @param {string} where - class name
14
- * @param {string} method - method name
12
+ * @param {Opt} params - parameters
15
13
  * */
16
- constructor(field, expected, where, method) {
17
- super(`${field} value is not valid at ${where}#${method}`, { field, expected, where, method });
14
+ constructor(field, params) {
15
+ super(`${field} value is not valid`, Object.assign(Object.assign({}, params), { field }));
18
16
  }
19
17
  }
20
18
  exports.DbInvalidValueError = DbInvalidValueError;
@@ -1,12 +1,14 @@
1
1
  import { DbError } from "./db.error";
2
+ import type { Opt } from "@leyyo/common";
2
3
  /**
3
4
  * DB not-supported error
4
5
  * */
5
6
  export declare class DbNotSupportedError extends DbError {
6
7
  /**
8
+ * Constructor
9
+ *
7
10
  * @param {string} feature - feature name
8
- * @param {string} where - class name
9
- * @param {string} method - method name
11
+ * @param {Opt} params - parameters
10
12
  * */
11
- constructor(feature: string, where: string, method: string);
13
+ constructor(feature: string, params: Opt);
12
14
  }
@@ -7,12 +7,13 @@ const db_error_1 = require("./db.error");
7
7
  * */
8
8
  class DbNotSupportedError extends db_error_1.DbError {
9
9
  /**
10
+ * Constructor
11
+ *
10
12
  * @param {string} feature - feature name
11
- * @param {string} where - class name
12
- * @param {string} method - method name
13
+ * @param {Opt} params - parameters
13
14
  * */
14
- constructor(feature, where, method) {
15
- super(`${feature} is not supported at ${where}#${method}`, { feature, where, method });
15
+ constructor(feature, params) {
16
+ super(`${feature} is not supported`, Object.assign(Object.assign({}, params), { feature }));
16
17
  }
17
18
  }
18
19
  exports.DbNotSupportedError = DbNotSupportedError;
@@ -45,7 +45,7 @@ class DbRepo {
45
45
  * */
46
46
  this._opt = {};
47
47
  if (!(0, type_1.isObjectFilled)(conn)) {
48
- throw new error_1.DbInvalidValueError('Connection', typeof conn, 'DbRepo', 'constructor');
48
+ throw new error_1.DbInvalidValueError('Connection', { where: 'DbRepo', method: 'constructor', type: typeof conn });
49
49
  }
50
50
  if (!(0, type_1.isObjectBare)(this._opt)) {
51
51
  this._opt = {};
@@ -59,7 +59,7 @@ class DbRepo {
59
59
  }
60
60
  if (this._opt.useVersion) {
61
61
  if (typeof this._opt.version !== 'number') {
62
- throw new error_1.DbInvalidValueError('Model version', typeof conn, 'DbRepo', 'constructor');
62
+ throw new error_1.DbInvalidValueError('Model version', { where: 'DbRepo', method: 'constructor', type: typeof this._opt.version });
63
63
  }
64
64
  }
65
65
  else {
@@ -84,7 +84,7 @@ class DbRepo {
84
84
  }
85
85
  }
86
86
  if (!this._opt.urnPrefix) {
87
- throw new error_1.DbInvalidValueError('Urn prefix', typeof conn, 'DbRepo', 'constructor');
87
+ throw new error_1.DbInvalidValueError('Urn prefix', { where: 'DbRepo', method: 'constructor', type: typeof this._opt.urnPrefix });
88
88
  }
89
89
  this._opt.forbiddenSet = this._checkForbiddenList(this._opt.forbiddenSet, ['id', '_urn', '_trashId', 'createdAt', 'createdBy']);
90
90
  this._opt.forbiddenUnset = this._checkForbiddenList(this._opt.forbiddenUnset, ['id', '_urn', '_trashId', 'createdAt', 'createdBy', 'updatedAt', 'updatedBy']);
@@ -155,14 +155,10 @@ class DbRepo {
155
155
  * */
156
156
  _urnFromDoc(doc, method) {
157
157
  const { path } = this._props;
158
- (0, type_1.assertObject)(doc, () => {
159
- return { field: 'doc', where: 'DbRepo', path, method };
160
- });
161
- const urn = doc._urn;
162
- (0, type_1.assertObject)(urn, () => {
163
- return { field: 'urn', where: 'DbRepo', path, method };
164
- });
165
- return urn;
158
+ const param = { where: 'DbRepo', path, method };
159
+ (0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
160
+ (0, type_1.assertText)(doc._urn, Object.assign({ field: 'urn' }, param));
161
+ return doc._urn;
166
162
  }
167
163
  _checkKey(value) {
168
164
  const { useNumericId } = this._props;
@@ -274,26 +270,29 @@ class DbRepo {
274
270
  /** @inheritDoc */
275
271
  get(k1, p1) {
276
272
  return __awaiter(this, void 0, void 0, function* () {
273
+ const { conn } = this._props;
274
+ const opt = conn.buildOpt(p1, 'get');
277
275
  const [key, field] = this._checkKey(k1);
278
276
  switch (field) {
279
277
  case "_urn":
280
- return this.getByPrimary(key, p1, true);
278
+ return this.getByPrimary(key, opt, true);
281
279
  case "id":
282
- return this.getBySecondary(key, p1, true);
280
+ return this.getBySecondary(key, opt, true);
283
281
  default:
284
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'get' });
282
+ (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'get', queryName: opt.name });
285
283
  }
286
284
  });
287
285
  }
288
286
  /** @inheritDoc */
289
287
  getByPrimary(key, p1, ignoreCheck) {
290
288
  return __awaiter(this, void 0, void 0, function* () {
291
- const { cache } = this._props;
289
+ const { cache, conn } = this._props;
290
+ const opt = conn.buildOpt(p1, 'getByPrimary');
292
291
  if (!ignoreCheck) {
293
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'getByPrimary' });
292
+ (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'getByPrimary', queryName: opt.name });
294
293
  }
295
294
  let found;
296
- if (cache.props.isConnected) {
295
+ if (!opt.noCache && cache.props.isConnected) {
297
296
  try {
298
297
  found = (yield cache.getByPrimary(key))[0];
299
298
  }
@@ -304,8 +303,8 @@ class DbRepo {
304
303
  return found;
305
304
  }
306
305
  }
307
- found = yield this.$getByPrimary(key, p1, true);
308
- if (found && cache.props.isConnected) {
306
+ found = yield this.$getByPrimary(key, opt, true);
307
+ if (!opt.noCache && found && cache.props.isConnected) {
309
308
  cache.ingestDoc(found)
310
309
  .then()
311
310
  .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'getByPrimary', 'cache', 'ingestDoc')));
@@ -316,20 +315,21 @@ class DbRepo {
316
315
  /** @inheritDoc */
317
316
  getBySecondary(key, p1, ignoreCheck) {
318
317
  return __awaiter(this, void 0, void 0, function* () {
319
- const { cache, useNumericId, idSame } = this._props;
318
+ const { cache, useNumericId, idSame, conn } = this._props;
319
+ const opt = conn.buildOpt(p1, 'getBySecondary');
320
320
  if (!ignoreCheck) {
321
321
  if (useNumericId) {
322
- (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary' });
322
+ (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary', queryName: opt.name });
323
323
  }
324
324
  else {
325
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary' });
325
+ (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary', queryName: opt.name });
326
326
  }
327
327
  }
328
328
  if (idSame) {
329
- return this.getByPrimary(this._keyToUrn(key), p1, true);
329
+ return this.getByPrimary(this._keyToUrn(key), opt, true);
330
330
  }
331
331
  let found;
332
- if (cache.props.isConnected) {
332
+ if (!opt.noCache && cache.props.isConnected) {
333
333
  try {
334
334
  found = (yield cache.getBySecondary(key))[0];
335
335
  if (found) {
@@ -340,7 +340,7 @@ class DbRepo {
340
340
  this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'getBySecondary', 'cache', 'getBySecondary'));
341
341
  }
342
342
  }
343
- found = yield this.$getBySecondary(key, p1, true);
343
+ found = yield this.$getBySecondary(key, opt, true);
344
344
  if (found && cache.props.isConnected) {
345
345
  cache.ingestDoc(found)
346
346
  .then()
@@ -354,14 +354,16 @@ class DbRepo {
354
354
  /** @inheritDoc */
355
355
  exists(k1, p1) {
356
356
  return __awaiter(this, void 0, void 0, function* () {
357
+ const { conn } = this._props;
358
+ const opt = conn.buildOpt(p1, 'exists');
357
359
  const [key, field] = this._checkKey(k1);
358
360
  switch (field) {
359
361
  case "_urn":
360
- return this.existsByPrimary(key, p1, true);
362
+ return this.existsByPrimary(key, opt, true);
361
363
  case "id":
362
- return this.existsBySecondary(key, p1, true);
364
+ return this.existsBySecondary(key, opt, true);
363
365
  default:
364
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'exists' });
366
+ (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'exists', queryName: opt.name });
365
367
  break;
366
368
  }
367
369
  });
@@ -369,11 +371,12 @@ class DbRepo {
369
371
  /** @inheritDoc */
370
372
  existsByPrimary(key, p1, ignoreCheck) {
371
373
  return __awaiter(this, void 0, void 0, function* () {
372
- const { cache } = this._props;
374
+ const { cache, conn } = this._props;
375
+ const opt = conn.buildOpt(p1, 'existsByPrimary');
373
376
  if (!ignoreCheck) {
374
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'existsByPrimary' });
377
+ (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'existsByPrimary', queryName: opt.name });
375
378
  }
376
- if (cache.props.isConnected) {
379
+ if (!opt.noCache && cache.props.isConnected) {
377
380
  try {
378
381
  const found = (yield cache.getByPrimary(key))[0];
379
382
  if (found) {
@@ -384,25 +387,26 @@ class DbRepo {
384
387
  this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'existsByPrimary', 'cache', 'getByPrimary'));
385
388
  }
386
389
  }
387
- return this.$existsByPrimary(key, p1, true);
390
+ return this.$existsByPrimary(key, opt, true);
388
391
  });
389
392
  }
390
393
  /** @inheritDoc */
391
394
  existsBySecondary(key, p1, ignoreCheck) {
392
395
  return __awaiter(this, void 0, void 0, function* () {
393
- const { cache, useNumericId, idSame } = this._props;
396
+ const { cache, useNumericId, idSame, conn } = this._props;
397
+ const opt = conn.buildOpt(p1, 'existsBySecondary');
394
398
  if (!ignoreCheck) {
395
399
  if (useNumericId) {
396
- (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary' });
400
+ (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary', queryName: opt.name });
397
401
  }
398
402
  else {
399
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary' });
403
+ (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'existsBySecondary', queryName: opt.name });
400
404
  }
401
405
  }
402
406
  if (!idSame) {
403
407
  return this.existsByPrimary(this._keyToUrn(key));
404
408
  }
405
- if (cache.props.isConnected) {
409
+ if (!opt.noCache && cache.props.isConnected) {
406
410
  try {
407
411
  const found = (yield cache.getBySecondary(key))[0];
408
412
  if (found) {
@@ -413,7 +417,7 @@ class DbRepo {
413
417
  this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'existsBySecondary', 'cache', 'getBySecondary'));
414
418
  }
415
419
  }
416
- return this.$existsBySecondary(key, p1, true);
420
+ return this.$existsBySecondary(key, opt, true);
417
421
  });
418
422
  }
419
423
  // endregion exists
@@ -421,11 +425,11 @@ class DbRepo {
421
425
  /** @inheritDoc */
422
426
  list(keys, p1, ignoreCheck) {
423
427
  return __awaiter(this, void 0, void 0, function* () {
424
- if (!ignoreCheck) {
425
- (0, type_1.assertArray)(keys, { field: 'keys', where: 'DbRepo', method: 'list' });
426
- }
427
428
  const { conn, cache } = this._props;
428
429
  const opt = conn.buildOpt(p1, 'list');
430
+ if (!ignoreCheck) {
431
+ (0, type_1.assertArray)(keys, { field: 'keys', where: 'DbRepo', method: 'list', queryName: opt.name });
432
+ }
429
433
  const result = this._checkKeys(keys);
430
434
  if (result.ordered.length < 1) {
431
435
  return [];
@@ -433,7 +437,7 @@ class DbRepo {
433
437
  const docs = [];
434
438
  let founds = [];
435
439
  if (result.urns.length > 0) {
436
- if (cache.props.isConnected) {
440
+ if (!opt.noCache && cache.props.isConnected) {
437
441
  try {
438
442
  founds = yield cache.getByPrimary(...result.urns);
439
443
  }
@@ -451,7 +455,7 @@ class DbRepo {
451
455
  if (result.urns.length > 0) {
452
456
  founds = yield this.$listByPrimary(result.urns, opt, true);
453
457
  if (founds.length > 0) {
454
- if (cache.props.isConnected) {
458
+ if (!opt.noCache && cache.props.isConnected) {
455
459
  founds.forEach(found => cache.ingestDoc(found)
456
460
  .then()
457
461
  .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'list', 'cache', 'ingestDoc'))));
@@ -461,7 +465,7 @@ class DbRepo {
461
465
  }
462
466
  }
463
467
  if (result.ids.length > 0) {
464
- if (cache.props.isConnected) {
468
+ if (!opt.noCache && cache.props.isConnected) {
465
469
  try {
466
470
  founds = yield cache.getBySecondary(...result.ids);
467
471
  }
@@ -479,7 +483,7 @@ class DbRepo {
479
483
  if (result.ids.length > 0) {
480
484
  founds = yield this.$listBySecondary(result.ids, opt, true);
481
485
  if (founds.length > 0) {
482
- if (cache.props.isConnected) {
486
+ if (!opt.noCache && cache.props.isConnected) {
483
487
  founds.forEach(found => cache.ingestDoc(found)
484
488
  .then()
485
489
  .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'list', 'cache', 'ingestDoc'))));
@@ -506,19 +510,21 @@ class DbRepo {
506
510
  /** @inheritDoc */
507
511
  insert(doc, p1, ignoreCheck) {
508
512
  return __awaiter(this, void 0, void 0, function* () {
509
- const { useCreatedAt, noInsert, useNumericId, useCreatedBy, useUpdatedAt, useUpdatedBy } = this._props;
513
+ const { useCreatedAt, conn, noInsert, useNumericId, useCreatedBy, useUpdatedAt, useUpdatedBy } = this._props;
514
+ const opt = conn.buildOpt(p1, 'insert');
515
+ const param = { where: 'DbRepo', method: 'insert', queryName: opt.name };
510
516
  if (noInsert) {
511
- throw new error_1.DbNotSupportedError('Insert', 'DbRepo', 'insert');
517
+ throw new error_1.DbNotSupportedError('Insert', param);
512
518
  }
513
519
  if (!ignoreCheck) {
514
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'insert' });
520
+ (0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
515
521
  if (useNumericId) {
516
- (0, type_1.assertInteger)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'insert' });
522
+ (0, type_1.assertInteger)(doc.id, Object.assign({ field: 'doc.id' }, param));
517
523
  }
518
524
  else {
519
- (0, type_1.assertText)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'insert' });
525
+ (0, type_1.assertText)(doc.id, Object.assign({ field: 'doc.id' }, param));
520
526
  }
521
- (0, type_1.assertText)(doc._urn, { field: 'doc._urn', where: 'DbRepo', method: 'insert' });
527
+ (0, type_1.assertText)(doc._urn, Object.assign({ field: 'doc._urn' }, param));
522
528
  }
523
529
  if (!doc._trashId !== undefined) {
524
530
  delete doc._trashId;
@@ -535,22 +541,24 @@ class DbRepo {
535
541
  if (useUpdatedBy) {
536
542
  // todo
537
543
  }
538
- return this.$insert(doc, p1);
544
+ return this.$insert(doc, opt);
539
545
  });
540
546
  }
541
547
  /** @inheritDoc */
542
548
  inserts(docs, p1, ignoreCheck) {
543
549
  return __awaiter(this, void 0, void 0, function* () {
544
- const { noInsert } = this._props;
550
+ const { noInsert, conn } = this._props;
551
+ const opt = conn.buildOpt(p1, 'inserts');
552
+ const param = { where: 'DbRepo', method: 'inserts', queryName: opt.name };
545
553
  if (noInsert) {
546
- throw new error_1.DbNotSupportedError('Insert', 'DbRepo', 'inserts');
554
+ throw new error_1.DbNotSupportedError('Insert', param);
547
555
  }
548
556
  if (!ignoreCheck) {
549
- (0, type_1.assertArray)(docs, { field: 'docs', where: 'DbRepo', method: 'inserts' });
557
+ (0, type_1.assertArray)(docs, Object.assign({ field: 'docs' }, param));
550
558
  }
551
559
  const insertedIds = [];
552
560
  for (const doc of docs) {
553
- insertedIds.push(yield this.insert(doc, p1, true));
561
+ insertedIds.push(yield this.insert(doc, opt, true));
554
562
  }
555
563
  return insertedIds;
556
564
  });
@@ -560,15 +568,16 @@ class DbRepo {
560
568
  /** @inheritDoc */
561
569
  replace(doc, p1, ignoreCheck) {
562
570
  return __awaiter(this, void 0, void 0, function* () {
563
- const { cache, useNumericId, useUpdatedAt, useUpdatedBy } = this._props;
571
+ const { cache, conn, useNumericId, useUpdatedAt, useUpdatedBy } = this._props;
572
+ const opt = conn.buildOpt(p1, 'replace');
564
573
  if (!ignoreCheck) {
565
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'replace' });
566
- (0, type_1.assertText)(doc._urn, { field: 'doc._urn', where: 'DbRepo', method: 'replace' });
574
+ (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'replace', queryName: opt.name });
575
+ (0, type_1.assertText)(doc._urn, { field: 'doc._urn', where: 'DbRepo', method: 'replace', queryName: opt.name });
567
576
  if (useNumericId) {
568
- (0, type_1.assertInteger)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'replace' });
577
+ (0, type_1.assertInteger)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'replace', queryName: opt.name });
569
578
  }
570
579
  else {
571
- (0, type_1.assertText)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'replace' });
580
+ (0, type_1.assertText)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'replace', queryName: opt.name });
572
581
  }
573
582
  }
574
583
  if (doc._trashId !== undefined) {
@@ -580,8 +589,8 @@ class DbRepo {
580
589
  if (useUpdatedBy) {
581
590
  // todo
582
591
  }
583
- const replacedKey = yield this.$replace(doc, p1, true);
584
- if (cache.props.isConnected) {
592
+ const replacedKey = yield this.$replace(doc, opt, true);
593
+ if (!opt.noCache && cache.props.isConnected) {
585
594
  cache.clearDoc(replacedKey)
586
595
  .then()
587
596
  .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'replace', 'cache', 'clearDoc')));
@@ -594,18 +603,20 @@ class DbRepo {
594
603
  /** @inheritDoc */
595
604
  upsert(doc, p1, ignoreCheck) {
596
605
  return __awaiter(this, void 0, void 0, function* () {
597
- const { noInsert, noUpdate, cache, useNumericId, useUpdatedAt, useUpdatedBy } = this._props;
606
+ const { noInsert, conn, noUpdate, cache, useNumericId, useUpdatedAt, useUpdatedBy } = this._props;
607
+ const opt = conn.buildOpt(p1, 'upsert');
608
+ const param = { where: 'DbRepo', method: 'upsert', queryName: opt.name };
598
609
  if (noInsert || noUpdate) {
599
- throw new error_1.DbNotSupportedError('Upsert', 'DbRepo', 'upsert');
610
+ throw new error_1.DbNotSupportedError('Upsert', param);
600
611
  }
601
612
  if (!ignoreCheck) {
602
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'upsert' });
603
- (0, type_1.assertText)(doc._urn, { field: 'doc._urn', where: 'DbRepo', method: 'upsert' });
613
+ (0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
614
+ (0, type_1.assertText)(doc._urn, Object.assign({ field: 'doc._urn' }, param));
604
615
  if (useNumericId) {
605
- (0, type_1.assertInteger)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'upsert' });
616
+ (0, type_1.assertInteger)(doc.id, Object.assign({ field: 'doc.id' }, param));
606
617
  }
607
618
  else {
608
- (0, type_1.assertText)(doc.id, { field: 'doc.id', where: 'DbRepo', method: 'upsert' });
619
+ (0, type_1.assertText)(doc.id, Object.assign({ field: 'doc.id' }, param));
609
620
  }
610
621
  }
611
622
  if (doc._trashId !== undefined) {
@@ -617,8 +628,8 @@ class DbRepo {
617
628
  if (useUpdatedBy) {
618
629
  // todo
619
630
  }
620
- const createdKey = yield this.$replace(doc, p1, true);
621
- if (cache.props.isConnected) {
631
+ const createdKey = yield this.$replace(doc, opt, true);
632
+ if (!opt.noCache && cache.props.isConnected) {
622
633
  cache.clearDoc(createdKey)
623
634
  .then()
624
635
  .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'upsert', 'cache', 'clearDoc')));
@@ -631,30 +642,34 @@ class DbRepo {
631
642
  /** @inheritDoc */
632
643
  update(k1, doc, p1, ignoreCheck) {
633
644
  return __awaiter(this, void 0, void 0, function* () {
645
+ const { conn } = this._props;
646
+ const opt = conn.buildOpt(p1, 'update');
634
647
  if (!ignoreCheck) {
635
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'update' });
648
+ (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'update', queryName: opt.name });
636
649
  }
637
650
  const [key, field] = this._checkKey(k1);
638
651
  switch (field) {
639
652
  case "_urn":
640
- return this.updateByPrimary(key, doc, p1, true);
653
+ return this.updateByPrimary(key, doc, opt, true);
641
654
  case "id":
642
- return this.updateBySecondary(key, doc, p1, true);
655
+ return this.updateBySecondary(key, doc, opt, true);
643
656
  default:
644
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'update' });
657
+ (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'update', queryName: opt.name });
645
658
  }
646
659
  });
647
660
  }
648
661
  /** @inheritDoc */
649
662
  updateByPrimary(key, doc, p1, ignoreCheck) {
650
663
  return __awaiter(this, void 0, void 0, function* () {
651
- const { noUpdate, forbiddenSet, cache, useUpdatedAt, useUpdatedBy } = this._props;
664
+ const { noUpdate, conn, forbiddenSet, cache, useUpdatedAt, useUpdatedBy } = this._props;
665
+ const opt = conn.buildOpt(p1, 'updateByPrimary');
666
+ const param = { where: 'DbRepo', method: 'updateByPrimary', queryName: opt.name };
652
667
  if (noUpdate) {
653
- throw new error_1.DbNotSupportedError('Update', 'DbRepo', 'updateByPrimary');
668
+ throw new error_1.DbNotSupportedError('Update', param);
654
669
  }
655
670
  if (!ignoreCheck) {
656
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'updateByPrimary' });
657
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'updateByPrimary' });
671
+ (0, type_1.assertText)(key, Object.assign({ field: 'key' }, param));
672
+ (0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
658
673
  }
659
674
  if (doc._trashId !== undefined) {
660
675
  delete doc._trashId;
@@ -670,8 +685,8 @@ class DbRepo {
670
685
  if (useUpdatedBy) {
671
686
  // todo
672
687
  }
673
- const updatedKey = yield this.$updateByPrimary(key, doc, p1, true);
674
- if (updatedKey && cache.props.isConnected) {
688
+ const updatedKey = yield this.$updateByPrimary(key, doc, opt, true);
689
+ if (updatedKey && !opt.noCache && cache.props.isConnected) {
675
690
  cache.clearDoc(updatedKey)
676
691
  .then()
677
692
  .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'updateByPrimary', 'cache', 'clearDoc')));
@@ -682,21 +697,23 @@ class DbRepo {
682
697
  /** @inheritDoc */
683
698
  updateBySecondary(key, doc, p1, ignoreCheck) {
684
699
  return __awaiter(this, void 0, void 0, function* () {
685
- const { forbiddenSet, noUpdate, cache, useNumericId, idSame, useUpdatedAt, useUpdatedBy } = this._props;
700
+ const { forbiddenSet, noUpdate, cache, useNumericId, idSame, useUpdatedAt, useUpdatedBy, conn } = this._props;
701
+ const opt = conn.buildOpt(p1, 'updateBySecondary');
702
+ const param = { where: 'DbRepo', method: 'updateBySecondary', queryName: opt.name };
686
703
  if (noUpdate) {
687
- throw new error_1.DbNotSupportedError('Update', 'DbRepo', 'updateBySecondary');
704
+ throw new error_1.DbNotSupportedError('Update', param);
688
705
  }
689
706
  if (!ignoreCheck) {
690
707
  if (useNumericId) {
691
- (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary' });
708
+ (0, type_1.assertInteger)(key, Object.assign({ field: 'key' }, param));
692
709
  }
693
710
  else {
694
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'getBySecondary' });
711
+ (0, type_1.assertText)(key, Object.assign({ field: 'key' }, param));
695
712
  }
696
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'getBySecondary' });
713
+ (0, type_1.assertObject)(doc, Object.assign({ field: 'doc' }, param));
697
714
  }
698
715
  if (idSame) {
699
- return this.updateBySecondary(this._keyToUrn(key), doc, p1, true);
716
+ return this.updateBySecondary(this._keyToUrn(key), doc, opt, true);
700
717
  }
701
718
  if (doc._trashId !== undefined) {
702
719
  delete doc._trashId;
@@ -712,8 +729,8 @@ class DbRepo {
712
729
  if (useUpdatedBy) {
713
730
  // todo
714
731
  }
715
- const updatedKey = yield this.$updateBySecondary(key, doc, p1, true);
716
- if (updatedKey && cache.props.isConnected) {
732
+ const updatedKey = yield this.$updateBySecondary(key, doc, opt, true);
733
+ if (updatedKey && !opt.noCache && cache.props.isConnected) {
717
734
  cache.clearDoc(updatedKey)
718
735
  .then()
719
736
  .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'updateBySecondary', 'cache', 'clearDoc')));
@@ -726,24 +743,28 @@ class DbRepo {
726
743
  /** @inheritDoc */
727
744
  set(key, doc, p1, ignoreCheck) {
728
745
  return __awaiter(this, void 0, void 0, function* () {
746
+ const { conn } = this._props;
747
+ const opt = conn.buildOpt(p1, 'set');
729
748
  if (!ignoreCheck) {
730
- (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'set' });
749
+ (0, type_1.assertObject)(doc, { field: 'doc', where: 'DbRepo', method: 'set', queryName: opt.name });
731
750
  }
732
751
  for (const [f, v] of Object.entries(doc)) {
733
752
  if (v === undefined) { // dont allow undefined
734
753
  delete doc[f];
735
754
  }
736
755
  }
737
- return this.update(key, doc, p1, false);
756
+ return this.update(key, doc, opt, false);
738
757
  });
739
758
  }
740
759
  /** @inheritDoc */
741
760
  unset(key, fields, p1, ignoreCheck) {
742
761
  return __awaiter(this, void 0, void 0, function* () {
762
+ const { conn } = this._props;
763
+ const opt = conn.buildOpt(p1, 'unset');
743
764
  if (!ignoreCheck) {
744
- (0, type_1.assertArray)(fields, { field: 'fields', where: 'DbRepo', method: 'unset' });
765
+ (0, type_1.assertArray)(fields, { field: 'fields', where: 'DbRepo', method: 'unset', queryName: opt.name });
745
766
  fields.forEach((field, index) => {
746
- (0, type_1.assertText)(field, { field: field, where: 'DbRepo', method: 'unset', index });
767
+ (0, type_1.assertText)(field, { field: field, where: 'DbRepo', method: 'unset', index, queryName: opt.name });
747
768
  });
748
769
  }
749
770
  const { forbiddenUnset } = this._props;
@@ -753,7 +774,7 @@ class DbRepo {
753
774
  doc[f] = undefined;
754
775
  }
755
776
  });
756
- return this.update(key, doc, p1, false);
777
+ return this.update(key, doc, opt, false);
757
778
  });
758
779
  }
759
780
  // endregion set
@@ -761,29 +782,33 @@ class DbRepo {
761
782
  /** @inheritDoc */
762
783
  remove(k1, p1) {
763
784
  return __awaiter(this, void 0, void 0, function* () {
785
+ const { conn } = this._props;
786
+ const opt = conn.buildOpt(p1, 'remove');
764
787
  const [key, field] = this._checkKey(k1);
765
788
  switch (field) {
766
789
  case "_urn":
767
- return this.removeByPrimary(key, p1, true);
790
+ return this.removeByPrimary(key, opt, true);
768
791
  case "id":
769
- return this.removeBySecondary(key, p1, true);
792
+ return this.removeBySecondary(key, opt, true);
770
793
  default:
771
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'remove' });
794
+ (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'remove', queryName: opt.name });
772
795
  }
773
796
  });
774
797
  }
775
798
  /** @inheritDoc */
776
799
  removeByPrimary(key, p1, ignoreCheck) {
777
800
  return __awaiter(this, void 0, void 0, function* () {
778
- const { cache, noRemove } = this._props;
801
+ const { cache, noRemove, conn } = this._props;
802
+ const opt = conn.buildOpt(p1, 'removeByPrimary');
803
+ const param = { where: 'DbRepo', method: 'removeByPrimary', queryName: opt.name };
779
804
  if (noRemove) {
780
- throw new error_1.DbNotSupportedError('Remove', 'DbRepo', 'removeByPrimary');
805
+ throw new error_1.DbNotSupportedError('Remove', param);
781
806
  }
782
807
  if (!ignoreCheck) {
783
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'removeByPrimary' });
808
+ (0, type_1.assertText)(key, Object.assign({ field: 'key' }, param));
784
809
  }
785
- const removedKey = yield this.$removeByPrimary(key, p1, true);
786
- if (removedKey && cache.props.isConnected) {
810
+ const removedKey = yield this.$removeByPrimary(key, opt, true);
811
+ if (removedKey && !opt.noCache && cache.props.isConnected) {
787
812
  cache.clearDoc(removedKey)
788
813
  .then()
789
814
  .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'removeByPrimary', 'cache', 'clearDoc')));
@@ -794,21 +819,23 @@ class DbRepo {
794
819
  /** @inheritDoc */
795
820
  removeBySecondary(key, p1, ignoreCheck) {
796
821
  return __awaiter(this, void 0, void 0, function* () {
797
- const { cache, noRemove, useNumericId, idSame } = this._props;
822
+ const { cache, noRemove, useNumericId, idSame, conn } = this._props;
823
+ const opt = conn.buildOpt(p1, 'removeBySecondary');
824
+ const param = { where: 'DbRepo', method: 'removeByPrimary', queryName: opt.name };
798
825
  if (noRemove) {
799
- throw new error_1.DbNotSupportedError('Remove', 'DbRepo', 'removeByPrimary');
826
+ throw new error_1.DbNotSupportedError('Remove', param);
800
827
  }
801
828
  if (useNumericId) {
802
- (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'removeBySecondary' });
829
+ (0, type_1.assertInteger)(key, { field: 'key', param });
803
830
  }
804
831
  else {
805
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'removeBySecondary' });
832
+ (0, type_1.assertText)(key, { field: 'key', param });
806
833
  }
807
834
  if (idSame) {
808
- return this.removeByPrimary(this._keyToUrn(key), p1, ignoreCheck);
835
+ return this.removeByPrimary(this._keyToUrn(key), opt, ignoreCheck);
809
836
  }
810
- const removedKey = yield this.$removeBySecondary(key, p1, true);
811
- if (removedKey && cache.props.isConnected) {
837
+ const removedKey = yield this.$removeBySecondary(key, opt, true);
838
+ if (removedKey && !opt.noCache && cache.props.isConnected) {
812
839
  cache.clearDoc(removedKey)
813
840
  .then()
814
841
  .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'removeBySecondary', 'cache', 'clearDoc')));
@@ -821,33 +848,39 @@ class DbRepo {
821
848
  /** @inheritDoc */
822
849
  trash(k1, p1) {
823
850
  return __awaiter(this, void 0, void 0, function* () {
851
+ const { conn } = this._props;
852
+ const opt = conn.buildOpt(p1, 'trash');
824
853
  const [key, field] = this._checkKey(k1);
825
854
  switch (field) {
826
855
  case "_urn":
827
- return this.trashByPrimary(key, p1, true);
856
+ return this.trashByPrimary(key, opt, true);
828
857
  case "id":
829
- return this.trashBySecondary(key, p1, true);
858
+ return this.trashBySecondary(key, opt, true);
830
859
  default:
831
- (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'trash' });
860
+ (0, type_1.assertMessage)('Invalid key', { field: 'key', value: k1, where: 'DbRepo', method: 'trash', queryName: opt.name });
832
861
  }
833
862
  });
834
863
  }
835
864
  /** @inheritDoc */
836
865
  trashByPrimary(key, p1, ignoreCheck) {
837
866
  return __awaiter(this, void 0, void 0, function* () {
838
- const { noTrash, conn, cache } = this._props;
867
+ const { noTrash, useSoftDelete, conn, cache } = this._props;
868
+ const opt = conn.buildOpt(p1, 'trashByPrimary');
869
+ const param = { where: 'DbRepo', method: 'trashByPrimary', queryName: opt.name };
870
+ if (!useSoftDelete) {
871
+ throw new error_1.DbNotSupportedError('Soft Delete', param);
872
+ }
839
873
  if (noTrash) {
840
- throw new error_1.DbNotSupportedError('Trash', 'DbRepo', 'trashByPrimary');
874
+ throw new error_1.DbNotSupportedError('Trash', param);
841
875
  }
842
876
  if (!ignoreCheck) {
843
- (0, type_1.assertText)(key, { field: 'urn', where: 'DbRepo', method: 'trashByPrimary' });
877
+ (0, type_1.assertText)(key, Object.assign({ field: 'urn' }, param));
844
878
  }
845
- const opt = conn.buildOpt(p1, 'trashByPrimary');
846
879
  if (!(0, type_1.isText)(opt.trashId)) {
847
880
  opt.trashId = (0, node_crypto_1.randomUUID)();
848
881
  }
849
882
  const trashedKey = yield this.$trashByPrimary(key, opt, true);
850
- if (trashedKey && cache.props.isConnected) {
883
+ if (trashedKey && !opt.noCache && cache.props.isConnected) {
851
884
  cache.clearDoc(trashedKey)
852
885
  .then()
853
886
  .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'trashByPrimary', 'cache', 'clearDoc')));
@@ -858,25 +891,29 @@ class DbRepo {
858
891
  /** @inheritDoc */
859
892
  trashBySecondary(key, p1, ignoreCheck) {
860
893
  return __awaiter(this, void 0, void 0, function* () {
861
- const { noTrash, useNumericId, idSame, conn, cache } = this._props;
894
+ const { noTrash, useSoftDelete, useNumericId, idSame, conn, cache } = this._props;
895
+ const opt = conn.buildOpt(p1, 'trashBySecondary');
896
+ const param = { where: 'DbRepo', method: 'trashBySecondary', queryName: opt.name };
897
+ if (!useSoftDelete) {
898
+ throw new error_1.DbNotSupportedError('Soft Delete', param);
899
+ }
862
900
  if (noTrash) {
863
- throw new error_1.DbNotSupportedError('Trash', 'DbRepo', 'trashBySecondary');
901
+ throw new error_1.DbNotSupportedError('Trash', param);
864
902
  }
865
903
  if (useNumericId) {
866
- (0, type_1.assertInteger)(key, { field: 'key', where: 'DbRepo', method: 'trashBySecondary' });
904
+ (0, type_1.assertInteger)(key, Object.assign({ field: 'key' }, param));
867
905
  }
868
906
  else {
869
- (0, type_1.assertText)(key, { field: 'key', where: 'DbRepo', method: 'trashBySecondary' });
907
+ (0, type_1.assertText)(key, Object.assign({ field: 'key' }, param));
870
908
  }
871
909
  if (idSame) {
872
- return this.trashByPrimary(this._keyToUrn(key), p1, ignoreCheck);
910
+ return this.trashByPrimary(this._keyToUrn(key), opt, ignoreCheck);
873
911
  }
874
- const opt = conn.buildOpt(p1, 'trashBySecondary');
875
912
  if (!(0, type_1.isText)(opt.trashId)) {
876
913
  opt.trashId = (0, node_crypto_1.randomUUID)();
877
914
  }
878
915
  const trashedKey = yield this.$trashBySecondary(key, opt, true);
879
- if (trashedKey && cache.props.isConnected) {
916
+ if (trashedKey && !opt.noCache && cache.props.isConnected) {
880
917
  cache.clearDoc(trashedKey)
881
918
  .then()
882
919
  .catch(e => this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'trashBySecondary', 'cache', 'clearDoc')));
@@ -587,6 +587,15 @@ export interface DbRepoOpt<ENT extends Entity<ID>, ID extends KeyValue> {
587
587
  * @default false
588
588
  * */
589
589
  idSame?: boolean;
590
+ /**
591
+ * Ignores soft delete
592
+ * - if yes, it ignores trash case
593
+ * - if no, it validates trash case
594
+ *
595
+ * @fields `_trashId`
596
+ * @default false
597
+ * */
598
+ useSoftDelete?: boolean;
590
599
  /**
591
600
  * Manages `created at` case, as time by `useIsoDate`
592
601
  * - if yes, it builds `createdAt`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samet-it/be-db-common",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "Backend DB Common",
5
5
  "repository": {
6
6
  "type": "git",