Skip to content

add association hooks #9113

Closed
Closed

Description

@FranckFreiburger

I wondering if it is possible to add hooks for Associations (like beforeDefine, afterDefine, ... for model)

What are you doing?

const User = db.define('user');
const Tag = db.define('user');

User.belongsToMany(Tag, { through: UserTag }); // trigger association created hook here
Tag.belongsToMany(User, { through: UserTag }); // trigger association created hook here

Currently, the best I can do is:

var prev = Association.BelongsTo.prototype.injectAttributes;
Association.BelongsTo.prototype.injectAttributes = function() {
  // my hook here
  return prev.apply(this, arguments);
}

// ... and same for HasOne, BelongsToMany, HasMany

Sequelize version: latest version

Activity

  1. lukeed commented on Mar 20, 2018

    @lukeed

    Add a *Create hook on your UserTag model.

  2. FranckFreiburger commented on Mar 20, 2018

    @FranckFreiburger
    Author

    I agree that this will work for belongsToMany because of the through model,
    however I also need callback for new BelongsTo, HasOne and HasMany associations.

  3. sushantdhiman commented on Apr 7, 2018

    @sushantdhiman
    Contributor

    You want to modify association option using these hooks I assume, currently there are no such hooks available but should be easy to add them. I am tagging this as a feature request.

  4. added
    type: feature
    good first issue
    on Apr 7, 2018
  5. NicolasNarvaez commented on Jun 28, 2018

    @NicolasNarvaez

    I need this to transparently keep track of references to a model, do you know another way to do it?

  6. sushantdhiman commented on Jul 19, 2018

    @sushantdhiman
    Contributor

    Implemented in #9590

    beforeAssociate / afterAssociate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issuetype: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions