You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wondering if it is possible to add hooks for Associations (like beforeDefine, afterDefine, ... for model)
What are you doing?
constUser=db.define('user');constTag=db.define('user');User.belongsToMany(Tag,{through: UserTag});// trigger association created hook hereTag.belongsToMany(User,{through: UserTag});// trigger association created hook here
Currently, the best I can do is:
varprev=Association.BelongsTo.prototype.injectAttributes;Association.BelongsTo.prototype.injectAttributes=function(){// my hook herereturnprev.apply(this,arguments);}// ... and same for HasOne, BelongsToMany, HasMany
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.
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.
Activity
lukeed commentedon Mar 20, 2018
Add a
*Createhook on yourUserTagmodel.FranckFreiburger commentedon Mar 20, 2018
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.
sushantdhiman commentedon Apr 7, 2018
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.
NicolasNarvaez commentedon Jun 28, 2018
I need this to transparently keep track of references to a model, do you know another way to do it?
sushantdhiman commentedon Jul 19, 2018
Implemented in #9590
beforeAssociate / afterAssociate