-
Notifications
You must be signed in to change notification settings - Fork 286
A @Hook
decorator would be great
#88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@natesilva Currently there is no implementation of hook decorators in progress from my side. But yeah, sounds great. I would be pleased to see this implemented :) |
I was thinking the argument to |
Perfect :) Can I assign this feature to you? |
Yes. |
This works, but there are no tests yet. (yeah I know) Typical usage would be: @Table
class Person extends Model<Person> {
@Column
name: string;
@BeforeUpdate
@BeforeCreate
static capitalizeName(instance: Person) {
instance.name = instance.name.toLocaleUpperCase();
}
@BeforeCreate
static addUnicorn(instance: Person) {
instance.name += ' 🦄';
}
} If a hook decorator is applied to a non-static method, an informative error will be thrown. It’s possible to name a hook: |
The After tests are written I’ll submit this PR. |
Looks great! 🎉 |
Hi do we really need to pass the instance? Can't it be just "this"? |
The context of a static function is the reference to the class itself. Therefore |
The arguments depend on which hook you are implementing. For some hooks Sequelize passes an instance, for others an array of instances ( I can imagine a future enhancement where the subset of hooks that receive a single instance create a wrapper function to be the actual hook, which then calls a method on the instance -- allowing you to use |
There are several ways to add hooks, but they need your
Model
to be added to a Sequelize instance first. It isn’t very declarative.It would be great if there was a
@Hook
decorator. Something like:Is this in the works? I could try to write something. The decorator would store information using the
reflect-metadata
system and then something likeinitializeHooks
would be called from theaddModels
method.The text was updated successfully, but these errors were encountered: