Skip to content

Add association hook - #9590

Merged
sushantdhiman merged 7 commits into
sequelize:masterfrom
mirkojotic:add-association-hook
Jul 19, 2018
Merged

sushantdhiman merged 7 commits into
sequelize:masterfrom
mirkojotic:add-association-hook

Conversation

@mirkojotic

Copy link
Copy Markdown
Contributor

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

Added hooks for association creation as per #9113

@mirkojotic

Copy link
Copy Markdown
Contributor Author

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.

@codecov

codecov Bot commented Jun 25, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #9590 into master will increase coverage by <.01%.
The diff coverage is 100%.

@sushantdhiman sushantdhiman left a comment

Copy link
Copy Markdown
Contributor

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

@mirkojotic

Copy link
Copy Markdown
Contributor Author

@sushantdhiman I've made changes to the name of the hooks and changed what gets emitted with afterAssociate not sure if that's what your comment meant.

Comment thread lib/associations/mixin.js Outdated
association.mixin(source.prototype);

if (options.useHooks) {
this.runHooks('afterAssociate', target, options);

Copy link
Copy Markdown
Contributor

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)

Copy link
Copy Markdown
Contributor Author

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?

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

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.

@sushantdhiman

Copy link
Copy Markdown
Contributor

I was thinking more about usecase of these hooks, I think we need to support

  1. beforeHasOneAssociate / afterHasOneAssociate
  2. beforeHasManyAssociate / afterHasManyAssociate
  3. beforeBelongsToAssociate / afterBelongsToAssociate
  4. beforeBelongsToManyAssociate / afterBelongsToManyAssociate

Each before* hook should provide target / options, where as each after* hook should only provide association

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'm not passing source or target anymore since they are available within hook through this

I agree source is available as this but how target is available?

@mirkojotic

Copy link
Copy Markdown
Contributor Author

@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...

@sushantdhiman
sushantdhiman requested a review from a team July 6, 2018 03:35
@mickhansen

Copy link
Copy Markdown
Contributor

I'd suggest just one before/after hook for associations, let userland inspect the association for type, less surface area.

Comment thread lib/associations/mixin.js Outdated
options = _.extend(options, _.omit(source.options, ['hooks']));

if (options.useHooks) {
this.runHooks('beforeHasManyAssociate', target, options);

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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

@sushantdhiman

Copy link
Copy Markdown
Contributor

I'd suggest just one before/after hook for associations, let userland inspect the association for type, less surface area.

I don't see a good way to identify what kind of association is being created, especially in before* hooks. We can try

  1. different before hooks, single after hook
  2. single before after hook, with association constructor passed as type

@mirkojotic

Copy link
Copy Markdown
Contributor Author

@mickhansen @sushantdhiman

I'm willing to work with either of these options, just let me know what you guys think is a better option.

@sushantdhiman

Copy link
Copy Markdown
Contributor

@mirkojotic Sorry for this wait, please implement this format if you still like to continue.

  1. beforeAssociate, two options passed {source, target, type}, options
  2. afterAssociate, two options passed {source, target, type, association}, options

@mirkojotic

Copy link
Copy Markdown
Contributor Author

Hey @sushantdhiman I gave it another shot. Let me know if further modifications are required.

@sushantdhiman
sushantdhiman merged commit 9473061 into sequelize:master Jul 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants