I am trying to use hooks in sequlize to add common attributes to models, I have defined a global hook as follows,
module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
sequelizeClient.addHook('beforeDefine', (attributes) => {
var defaultAttributes = {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
createdBy: {
type: Sequelize.INTEGER,
},
updatedBy: {
type: Sequelize.INTEGER,
},
deletedBy: {
type: Sequelize.INTEGER,
}
};
Object.assign(attributes, defaultAttributes);
//return attributes;
});
};
But even the hook get fired the attributes are still not added to the model. I got the idea to add hooks for common attributes from here https://github.com/sequelize/sequelize/issues/4383. What I am doing wrong here? or Is there any better way to add common attributes?
Please note that I am using mysql as backend database. The sequelize model I am using is as follows.
module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const person = sequelizeClient.define('person', {
name: {
type: DataTypes.STRING,
allowNull: false
}
}, {
hooks: {
beforeCount(options) {
options.raw = true;
}
}
});
person.associate = function (models) { // eslint-disable-line no-unused-vars
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
};
return person;
};