@naturalcycles/db-lib 8.39.0 → 8.40.0

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.
@@ -28,6 +28,7 @@ class CommonDao {
28
28
  logLevel: isGAE || isCI ? common_dao_model_1.CommonDaoLogLevel.NONE : common_dao_model_1.CommonDaoLogLevel.OPERATIONS,
29
29
  idType: 'string',
30
30
  createId: true,
31
+ assignGeneratedIds: false,
31
32
  created: true,
32
33
  updated: true,
33
34
  logger: console,
@@ -450,7 +451,7 @@ class CommonDao {
450
451
  */
451
452
  async save(bm, opt = {}) {
452
453
  this.requireWriteAccess();
453
- const idWasGenerated = !bm.id;
454
+ const idWasGenerated = !bm.id && this.cfg.createId;
454
455
  this.assignIdCreatedUpdated(bm, opt); // mutates
455
456
  const dbm = await this.bmToDBM(bm, opt);
456
457
  const table = opt.table || this.cfg.table;
@@ -463,6 +464,7 @@ class CommonDao {
463
464
  const started = this.logSaveStarted(op, bm, table);
464
465
  await this.cfg.db.saveBatch(table, [dbm], {
465
466
  excludeFromIndexes: this.cfg.excludeFromIndexes,
467
+ assignGeneratedIds: this.cfg.assignGeneratedIds,
466
468
  ...opt,
467
469
  });
468
470
  this.logSaveResult(started, op, table);
@@ -496,7 +498,7 @@ class CommonDao {
496
498
  // assigning id in case it misses the id
497
499
  // will override/set `updated` field, unless opts.preserveUpdated is set
498
500
  if (!opt.raw) {
499
- const idWasGenerated = !dbm.id;
501
+ const idWasGenerated = !dbm.id && this.cfg.createId;
500
502
  this.assignIdCreatedUpdated(dbm, opt); // mutates
501
503
  dbm = this.anyToDBM(dbm, opt);
502
504
  if (opt.ensureUniqueId && idWasGenerated)
@@ -509,6 +511,7 @@ class CommonDao {
509
511
  const started = this.logSaveStarted(op, dbm, table);
510
512
  await this.cfg.db.saveBatch(table, [dbm], {
511
513
  excludeFromIndexes: this.cfg.excludeFromIndexes,
514
+ assignGeneratedIds: this.cfg.assignGeneratedIds,
512
515
  ...opt,
513
516
  });
514
517
  this.logSaveResult(started, op, table);
@@ -531,6 +534,7 @@ class CommonDao {
531
534
  const started = this.logSaveStarted(op, bms, table);
532
535
  await this.cfg.db.saveBatch(table, dbms, {
533
536
  excludeFromIndexes: this.cfg.excludeFromIndexes,
537
+ assignGeneratedIds: this.cfg.assignGeneratedIds,
534
538
  ...opt,
535
539
  });
536
540
  this.logSaveResult(started, op, table);
@@ -555,6 +559,7 @@ class CommonDao {
555
559
  const started = this.logSaveStarted(op, dbms, table);
556
560
  await this.cfg.db.saveBatch(table, dbms, {
557
561
  excludeFromIndexes: this.cfg.excludeFromIndexes,
562
+ assignGeneratedIds: this.cfg.assignGeneratedIds,
558
563
  ...opt,
559
564
  });
560
565
  this.logSaveResult(started, op, table);
@@ -86,6 +86,11 @@ export interface CommonDaoCfg<BM extends Partial<ObjectWithId<ID>>, DBM extends
86
86
  * Useful e.g when your DB is generating ids by itself (e.g mysql auto_increment).
87
87
  */
88
88
  createId?: boolean;
89
+ /**
90
+ * See the same option in CommonDB.
91
+ * Defaults to false normally.
92
+ */
93
+ assignGeneratedIds?: boolean;
89
94
  /**
90
95
  * Defaults to true
91
96
  * Set to false to disable `created` field management.
@@ -19,6 +19,13 @@ export interface CommonDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObje
19
19
  * Default is `upsert`
20
20
  */
21
21
  saveMethod?: CommonDBSaveMethod;
22
+ /**
23
+ * Only applicable to tables where id is "auto-generated by DB", e.g `auto_increment` in MySQL.
24
+ * By default it's false, so, auto-generated id will NOT be assigned/returned.
25
+ * Setting it to true will assign and return auto-generated id (on all rows, one by one).
26
+ * It's not true by default, because getting auto-generated id incurs an overhead of doing extra call (e.g LAST_INSERT_ID() in MySQL).
27
+ */
28
+ assignGeneratedIds?: boolean;
22
29
  }
23
30
  export declare type CommonDBStreamOptions = CommonDBOptions;
24
31
  export interface CommonDBCreateOptions extends CommonDBOptions {
package/package.json CHANGED
@@ -42,7 +42,7 @@
42
42
  "engines": {
43
43
  "node": ">=14.15"
44
44
  },
45
- "version": "8.39.0",
45
+ "version": "8.40.0",
46
46
  "description": "Lowest Common Denominator API to supported Databases",
47
47
  "keywords": [
48
48
  "db",
@@ -119,6 +119,12 @@ export interface CommonDaoCfg<
119
119
  */
120
120
  createId?: boolean
121
121
 
122
+ /**
123
+ * See the same option in CommonDB.
124
+ * Defaults to false normally.
125
+ */
126
+ assignGeneratedIds?: boolean
127
+
122
128
  /**
123
129
  * Defaults to true
124
130
  * Set to false to disable `created` field management.
@@ -73,6 +73,7 @@ export class CommonDao<
73
73
  logLevel: isGAE || isCI ? CommonDaoLogLevel.NONE : CommonDaoLogLevel.OPERATIONS,
74
74
  idType: 'string',
75
75
  createId: true,
76
+ assignGeneratedIds: false,
76
77
  created: true,
77
78
  updated: true,
78
79
  logger: console,
@@ -609,7 +610,7 @@ export class CommonDao<
609
610
  */
610
611
  async save(bm: Unsaved<BM>, opt: CommonDaoSaveOptions<DBM> = {}): Promise<Saved<BM>> {
611
612
  this.requireWriteAccess()
612
- const idWasGenerated = !bm.id
613
+ const idWasGenerated = !bm.id && this.cfg.createId
613
614
  this.assignIdCreatedUpdated(bm, opt) // mutates
614
615
  const dbm = await this.bmToDBM(bm as BM, opt)
615
616
  const table = opt.table || this.cfg.table
@@ -621,6 +622,7 @@ export class CommonDao<
621
622
  const started = this.logSaveStarted(op, bm, table)
622
623
  await this.cfg.db.saveBatch(table, [dbm], {
623
624
  excludeFromIndexes: this.cfg.excludeFromIndexes,
625
+ assignGeneratedIds: this.cfg.assignGeneratedIds,
624
626
  ...opt,
625
627
  })
626
628
 
@@ -667,7 +669,7 @@ export class CommonDao<
667
669
  // assigning id in case it misses the id
668
670
  // will override/set `updated` field, unless opts.preserveUpdated is set
669
671
  if (!opt.raw) {
670
- const idWasGenerated = !dbm.id
672
+ const idWasGenerated = !dbm.id && this.cfg.createId
671
673
  this.assignIdCreatedUpdated(dbm, opt) // mutates
672
674
  dbm = this.anyToDBM(dbm, opt)
673
675
  if (opt.ensureUniqueId && idWasGenerated) await this.ensureUniqueId(table, dbm)
@@ -679,6 +681,7 @@ export class CommonDao<
679
681
  const started = this.logSaveStarted(op, dbm, table)
680
682
  await this.cfg.db.saveBatch(table, [dbm], {
681
683
  excludeFromIndexes: this.cfg.excludeFromIndexes,
684
+ assignGeneratedIds: this.cfg.assignGeneratedIds,
682
685
  ...opt,
683
686
  })
684
687
  this.logSaveResult(started, op, table)
@@ -706,6 +709,7 @@ export class CommonDao<
706
709
 
707
710
  await this.cfg.db.saveBatch(table, dbms, {
708
711
  excludeFromIndexes: this.cfg.excludeFromIndexes,
712
+ assignGeneratedIds: this.cfg.assignGeneratedIds,
709
713
  ...opt,
710
714
  })
711
715
 
@@ -736,6 +740,7 @@ export class CommonDao<
736
740
 
737
741
  await this.cfg.db.saveBatch(table, dbms, {
738
742
  excludeFromIndexes: this.cfg.excludeFromIndexes,
743
+ assignGeneratedIds: this.cfg.assignGeneratedIds,
739
744
  ...opt,
740
745
  })
741
746
 
package/src/db.model.ts CHANGED
@@ -24,6 +24,14 @@ export interface CommonDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObje
24
24
  * Default is `upsert`
25
25
  */
26
26
  saveMethod?: CommonDBSaveMethod
27
+
28
+ /**
29
+ * Only applicable to tables where id is "auto-generated by DB", e.g `auto_increment` in MySQL.
30
+ * By default it's false, so, auto-generated id will NOT be assigned/returned.
31
+ * Setting it to true will assign and return auto-generated id (on all rows, one by one).
32
+ * It's not true by default, because getting auto-generated id incurs an overhead of doing extra call (e.g LAST_INSERT_ID() in MySQL).
33
+ */
34
+ assignGeneratedIds?: boolean
27
35
  }
28
36
 
29
37
  export type CommonDBStreamOptions = CommonDBOptions