提问人:Vinay Pandey 提问时间:11/14/2023 更新时间:11/14/2023 访问量:12
在具有 Sequelize-Typescript 的基类中具有通用的钩子和字段
Having common hooks and fields in a base class with Sequelize-Typescript
问:
我有几个具有共同属性和钩子的类。我想把所有的代码放在一个中心类中。
export class CommonFields extends Model<CommonFields> {
@IsDate @CreatedAt @Column
declare createdAt?: Date;
@IsDate @UpdatedAt @Column
declare updatedAt?: Date;
@IsDate @DeletedAt @Column
declare deletedAt?: Date;
@Is(SOME_REGEX_MATCH) @Column(DataType.STRING(10))
modifiedBy?: string;
@BeforeCreate
@BeforeBulkCreate
static setCreateDate(instance: CommonFields): void {
instance.createdAt = getUTCDateTimeNow();
instance.updatedAt = undefined;
instance.deletedAt = undefined;
}
@BeforeDestroy
@BeforeBulkDestroy
static setDeleteDate(instance: CommonFields): void {
instance.deletedAt = getUTCDateTimeNow();
}
@BeforeUpsert
@BeforeUpdate
@BeforeBulkUpdate
static setUpdateDate(instance: CommonFields): void {
instance.updatedAt = getUTCDateTimeNow();
}
}```
I do not want to copy and paste this code across multiple classes. Is there any way of doing this?
The best I have come across is [this post](https://stackoverflow.com/questions/68251335/exteding-base-class-with-some-default-column-in-sequelize-with-type-support-n). However, I am not able to access the common fields or hooks with the method mentioned in the post.
答: 暂无答案
评论