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
Add association hook #9590
Add association hook #9590
Conversation
I may be way off on this one so feel free to critique it, I did it as I understood the issue but that could be very wrong. I've added some new integration tests but I might have missed another place where I should add them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beforeAssociate / afterAssociate
, emit association / options in afterAssociate
@sushantdhiman I've made changes to the name of the hooks and changed what gets emitted with |
lib/associations/mixin.js
Outdated
// the id is in the foreign table | ||
const association = new Type(source, target, _.extend(options, source.options)); | ||
source.associations[association.associationAccessor] = association; | ||
|
||
association._injectAttributes(); | ||
association.mixin(source.prototype); | ||
|
||
if (options.useHooks) { | ||
this.runHooks('afterAssociate', target, options); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of target
, pass association
(that is what I meany by emit association :p)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, sorry about that. Do we still need to pass the source in after?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, we just want to know what association got created, source and target info is available on this object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sushantdhiman Ok. Here is another stab at it. I'm not passing source or target anymore since they are available within hook through this
. We're passing association object now. I'm open to more suggestions. Tests should be passing but they might be a bit wonky so I'll accept any tips.
I was thinking more about usecase of these hooks, I think we need to support
Each Just unit tests will be enough for these hooks, you can add them in respective association test file https://github.com/sequelize/sequelize/tree/master/test/unit/associations
I agree |
@sushantdhiman Ok. I've tried to change stuff up as per your direction. Tests are passing locally on all dialects but for whatever reason they've failed on Travis. I haven't touched any files that might mess them up... |
I'd suggest just one before/after hook for associations, let userland inspect the association for type, less surface area. |
lib/associations/mixin.js
Outdated
@@ -26,13 +26,21 @@ const Mixin = { | |||
|
|||
options = _.extend(options, _.omit(source.options, ['hooks'])); | |||
|
|||
if (options.useHooks) { | |||
this.runHooks('beforeHasManyAssociate', target, options); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than target, options
i'd suggest.
{source, target, ...options, association}
, association only for after of course.
/cc @sushantdhiman
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can have {source, target}
for before* hooks, {source, target, association}
for after* hooks, I would like to keep options
separate as in future we may have conflicting options
I don't see a good way to identify what kind of association is being created, especially in
|
I'm willing to work with either of these options, just let me know what you guys think is a better option. |
@mirkojotic Sorry for this wait, please implement this format if you still like to continue.
|
Hey @sushantdhiman I gave it another shot. Let me know if further modifications are required. |
Pull Request check-list
Please make sure to review and check all of these items:
npm run test
ornpm run test-DIALECT
pass with this change (including linting)?Description of change
Added hooks for association creation as per #9113