@naturalcycles/db-lib 8.34.1 → 8.34.2

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.
@@ -85,7 +85,7 @@ export declare class CommonDao<BM extends Partial<ObjectWithId>, DBM extends Obj
85
85
  * Mutates with id, created, updated
86
86
  */
87
87
  save(bm: BM, opt?: CommonDaoSaveOptions<DBM>): Promise<Saved<BM>>;
88
- private ensureImmutableDoesntExist;
88
+ private ensureImmutableDontExist;
89
89
  private ensureUniqueId;
90
90
  private throwIfObjectExists;
91
91
  /**
@@ -442,7 +442,7 @@ class CommonDao {
442
442
  if (opt.ensureUniqueId && idWasGenerated)
443
443
  await this.ensureUniqueId(table, dbm);
444
444
  if (this.cfg.immutable)
445
- await this.ensureImmutableDoesntExist(table, dbm);
445
+ await this.ensureImmutableDontExist(table, [dbm.id]);
446
446
  const op = `save(${dbm.id})`;
447
447
  const started = this.logSaveStarted(op, bm, table);
448
448
  await this.cfg.db.saveBatch(table, [dbm], {
@@ -452,31 +452,29 @@ class CommonDao {
452
452
  this.logSaveResult(started, op, table);
453
453
  return bm;
454
454
  }
455
- async ensureImmutableDoesntExist(table, dbm) {
456
- await this.throwIfObjectExists(table, dbm, [
455
+ async ensureImmutableDontExist(table, ids) {
456
+ await this.throwIfObjectExists(table, ids, [
457
457
  cnst_1.DBLibError.OBJECT_IS_IMMUTABLE,
458
458
  {
459
459
  code: cnst_1.DBLibError.OBJECT_IS_IMMUTABLE,
460
- id: dbm.id,
461
460
  table,
462
461
  },
463
462
  ]);
464
463
  }
465
464
  async ensureUniqueId(table, dbm) {
466
465
  // todo: retry N times
467
- await this.throwIfObjectExists(table, dbm, [
466
+ await this.throwIfObjectExists(table, [dbm.id], [
468
467
  cnst_1.DBLibError.OBJECT_IS_IMMUTABLE,
469
468
  {
470
469
  code: cnst_1.DBLibError.OBJECT_IS_IMMUTABLE,
471
- id: dbm.id,
472
470
  table,
473
471
  },
474
472
  ]);
475
473
  }
476
- async throwIfObjectExists(table, dbm, errorMeta) {
477
- const [existing] = await this.cfg.db.getByIds(table, [dbm.id]);
478
- if (existing)
479
- throw new js_lib_1.AppError(errorMeta[0], errorMeta[1]);
474
+ async throwIfObjectExists(table, ids, errorMeta) {
475
+ const existing = await this.cfg.db.getByIds(table, ids);
476
+ if (existing.length > 0)
477
+ throw new js_lib_1.AppError(errorMeta[0], { ...errorMeta[1], ids: existing.map(i => i.id) });
480
478
  }
481
479
  /**
482
480
  * Loads the row by id.
@@ -512,7 +510,7 @@ class CommonDao {
512
510
  if (opt.ensureUniqueId && idWasGenerated)
513
511
  await this.ensureUniqueId(table, dbm);
514
512
  if (this.cfg.immutable)
515
- await this.ensureImmutableDoesntExist(table, dbm);
513
+ await this.ensureImmutableDontExist(table, [dbm.id]);
516
514
  }
517
515
  const op = `saveAsDBM(${dbm.id})`;
518
516
  const started = this.logSaveStarted(op, dbm, table);
@@ -531,7 +529,7 @@ class CommonDao {
531
529
  if (opt.ensureUniqueId)
532
530
  throw new js_lib_1.AppError('ensureUniqueId is not supported in saveBatch');
533
531
  if (this.cfg.immutable)
534
- throw new js_lib_1.AppError('immutable DB entries are not supported in saveBatch');
532
+ await this.ensureImmutableDontExist(table, dbms.map(dbm => dbm.id));
535
533
  const op = `saveBatch ${dbms.length} row(s) (${(0, js_lib_1._truncate)(dbms
536
534
  .slice(0, 10)
537
535
  .map(bm => bm.id)
@@ -553,7 +551,7 @@ class CommonDao {
553
551
  if (opt.ensureUniqueId)
554
552
  throw new js_lib_1.AppError('ensureUniqueId is not supported in saveBatch');
555
553
  if (this.cfg.immutable)
556
- throw new js_lib_1.AppError('immutable objects are not supported in saveBatch');
554
+ await this.ensureImmutableDontExist(table, dbms.map(dbm => dbm.id));
557
555
  }
558
556
  const op = `saveBatchAsDBM ${dbms.length} row(s) (${(0, js_lib_1._truncate)(dbms
559
557
  .slice(0, 10)
package/package.json CHANGED
@@ -43,7 +43,7 @@
43
43
  "engines": {
44
44
  "node": ">=14.15"
45
45
  },
46
- "version": "8.34.1",
46
+ "version": "8.34.2",
47
47
  "description": "Lowest Common Denominator API to supported Databases",
48
48
  "keywords": [
49
49
  "db",
@@ -601,7 +601,7 @@ export class CommonDao<
601
601
  const dbm = await this.bmToDBM(bm, opt)
602
602
  const table = opt.table || this.cfg.table
603
603
  if (opt.ensureUniqueId && idWasGenerated) await this.ensureUniqueId(table, dbm)
604
- if (this.cfg.immutable) await this.ensureImmutableDoesntExist(table, dbm)
604
+ if (this.cfg.immutable) await this.ensureImmutableDontExist(table, [dbm.id])
605
605
  const op = `save(${dbm.id})`
606
606
  const started = this.logSaveStarted(op, bm, table)
607
607
  await this.cfg.db.saveBatch(table, [dbm], {
@@ -613,12 +613,11 @@ export class CommonDao<
613
613
  return bm as any
614
614
  }
615
615
 
616
- private async ensureImmutableDoesntExist(table: string, dbm: DBM): Promise<void> {
617
- await this.throwIfObjectExists(table, dbm, [
616
+ private async ensureImmutableDontExist(table: string, ids: string[]): Promise<void> {
617
+ await this.throwIfObjectExists(table, ids, [
618
618
  DBLibError.OBJECT_IS_IMMUTABLE,
619
619
  {
620
620
  code: DBLibError.OBJECT_IS_IMMUTABLE,
621
- id: dbm.id,
622
621
  table,
623
622
  },
624
623
  ])
@@ -626,23 +625,27 @@ export class CommonDao<
626
625
 
627
626
  private async ensureUniqueId(table: string, dbm: DBM): Promise<void> {
628
627
  // todo: retry N times
629
- await this.throwIfObjectExists(table, dbm, [
630
- DBLibError.OBJECT_IS_IMMUTABLE,
631
- {
632
- code: DBLibError.OBJECT_IS_IMMUTABLE,
633
- id: dbm.id,
634
- table,
635
- },
636
- ])
628
+ await this.throwIfObjectExists(
629
+ table,
630
+ [dbm.id],
631
+ [
632
+ DBLibError.OBJECT_IS_IMMUTABLE,
633
+ {
634
+ code: DBLibError.OBJECT_IS_IMMUTABLE,
635
+ table,
636
+ },
637
+ ],
638
+ )
637
639
  }
638
640
 
639
641
  private async throwIfObjectExists(
640
642
  table: string,
641
- dbm: DBM,
643
+ ids: string[],
642
644
  errorMeta: [DBLibError, any],
643
645
  ): Promise<void> {
644
- const [existing] = await this.cfg.db.getByIds<DBM>(table, [dbm.id])
645
- if (existing) throw new AppError(errorMeta[0], errorMeta[1])
646
+ const existing = await this.cfg.db.getByIds<DBM>(table, ids)
647
+ if (existing.length > 0)
648
+ throw new AppError(errorMeta[0], { ...errorMeta[1], ids: existing.map(i => i.id) })
646
649
  }
647
650
 
648
651
  /**
@@ -696,7 +699,7 @@ export class CommonDao<
696
699
  this.assignIdCreatedUpdated(dbm, opt) // mutates
697
700
  dbm = this.anyToDBM(dbm, opt)
698
701
  if (opt.ensureUniqueId && idWasGenerated) await this.ensureUniqueId(table, dbm)
699
- if (this.cfg.immutable) await this.ensureImmutableDoesntExist(table, dbm)
702
+ if (this.cfg.immutable) await this.ensureImmutableDontExist(table, [dbm.id])
700
703
  }
701
704
  const op = `saveAsDBM(${dbm.id})`
702
705
  const started = this.logSaveStarted(op, dbm, table)
@@ -715,7 +718,11 @@ export class CommonDao<
715
718
  const dbms = await this.bmsToDBM(bms, opt)
716
719
  if (opt.ensureUniqueId) throw new AppError('ensureUniqueId is not supported in saveBatch')
717
720
  if (this.cfg.immutable)
718
- throw new AppError('immutable DB entries are not supported in saveBatch')
721
+ await this.ensureImmutableDontExist(
722
+ table,
723
+ dbms.map(dbm => dbm.id),
724
+ )
725
+
719
726
  const op = `saveBatch ${dbms.length} row(s) (${_truncate(
720
727
  dbms
721
728
  .slice(0, 10)
@@ -742,7 +749,11 @@ export class CommonDao<
742
749
  dbms.forEach(dbm => this.assignIdCreatedUpdated(dbm, opt)) // mutates
743
750
  dbms = this.anyToDBMs(dbms, opt)
744
751
  if (opt.ensureUniqueId) throw new AppError('ensureUniqueId is not supported in saveBatch')
745
- if (this.cfg.immutable) throw new AppError('immutable objects are not supported in saveBatch')
752
+ if (this.cfg.immutable)
753
+ await this.ensureImmutableDontExist(
754
+ table,
755
+ dbms.map(dbm => dbm.id),
756
+ )
746
757
  }
747
758
  const op = `saveBatchAsDBM ${dbms.length} row(s) (${_truncate(
748
759
  dbms