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

@mirkojotic mirkojotic commented Jun 25, 2018

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

@mirkojotic mirkojotic commented Jun 25, 2018

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

beforeAssociate / afterAssociate, emit association / options in afterAssociate

@mirkojotic
Copy link
Contributor Author

@mirkojotic mirkojotic commented Jul 4, 2018

@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);

This comment has been minimized.

@sushantdhiman

sushantdhiman Jul 4, 2018
Contributor

Instead of target, pass association (that is what I meany by emit association :p)

This comment has been minimized.

@mirkojotic

mirkojotic Jul 4, 2018
Author Contributor

Ah, sorry about that. Do we still need to pass the source in after?

This comment has been minimized.

@sushantdhiman

sushantdhiman Jul 4, 2018
Contributor

No, we just want to know what association got created, source and target info is available on this object

This comment has been minimized.

@mirkojotic

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

@sushantdhiman sushantdhiman commented Jul 5, 2018

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

@mirkojotic mirkojotic commented Jul 5, 2018

@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 sequelize/orm Jul 6, 2018
@mickhansen
Copy link
Contributor

@mickhansen mickhansen commented Jul 6, 2018

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);

This comment has been minimized.

@mickhansen

mickhansen Jul 6, 2018
Contributor

Rather than target, options i'd suggest.

{source, target, ...options, association}, association only for after of course.
/cc @sushantdhiman

This comment has been minimized.

@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

@sushantdhiman
Copy link
Contributor

@sushantdhiman sushantdhiman commented Jul 6, 2018

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

@mirkojotic mirkojotic commented Jul 6, 2018

@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

@sushantdhiman sushantdhiman commented Jul 17, 2018

@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

@mirkojotic mirkojotic commented Jul 19, 2018

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
4 checks passed
4 checks passed
codecov/patch 100% of diff hit (target 96.01%)
Details
codecov/project 96.02% (+<.01%) compared to 702e365
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants