@naturalcycles/db-lib 8.40.0 → 8.40.1

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.
@@ -462,11 +462,16 @@ class CommonDao {
462
462
  }
463
463
  const op = `save(${dbm.id})`;
464
464
  const started = this.logSaveStarted(op, bm, table);
465
+ const { excludeFromIndexes } = this.cfg;
466
+ const assignGeneratedIds = opt.assignGeneratedIds || this.cfg.assignGeneratedIds;
465
467
  await this.cfg.db.saveBatch(table, [dbm], {
466
- excludeFromIndexes: this.cfg.excludeFromIndexes,
467
- assignGeneratedIds: this.cfg.assignGeneratedIds,
468
+ excludeFromIndexes,
469
+ assignGeneratedIds,
468
470
  ...opt,
469
471
  });
472
+ if (assignGeneratedIds) {
473
+ bm.id = dbm.id;
474
+ }
470
475
  this.logSaveResult(started, op, table);
471
476
  return bm;
472
477
  }
@@ -497,25 +502,31 @@ class CommonDao {
497
502
  const table = opt.table || this.cfg.table;
498
503
  // assigning id in case it misses the id
499
504
  // will override/set `updated` field, unless opts.preserveUpdated is set
505
+ let row = dbm;
500
506
  if (!opt.raw) {
501
507
  const idWasGenerated = !dbm.id && this.cfg.createId;
502
508
  this.assignIdCreatedUpdated(dbm, opt); // mutates
503
- dbm = this.anyToDBM(dbm, opt);
509
+ row = this.anyToDBM(dbm, opt);
504
510
  if (opt.ensureUniqueId && idWasGenerated)
505
- await this.ensureUniqueId(table, dbm);
511
+ await this.ensureUniqueId(table, row);
506
512
  }
507
513
  if (this.cfg.immutable && !opt.allowMutability && !opt.saveMethod) {
508
514
  opt = { ...opt, saveMethod: 'insert' };
509
515
  }
510
- const op = `saveAsDBM(${dbm.id})`;
511
- const started = this.logSaveStarted(op, dbm, table);
512
- await this.cfg.db.saveBatch(table, [dbm], {
513
- excludeFromIndexes: this.cfg.excludeFromIndexes,
514
- assignGeneratedIds: this.cfg.assignGeneratedIds,
516
+ const op = `saveAsDBM(${row.id})`;
517
+ const started = this.logSaveStarted(op, row, table);
518
+ const { excludeFromIndexes } = this.cfg;
519
+ const assignGeneratedIds = opt.assignGeneratedIds || this.cfg.assignGeneratedIds;
520
+ await this.cfg.db.saveBatch(table, [row], {
521
+ excludeFromIndexes,
522
+ assignGeneratedIds,
515
523
  ...opt,
516
524
  });
525
+ if (assignGeneratedIds) {
526
+ dbm.id = row.id;
527
+ }
517
528
  this.logSaveResult(started, op, table);
518
- return dbm;
529
+ return row;
519
530
  }
520
531
  async saveBatch(bms, opt = {}) {
521
532
  this.requireWriteAccess();
@@ -532,38 +543,49 @@ class CommonDao {
532
543
  .map(bm => bm.id)
533
544
  .join(', '), 50)})`;
534
545
  const started = this.logSaveStarted(op, bms, table);
546
+ const { excludeFromIndexes } = this.cfg;
547
+ const assignGeneratedIds = opt.assignGeneratedIds || this.cfg.assignGeneratedIds;
535
548
  await this.cfg.db.saveBatch(table, dbms, {
536
- excludeFromIndexes: this.cfg.excludeFromIndexes,
537
- assignGeneratedIds: this.cfg.assignGeneratedIds,
549
+ excludeFromIndexes,
550
+ assignGeneratedIds,
538
551
  ...opt,
539
552
  });
553
+ if (assignGeneratedIds) {
554
+ dbms.forEach((dbm, i) => (bms[i].id = dbm.id));
555
+ }
540
556
  this.logSaveResult(started, op, table);
541
557
  return bms;
542
558
  }
543
559
  async saveBatchAsDBM(dbms, opt = {}) {
544
560
  this.requireWriteAccess();
545
561
  const table = opt.table || this.cfg.table;
562
+ let rows = dbms;
546
563
  if (!opt.raw) {
547
564
  dbms.forEach(dbm => this.assignIdCreatedUpdated(dbm, opt)); // mutates
548
- dbms = this.anyToDBMs(dbms, opt);
565
+ rows = this.anyToDBMs(dbms, opt);
549
566
  if (opt.ensureUniqueId)
550
567
  throw new js_lib_1.AppError('ensureUniqueId is not supported in saveBatch');
551
568
  }
552
569
  if (this.cfg.immutable && !opt.allowMutability && !opt.saveMethod) {
553
570
  opt = { ...opt, saveMethod: 'insert' };
554
571
  }
555
- const op = `saveBatchAsDBM ${dbms.length} row(s) (${(0, js_lib_1._truncate)(dbms
572
+ const op = `saveBatchAsDBM ${rows.length} row(s) (${(0, js_lib_1._truncate)(rows
556
573
  .slice(0, 10)
557
574
  .map(bm => bm.id)
558
575
  .join(', '), 50)})`;
559
- const started = this.logSaveStarted(op, dbms, table);
560
- await this.cfg.db.saveBatch(table, dbms, {
561
- excludeFromIndexes: this.cfg.excludeFromIndexes,
562
- assignGeneratedIds: this.cfg.assignGeneratedIds,
576
+ const started = this.logSaveStarted(op, rows, table);
577
+ const { excludeFromIndexes } = this.cfg;
578
+ const assignGeneratedIds = opt.assignGeneratedIds || this.cfg.assignGeneratedIds;
579
+ await this.cfg.db.saveBatch(table, rows, {
580
+ excludeFromIndexes,
581
+ assignGeneratedIds,
563
582
  ...opt,
564
583
  });
584
+ if (assignGeneratedIds) {
585
+ rows.forEach((row, i) => (dbms[i].id = row.id));
586
+ }
565
587
  this.logSaveResult(started, op, table);
566
- return dbms;
588
+ return rows;
567
589
  }
568
590
  async deleteById(id, opt = {}) {
569
591
  if (!id)
package/package.json CHANGED
@@ -42,7 +42,7 @@
42
42
  "engines": {
43
43
  "node": ">=14.15"
44
44
  },
45
- "version": "8.40.0",
45
+ "version": "8.40.1",
46
46
  "description": "Lowest Common Denominator API to supported Databases",
47
47
  "keywords": [
48
48
  "db",
@@ -620,12 +620,18 @@ export class CommonDao<
620
620
  }
621
621
  const op = `save(${dbm.id})`
622
622
  const started = this.logSaveStarted(op, bm, table)
623
+ const { excludeFromIndexes } = this.cfg
624
+ const assignGeneratedIds = opt.assignGeneratedIds || this.cfg.assignGeneratedIds
623
625
  await this.cfg.db.saveBatch(table, [dbm], {
624
- excludeFromIndexes: this.cfg.excludeFromIndexes,
625
- assignGeneratedIds: this.cfg.assignGeneratedIds,
626
+ excludeFromIndexes,
627
+ assignGeneratedIds,
626
628
  ...opt,
627
629
  })
628
630
 
631
+ if (assignGeneratedIds) {
632
+ bm.id = dbm.id as any
633
+ }
634
+
629
635
  this.logSaveResult(started, op, table)
630
636
  return bm as any
631
637
  }
@@ -668,24 +674,32 @@ export class CommonDao<
668
674
 
669
675
  // assigning id in case it misses the id
670
676
  // will override/set `updated` field, unless opts.preserveUpdated is set
677
+ let row = dbm
671
678
  if (!opt.raw) {
672
679
  const idWasGenerated = !dbm.id && this.cfg.createId
673
680
  this.assignIdCreatedUpdated(dbm, opt) // mutates
674
- dbm = this.anyToDBM(dbm, opt)
675
- if (opt.ensureUniqueId && idWasGenerated) await this.ensureUniqueId(table, dbm)
681
+ row = this.anyToDBM(dbm, opt)
682
+ if (opt.ensureUniqueId && idWasGenerated) await this.ensureUniqueId(table, row)
676
683
  }
677
684
  if (this.cfg.immutable && !opt.allowMutability && !opt.saveMethod) {
678
685
  opt = { ...opt, saveMethod: 'insert' }
679
686
  }
680
- const op = `saveAsDBM(${dbm.id})`
681
- const started = this.logSaveStarted(op, dbm, table)
682
- await this.cfg.db.saveBatch(table, [dbm], {
683
- excludeFromIndexes: this.cfg.excludeFromIndexes,
684
- assignGeneratedIds: this.cfg.assignGeneratedIds,
687
+ const op = `saveAsDBM(${row.id})`
688
+ const started = this.logSaveStarted(op, row, table)
689
+ const { excludeFromIndexes } = this.cfg
690
+ const assignGeneratedIds = opt.assignGeneratedIds || this.cfg.assignGeneratedIds
691
+ await this.cfg.db.saveBatch(table, [row], {
692
+ excludeFromIndexes,
693
+ assignGeneratedIds,
685
694
  ...opt,
686
695
  })
696
+
697
+ if (assignGeneratedIds) {
698
+ dbm.id = row.id
699
+ }
700
+
687
701
  this.logSaveResult(started, op, table)
688
- return dbm
702
+ return row
689
703
  }
690
704
 
691
705
  async saveBatch(bms: Unsaved<BM>[], opt: CommonDaoSaveOptions<DBM> = {}): Promise<Saved<BM>[]> {
@@ -706,13 +720,19 @@ export class CommonDao<
706
720
  50,
707
721
  )})`
708
722
  const started = this.logSaveStarted(op, bms, table)
723
+ const { excludeFromIndexes } = this.cfg
724
+ const assignGeneratedIds = opt.assignGeneratedIds || this.cfg.assignGeneratedIds
709
725
 
710
726
  await this.cfg.db.saveBatch(table, dbms, {
711
- excludeFromIndexes: this.cfg.excludeFromIndexes,
712
- assignGeneratedIds: this.cfg.assignGeneratedIds,
727
+ excludeFromIndexes,
728
+ assignGeneratedIds,
713
729
  ...opt,
714
730
  })
715
731
 
732
+ if (assignGeneratedIds) {
733
+ dbms.forEach((dbm, i) => (bms[i]!.id = dbm.id as any))
734
+ }
735
+
716
736
  this.logSaveResult(started, op, table)
717
737
 
718
738
  return bms as any[]
@@ -721,31 +741,38 @@ export class CommonDao<
721
741
  async saveBatchAsDBM(dbms: DBM[], opt: CommonDaoSaveOptions<DBM> = {}): Promise<DBM[]> {
722
742
  this.requireWriteAccess()
723
743
  const table = opt.table || this.cfg.table
744
+ let rows = dbms
724
745
  if (!opt.raw) {
725
746
  dbms.forEach(dbm => this.assignIdCreatedUpdated(dbm, opt)) // mutates
726
- dbms = this.anyToDBMs(dbms, opt)
747
+ rows = this.anyToDBMs(dbms, opt)
727
748
  if (opt.ensureUniqueId) throw new AppError('ensureUniqueId is not supported in saveBatch')
728
749
  }
729
750
  if (this.cfg.immutable && !opt.allowMutability && !opt.saveMethod) {
730
751
  opt = { ...opt, saveMethod: 'insert' }
731
752
  }
732
- const op = `saveBatchAsDBM ${dbms.length} row(s) (${_truncate(
733
- dbms
753
+ const op = `saveBatchAsDBM ${rows.length} row(s) (${_truncate(
754
+ rows
734
755
  .slice(0, 10)
735
756
  .map(bm => bm.id)
736
757
  .join(', '),
737
758
  50,
738
759
  )})`
739
- const started = this.logSaveStarted(op, dbms, table)
760
+ const started = this.logSaveStarted(op, rows, table)
761
+ const { excludeFromIndexes } = this.cfg
762
+ const assignGeneratedIds = opt.assignGeneratedIds || this.cfg.assignGeneratedIds
740
763
 
741
- await this.cfg.db.saveBatch(table, dbms, {
742
- excludeFromIndexes: this.cfg.excludeFromIndexes,
743
- assignGeneratedIds: this.cfg.assignGeneratedIds,
764
+ await this.cfg.db.saveBatch(table, rows, {
765
+ excludeFromIndexes,
766
+ assignGeneratedIds,
744
767
  ...opt,
745
768
  })
746
769
 
770
+ if (assignGeneratedIds) {
771
+ rows.forEach((row, i) => (dbms[i]!.id = row.id))
772
+ }
773
+
747
774
  this.logSaveResult(started, op, table)
748
- return dbms
775
+ return rows
749
776
  }
750
777
 
751
778
  // DELETE