Hello,I'm having trouble triggering hooks in ExpressJS. I'm trying to add a counter cache column to my postgres DB but when I run an update the hook is never triggered/called. I've tried putting a console.log in the afterUpdate hook and that doesn't get called either.Here is my item model code with hooks:'use strict'; module.exports = (sequelize, DataTypes) => { const item = sequelize.define('item', { item_id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, price: DataTypes.FLOAT, }, { underscored: true, hooks: { afterUpdate: async (item, options) => { const count = await sequelize.models.items.count({ where: { store_id: item.store_id, is_active: 1 }, transaction: options.transaction }) await sequelize.models.stores.update( { active_items_count: count }, { where: { store_id: item.store_id }, transaction: options.transaction }) } } }); item.associate = function(models) { this.belongsTo(models.user, {as: 'user', foreignKey: 'user_id'}); this.belongsTo(models.store, { as: "store", foreignKey: "store_id" }); }; return item; }; Here is a script that should trigger the afterUpdate hook:var Sequelize = require('sequelize'); const config = require('../config') const config_db = config.database; const models = require('../models'); let connection = new Sequelize(config_db.url, config); connection.sync({ force: false }).then(function () { models.item.update({is_active: 1 }, { where: {item_id: 147} }).then(function (item) { console.log(item.dataValues) process.exit(); }) }) The `is_active` column successfully updates but no further transactions happen. Am I triggering the hook incorrectly this way or are my hooks not set up correctly?
Submitted October 17, 2019 at 11:31PM by surfordie
No comments:
Post a Comment