Skip to content
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

Merged
merged 7 commits into from Jul 19, 2018
Merged

Conversation

mirkojotic
Copy link
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
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
Copy link

codecov bot commented Jun 25, 2018

Codecov Report

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

Copy link
Contributor

@sushantdhiman sushantdhiman left a 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

@mirkojotic
Copy link
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.

// 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);
Copy link
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
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
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
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
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
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
Contributor

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);
Copy link
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
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
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
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
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
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.

None yet

3 participants