sequelize before update hooks doesn't catch the model before update

Viewed 151

here is my code. I'm trying to log a model attributes before every time it gets updated.

[This in case I want to know historical values]

global.sequelize.afterConnect(async (connection, config) => {
        console.log(`after connect mysql`);
        for (const [modelName, model] of Object.entries(global.sequelize.models)){
            model.beforeUpdate(async function(modelInstanceBeforeUpdate,options, cb) {
                console.log(`${modelName} beforeUpdate is `, modelInstanceBeforeUpdate)
            })

            model.beforeSave(async function(modelInstanceBeforeSave,options, cb) {
                console.log(`${modelName} beforeSave is `, modelInstanceBeforeSave)
            })

            model.afterUpdate(async function(modelInstanceAfterUpdate,options, cb) {
                console.log(`${modelName} afterUpdate is `, modelInstanceAfterUpdate)
            })

            model.beforeBulkUpdate(async function(modelInstanceBeforeUpdate,options, cb) {
                console.log(`${modelName} beforeBulkUpdate is `, modelInstanceBeforeUpdate)
            })

            model.afterBulkUpdate(async function(modelInstanceAfterUpdate,options, cb) {
                console.log(`${modelName} afterBulkUpdate is `, modelInstanceAfterUpdate)
            })


        }
    });

here is call to update

await Building.update(payload, {where: {....}});

the beforeBulkUpdate is called (3 times) but the modelInstanceBeforeUpdate appears with the new attributes, not the old ones

0 Answers
Related