@naturalcycles/db-lib 9.3.0 → 9.3.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.
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { Transform } from 'node:stream';
3
- import { AnyObject, AsyncMapper, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, PartialObjectWithId, Saved, UnixTimestampMillisNumber, ZodSchema } from '@naturalcycles/js-lib';
3
+ import { AnyObject, AsyncMapper, BaseDBEntity, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, PartialObjectWithId, Saved, UnixTimestampMillisNumber, ZodSchema } from '@naturalcycles/js-lib';
4
4
  import { AjvSchema, ObjectSchema, ReadableTyped } from '@naturalcycles/nodejs-lib';
5
5
  import { CommonDBTransactionOptions, DBModelType, DBPatch, DBTransaction, RunQueryResult } from '../db.model';
6
6
  import { DBQuery, RunnableDBQuery } from '../query/dbQuery';
@@ -82,8 +82,7 @@ export declare class CommonDao<BM extends PartialObjectWithId, DBM extends Parti
82
82
  * Mutates!
83
83
  * "Returns", just to have a type of "Saved"
84
84
  */
85
- assignIdCreatedUpdated(obj: Partial<DBM>, opt?: CommonDaoOptions): Saved<Partial<DBM>>;
86
- assignIdCreatedUpdated(obj: Partial<BM>, opt?: CommonDaoOptions): Saved<Partial<BM>>;
85
+ assignIdCreatedUpdated<T extends BaseDBEntity>(obj: T, opt?: CommonDaoOptions): Saved<T>;
87
86
  /**
88
87
  * Mutates with id, created, updated
89
88
  */
@@ -479,16 +479,17 @@ class CommonDao {
479
479
  this.cfg.logger?.log(`<< ${q.table}.${op}: ${count} id(s) in ${(0, js_lib_1._since)(started)}`);
480
480
  }
481
481
  }
482
+ /**
483
+ * Mutates!
484
+ * "Returns", just to have a type of "Saved"
485
+ */
482
486
  assignIdCreatedUpdated(obj, opt = {}) {
483
487
  const now = Math.floor(Date.now() / 1000);
484
488
  if (this.cfg.useCreatedProperty) {
485
- ;
486
- obj['created'] ||= obj['updated'] || now;
489
+ obj.created ||= obj.updated || now;
487
490
  }
488
491
  if (this.cfg.useUpdatedProperty) {
489
- ;
490
- obj['updated'] =
491
- opt.preserveUpdatedCreated && obj['updated'] ? obj['updated'] : now;
492
+ obj.updated = opt.preserveUpdatedCreated && obj.updated ? obj.updated : now;
492
493
  }
493
494
  if (this.cfg.createId) {
494
495
  obj.id ||= this.cfg.hooks.createNaturalId?.(obj) || this.cfg.hooks.createRandomId();
@@ -1,4 +1,4 @@
1
- import { CommonLogger, ErrorMode, PartialObjectWithId, Promisable, Saved, ZodError, ZodSchema } from '@naturalcycles/js-lib';
1
+ import { AnyObject, CommonLogger, ErrorMode, PartialObjectWithId, Promisable, Saved, ZodError, ZodSchema } from '@naturalcycles/js-lib';
2
2
  import { AjvSchema, AjvValidationError, JoiValidationError, ObjectSchema, TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib';
3
3
  import { CommonDB } from '../common.db';
4
4
  import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model';
@@ -104,7 +104,7 @@ export declare enum CommonDaoLogLevel {
104
104
  */
105
105
  DATA_FULL = 30
106
106
  }
107
- export interface CommonDaoCfg<BM extends PartialObjectWithId, DBM extends PartialObjectWithId = BM, TM = BM> {
107
+ export interface CommonDaoCfg<BM extends PartialObjectWithId, DBM extends PartialObjectWithId = BM, TM extends AnyObject = BM> {
108
108
  db: CommonDB;
109
109
  table: string;
110
110
  /**
package/package.json CHANGED
@@ -40,7 +40,7 @@
40
40
  "engines": {
41
41
  "node": ">=18.12"
42
42
  },
43
- "version": "9.3.0",
43
+ "version": "9.3.1",
44
44
  "description": "Lowest Common Denominator API to supported Databases",
45
45
  "keywords": [
46
46
  "db",
@@ -1,4 +1,5 @@
1
1
  import {
2
+ AnyObject,
2
3
  CommonLogger,
3
4
  ErrorMode,
4
5
  PartialObjectWithId,
@@ -138,7 +139,7 @@ export enum CommonDaoLogLevel {
138
139
  export interface CommonDaoCfg<
139
140
  BM extends PartialObjectWithId,
140
141
  DBM extends PartialObjectWithId = BM,
141
- TM = BM,
142
+ TM extends AnyObject = BM,
142
143
  > {
143
144
  db: CommonDB
144
145
  table: string
@@ -13,6 +13,7 @@ import {
13
13
  AnyObject,
14
14
  AppError,
15
15
  AsyncMapper,
16
+ BaseDBEntity,
16
17
  CommonLogger,
17
18
  ErrorMode,
18
19
  JsonSchemaObject,
@@ -116,7 +117,7 @@ export class CommonDao<
116
117
  create(part: Partial<BM> = {}, opt: CommonDaoOptions = {}): Saved<BM> {
117
118
  const bm = this.cfg.hooks!.beforeCreate!(part)
118
119
  // First assignIdCreatedUpdated, then validate!
119
- this.assignIdCreatedUpdated(bm as BM, opt)
120
+ this.assignIdCreatedUpdated(bm, opt)
120
121
  return this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
121
122
  }
122
123
 
@@ -674,28 +675,22 @@ export class CommonDao<
674
675
  * Mutates!
675
676
  * "Returns", just to have a type of "Saved"
676
677
  */
677
- assignIdCreatedUpdated(obj: Partial<DBM>, opt?: CommonDaoOptions): Saved<Partial<DBM>>
678
- assignIdCreatedUpdated(obj: Partial<BM>, opt?: CommonDaoOptions): Saved<Partial<BM>>
679
- assignIdCreatedUpdated(
680
- obj: Partial<DBM> | Partial<BM>,
681
- opt: CommonDaoOptions = {},
682
- ): Saved<Partial<DBM>> | Saved<Partial<BM>> {
678
+ assignIdCreatedUpdated<T extends BaseDBEntity>(obj: T, opt: CommonDaoOptions = {}): Saved<T> {
683
679
  const now = Math.floor(Date.now() / 1000)
684
680
 
685
681
  if (this.cfg.useCreatedProperty) {
686
- ;(obj as any)['created'] ||= (obj as any)['updated'] || now
682
+ obj.created ||= obj.updated || now
687
683
  }
688
684
 
689
685
  if (this.cfg.useUpdatedProperty) {
690
- ;(obj as any)['updated'] =
691
- opt.preserveUpdatedCreated && (obj as any)['updated'] ? (obj as any)['updated'] : now
686
+ obj.updated = opt.preserveUpdatedCreated && obj.updated ? obj.updated : now
692
687
  }
693
688
 
694
689
  if (this.cfg.createId) {
695
- obj.id ||= this.cfg.hooks!.createNaturalId?.(obj as BM) || this.cfg.hooks!.createRandomId!()
690
+ obj.id ||= this.cfg.hooks!.createNaturalId?.(obj as any) || this.cfg.hooks!.createRandomId!()
696
691
  }
697
692
 
698
- return obj as any
693
+ return obj as Saved<T>
699
694
  }
700
695
 
701
696
  // SAVE