I'm submitting a ...
Doc request
I know how to write a Virtual field in sequelize, but not clear for sequelize-typescript class model
All I can find are two issues:
https://github.com/RobinBuschmann/sequelize-typescript/issues/663
https://github.com/RobinBuschmann/sequelize-typescript/issues/701
But still not very clear about:
DataType.VIRTUAL(DataType.STRING, ['first_name', 'last_name']))So, maybe write some small examples for VIRTUAL fields in doc would help people
Hey @zhangciwu , this is an example that worked for me, for setting a "transient" field in a sequelize Model instance.
@Table({ modelName: 'application' })
export class Application extends Model<Application> {
@Column(DataType.VIRTUAL(DataType.STRING))
get clientSecret(): string {
return this.getDataValue('clientSecret')
}
set clientSecret(value: string) {
this.setDataValue('clientSecret', value)
}
}
Then, the field could be populated like so:
const application: Application | null = await Application.findOne({where: {id}})
if(application === null) {
throw new Error(`application id ${id} not found`)
}
const clientSecret: string = await getClientSecret(application)
application.clientSecret = clientSecret
return application
Cheers~!
Most helpful comment
Hey @zhangciwu , this is an example that worked for me, for setting a "transient" field in a sequelize Model instance.
Then, the field could be populated like so:
Cheers~!