question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HOOKs and INCLUDE

See original GitHub issue

Hi everybody,

I have added some hook on my models but when this models are include in other models, their don’t call the hooks. For example :

return models.company.findAll({
    where : { idUser : idUser },
        include : [ {
            model : models.ticket , as : 'tickets',
            include : [{
                model : models.access , as : 'access' ,
                include : [
                    { model : models.event , as :'events' } ,
                    { model : models.eventPricing , as : 'priceItems' }
                ]
            }]
        }]
    }).then(function(transactions){
        return transactions;
    }).catch(function(error) {
        return error;
});

In this example I have a hook “afterFind” on my models “event” and “eventPricing” but it doesn’t call my hook afterFind. I tried to use this hooks: ‘beforeFindAfterExpandIncludeAll’ but its before Find and me I want it afterFindExpandIncludeModels or something like that I would like to call my hooks for each models event and eventPricing. Does anyone has already been faced to this problem ? Should I use an other Hook to do my job ? If I should, which one plz ? And I’m working with Sequelize 2.0 but I didn’t find other hooks available for with Sequelize 3.0 so. Want to know if I forgot something or if you got a quick solution to fix it, otherwise I’ll figured out with several requests but I prefer to do that in just one 😃

Thanks in advance for your help.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:13
  • Comments:18 (7 by maintainers)

github_iconTop GitHub Comments

15reactions
Keimenocommented, Jun 18, 2021

I think adding the attribute hooks with default set on false is the best option. For v7, it might make more sense to switch the default around.

I suppose we should add this attribute to the Includable object, right? Meaning, the API would look like this:

company.findAll({
    include: [{
        hooks: true // <---
        model: models.ticket,
        as: 'tickets',
        include: [{
            model: models.access,
            as: 'access',
            include: [{
                    model: models.event,
                    as: 'events'
                },
                {
                    hooks: true, // <---
                    model: models.eventPricing,
                    as: 'priceItems'
                }
            ]
        }]
    }]
})
5reactions
aedenmurraycommented, Dec 1, 2021

I needed to run the afterFind hook on all my included models, hopefully someone can find this helpful:

const handleChildrenAfterFindHook = async (instances, options, level = 0) => {
    if (!instances) return Promise.resolve();

    if (Array.isArray(instances)) {
        return Promise.all(
            instances.map((instance) => {
                const { options: instanceOptions } = instance.constructor;
                return handleChildrenAfterFindHook(
                    instance,
                    instanceOptions,
                    level
                );
            })
        );
    }

    const instance = instances;
    const { constructor } = instance;

    /**
     * Root model will have already run their "afterFind" hook.
     * Only run children "afterFind" hooks.
     */
    if (level >= 1) {
        await constructor.runHooks('afterFind', instance, options);
    }

    const { associations } = constructor;
    const associatedNames = Object.keys(instance).filter((attribute) =>
        Object.keys(associations).includes(attribute)
    );

    if (associatedNames.length) {
        const childInstances = associatedNames.map((name) => instance[name]);
        return handleChildrenAfterFindHook(childInstances, options, level + 1);
    }

    return Promise.resolve();
};

sequelize.addHook('afterFind', handleChildrenAfterFindHook);
models.sequelize = sequelize;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Git Hooks - Git SCM
The hooks are all stored in the hooks subdirectory of the Git directory. ... The first four hooks have to do with the...
Read more >
Git Hooks | Atlassian Git Tutorial
Git Hooks are scripts that run automatically every time a particular event occurs in a Git repository. Learn what they do and how...
Read more >
Hooks FAQ - React
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. This page...
Read more >
Get Started with Git Hooks - Medium
Major benefits of using Git hooks include encouraging a commit policy, automating development workflow, and implementing continuous integration.
Read more >
Supported hooks - pre-commit
add -trailing-comma - Automatically add trailing commas to calls and literals. ... reorder-python-imports - This hook reorders imports in python files.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found