Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit 7a2c15c

Browse files
fix: types
1 parent 16742f2 commit 7a2c15c

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

src/commondao/common.dao.model.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
AnyObject,
23
CommonLogger,
34
ErrorMode,
45
PartialObjectWithId,
@@ -138,7 +139,7 @@ export enum CommonDaoLogLevel {
138139
export interface CommonDaoCfg<
139140
BM extends PartialObjectWithId,
140141
DBM extends PartialObjectWithId = BM,
141-
TM = BM,
142+
TM extends AnyObject = BM,
142143
> {
143144
db: CommonDB
144145
table: string

src/commondao/common.dao.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
AnyObject,
1414
AppError,
1515
AsyncMapper,
16+
BaseDBEntity,
1617
CommonLogger,
1718
ErrorMode,
1819
JsonSchemaObject,
@@ -116,7 +117,7 @@ export class CommonDao<
116117
create(part: Partial<BM> = {}, opt: CommonDaoOptions = {}): Saved<BM> {
117118
const bm = this.cfg.hooks!.beforeCreate!(part)
118119
// First assignIdCreatedUpdated, then validate!
119-
this.assignIdCreatedUpdated(bm as BM, opt)
120+
this.assignIdCreatedUpdated(bm, opt)
120121
return this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
121122
}
122123

@@ -674,28 +675,22 @@ export class CommonDao<
674675
* Mutates!
675676
* "Returns", just to have a type of "Saved"
676677
*/
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> {
683679
const now = Math.floor(Date.now() / 1000)
684680

685681
if (this.cfg.useCreatedProperty) {
686-
;(obj as any)['created'] ||= (obj as any)['updated'] || now
682+
obj.created ||= obj.updated || now
687683
}
688684

689685
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
692687
}
693688

694689
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!()
696691
}
697692

698-
return obj as any
693+
return obj as Saved<T>
699694
}
700695

701696
// SAVE

0 commit comments

Comments
 (0)