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. |
|
@sushantdhiman I've made changes to the name of the hooks and changed what gets emitted with |
// 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); |
sushantdhiman
Jul 4, 2018
Contributor
Instead of target
, pass association
(that is what I meany by emit association :p)
Instead of target
, pass association
(that is what I meany by emit association :p)
mirkojotic
Jul 4, 2018
Author
Contributor
Ah, sorry about that. Do we still need to pass the source in after?
Ah, sorry about that. Do we still need to pass the source in after?
sushantdhiman
Jul 4, 2018
Contributor
No, we just want to know what association got created, source and target info is available on this object
No, we just want to know what association got created, source and target info is available on this object
mirkojotic
Jul 4, 2018
Author
Contributor
@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.
@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. |
@@ -26,13 +26,21 @@ const Mixin = { | |||
|
|||
options = _.extend(options, _.omit(source.options, ['hooks'])); | |||
|
|||
if (options.useHooks) { | |||
this.runHooks('beforeHasManyAssociate', target, options); |
mickhansen
Jul 6, 2018
Contributor
Rather than target, options
i'd suggest.
{source, target, ...options, association}
, association only for after of course.
/cc @sushantdhiman
Rather than target, options
i'd suggest.
{source, target, ...options, association}
, association only for after of course.
/cc @sushantdhiman
sushantdhiman
Jul 6, 2018
Contributor
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
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