每次在typeorm中更改密码时,我都尝试哈希密码
@Column()
password: string;
@BeforeInsert()
async hashPassword() {
this.password = await hash(this.password, 12);
}
我如何在每次像这样更改时将isModified功能从typeorm中的mongoose复制到hash password:
if (this.password.isModified()) return next();
this.password = await hash(this.password, 12);
如果您的密码被修改,您将需要在对象中进行设置,以传递给存储库的save方法。如果实体的id相同且某些属性已更改,Save方法会更新该实体。因此您只需要在@entity
中使用@beforeupdate
。
@BeforeUpdate()
private hashPassword(): void {
this.password = myHashMethod(this.password);
}