Express 如何为sequelize模型添加公共属性?
我正在尝试使用Sequalize中的钩子向模型添加公共属性, 我定义了一个全局钩子,如下所示Express 如何为sequelize模型添加公共属性?,express,sequelize.js,feathersjs,Express,Sequelize.js,Feathersjs,我正在尝试使用Sequalize中的钩子向模型添加公共属性, 我定义了一个全局钩子,如下所示 module.exports = function (app) { const sequelizeClient = app.get('sequelizeClient'); sequelizeClient.addHook('beforeDefine', (attributes) => { var defaultAttributes = { id: { ty
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;
});
};
但是即使钩子被触发,属性仍然没有添加到模型中。我从这里得到了为公共属性添加挂钩的想法。我做错了什么?或者有没有更好的方法来添加公共属性
请注意,我使用mysql作为后端数据库。我使用的sequelize模型如下所示
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;
};