How do you detect if an attribute like password has changed
See original GitHub issueIssue type:
[x] question [ ] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql
/ mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[x ] latest
[ ] @next
[ ] 0.x.x
(or put your version here)
Steps to reproduce or a small repository showing the problem:
I realise that this might be a noob question but i can’t find a reference in the docs. I am trying to a subscriber decorator to hash users password before persisting to the database.
In sequelizejs, I use the following code,
User.hashPassword = (user, options) => {
if (!user.changed('password')) {
return null;
}
// hash password
return Bcrypt.hash(user.get('password'), SALT_ROUNDS)
.then(hash => user.set('password', hash));
};
Right now, my typeorm
translation is roughly
@BeforeInsert()
@BeforeUpdate()
hashPassword() {
// conditional to detect if password has changed goes here
this.password = bcrypt.hashSync(this.password, SALT_ROUNDS);
}
Now, the issue is, I stuck at !user.changed('password')
. Is there an equivalent function in typeorm to do this without rolling out my own solution?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:31 (2 by maintainers)
just to update this thread
you can make a something like this
@PrimaryColumn() public username: string;