Closed tejo-ak closed 3 years ago
This does sound useful. I'll get to it soon
Published in 1.7.0
, docs can be found here.
Let me know if there is anything that's missing.
Perfect! Thanks a lot for considering the suggestion and implementing it.
Hello, I tested the new version with the hook. It works like a charm. I noticed that you put the hook after schema validation. Since the hook can change the data after being validated, the validation is too early and may not be very useful. Also, one of the purposes of using the hook was to set a primary key (id). Therefore my data which has no id when initialized failed the validation. (I have to set my id 'required' false).
That makes sense. Do you think that beforeCreate
should run before the validation, or should a new hook beforeValidate
be added?
Like with sequelize hooks, where beforeValidate
runs, then the instance is validated, and then beforeCreate
runs.
It works for me.
Making the Sequelize as a reference is a good move.
Thanks for your input.
I tested beforeValidate/afterValidate
hooks, and wasn't happy with the result. It added complexity, while making some things unclear. Also, I'm not in favor of mutating data in the beforeValidate
hook.
Therefore, I implemented the original concept of beforeCreate
running before validating the instance.
Published in 1.8.1
.
This solution should solve your issue, let me know if anything doesn't go as planned!
I want to manage id generation across different Models consistently. Is it possible to add a new hook right before a record is saved to the database?
Thanks.