Reputation: 51
I have two models. Purchase and PurchaseDetail and they are associated with Purchase.hasMany(models.PurchaseDetail) PurchaseDetail.belongsTo(models.Purchase)
I have the afterFind hook on PurchaseDetail that checks other models and adds data to the instance about the status of the product used in the PurchaseDetail and it works fine when I directly call PurchaseDetail.find
However, when I do Purchase.find and include PurchaseDetail, this hook is not triggered.
I know I can just call both models separately and combine it myself and send the data but if another developer in my team decides to include InvoiceDetail they will not receive this information.
How can I trigger this function anytime PurchaseDetail is included?
Upvotes: 5
Views: 2201
Reputation: 109
The hooks
aren't called in other models (findAll({ model, include: [{ model, include }]
)
https://github.com/sequelize/sequelize/issues/4627
Upvotes: 0
Reputation: 52
What we are doing here in the company I work for is extracting the behavior of the afterFind
hook in child to a prototype function that we called expand
, and then calling this expand
function inside afterFind
hook.
Then, we created an afterFind
hook inside the parent entity, and inside that, called child.expand()
.
Upvotes: 1