我想使用sequelize-typescript. 这是一个可以在 PostgreSQL、SQLite 和 MySQL 中工作的基本模型。
class MyModel extends Model<MyModel> {
@Column({
type: DataType.JSON
})
options!: string[]
}
Run Code Online (Sandbox Code Playgroud)
为了使用 MariaDB 获得相同的行为,我尝试了四件事:
@AfterFind或其他一些钩子。问题:正如github repo中的这个问题所述,有一个已知的错误,即钩子不会在包含的模型上触发,所以没有机会
问题:即使复制sequelize-docs 中的示例也会引发各种打字稿编译器问题,即使我使用any类型并欺骗 TS 编译器,我也不知道如何告诉我的其他模型有一个新的 DataType。
class MyModel extends Model<MyModel> {
@Column({
type: DataType.STRING
})
get options(): string[] {
return JSON.parse(this.getDataValue('options'))
}
set options(value: string[]) {
this.setDataValue('options', JSON.stringify(value))
}
}
Run Code Online (Sandbox Code Playgroud)
这会引发getDataValue和 …