Edit: Nvm, I figured it out. It should've been "sequelize.models.inventory.update"
I'm making an inventory management system using Sequelize ORM. I currently have two table "Inventory" and "IngoingInventory" (by extension, later, "OutgoingInventory"). "Inventory" stores the current quantity of the inventory, and "IngoingInventory" stores the ingoing flow of an inventory.
I want to update the current quantity in "Inventory" everytime there's a new record added in "IngoingInventory". I've searched on ways to do this, and found out that I can use Sequelize hooks to update "Inventory" everytime a record get added into "IngoingInventory". However, I'm unable to use it as it keeps saying that it "Cannot read 'update' property of undefined". Here is my code.
ingoing_inventory.model.js
module.exports = (sequelize, Sequelize) => {
const IngoingInventory = sequelize.define("ingoing_inventory", {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
},
quantity: {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0
}
}, {
freezeTableName: true,
hooks: {
afterCreate: function(ingoing_inventory) {
sequelize.models.Inventory.update({ balance: sequelize.literal('balance + ingoing_inventory.quantity')}, {where: { id: ingoing_inventory.id }});
}
}
});
return IngoingInventory;};
inventory.model.js
module.exports = (sequelize, Sequelize) => {
const Inventory = sequelize.define("inventory", {
inventory_code: {
type: Sequelize.STRING,
allowNull: false,
unique: true
},
name: {
type: Sequelize.STRING,
allowNull: false,
unique: true
},
unit: {
type: Sequelize.STRING,
allowNull: false
},
balance: {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0
}
}, {
freezeTableName: true,
paranoid: true
});
return Inventory;};
They are both in the same 'models' folder. I've tried changing how to import "Inventory" and even trying to increment instead of update but nothing seems to work. Please help, thank you.
EDIT: Model registered through index.js
const dbConfig = require("../config/db.config.js");
const Sequelize = require("sequelize");
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
dialect: dbConfig.dialect,
operatorsAliases: 0,
pool: {
max: dbConfig.pool.max,
min: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle
}
});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.inventory = require("./inventory.model.js")(sequelize, Sequelize);
db.ingoing_form = require("./ingoing_form.model.js")(sequelize, Sequelize);
db.ingoing_inventory = require("./ingoing_inventory.model.js")(sequelize, Sequelize);
db.inventory.belongsToMany(db.ingoing_form, {through: db.ingoing_inventory});
db.ingoing_form.belongsToMany(db.inventory, {through: db.ingoing_inventory});
db.inventory.hasMany(db.ingoing_inventory);
db.ingoing_inventory.belongsTo(db.inventory);
db.ingoing_form.hasMany(db.ingoing_inventory, {as: "ingoing_inventory"});
db.ingoing_inventory.belongsTo(db.ingoing_form);
module.exports = db;