@BeforeUpdate Not called when using Model.update(id, args)
See original GitHub issueIssue type:
[ ] question [x] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql
/ mariadb
[ ] oracle
[x] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x
(or put your version here)
Steps to reproduce or a small repository showing the problem:
Hi. I’ve got a function with the @BeforeUpdate
decorator that is not being called when I use Model.update(id, args)
My model is extending from BaseEntity
Is this expected? On the listeners-subscribers documentation it says that @BeforeUpdate
gets called on .save(), shouldn’t it be also called on .update()?
Repo: https://github.com/serranoarevalo/nuber/tree/33d2e729a36c8fb08d721a5453e5c2d03d24e8d4
Example of the GraphQL Resolver:
updateUser: async (parent, args, { entities: { User } }) => {
try {
await User.update(args.id, args);
return true;
} catch (error) {
console.log(error);
return false;
}
}
Example of the model
@Entity()
class User extends BaseEntity {
...
@BeforeUpdate()
async updatePassword(): Promise<void> {
if (this.password) {
const hashedPassword = await this.hashPassword(this.password);
this.password = hashedPassword;
}
}
}
Anyways thanks for the great work and for looking into this!
N.
Issue Analytics
- State:
- Created 5 years ago
- Comments:13 (3 by maintainers)
Top GitHub Comments
To make it easier there is Object.assign
In the documentation it says that updates must be made directly to the model https://github.com/typeorm/typeorm/blob/master/docs/listeners-and-subscribers.md#beforeupdate
alternatively, I used the following (T = User)