我希望通过全局钩子为我的所有Sequelize操作添加一些低级别的日志记录。像这样:
db.addHook('beforeUpdate', (instance, options) => {
log.info({ instance, options }, `Updating ${modelName} ${instance.get('id')}`);
});
我还没想到的是如何填充modelName
甚至是否可能(虽然我发现缺乏信息表明它可能不可能)。有可能 ,我还没找到钥匙?
记录时,这是实例值:
instance: {
"id": null,
"nonIncarcerationAgreementIndicator": true,
"insuranceApplicationId": 22,
"updatedAt": "2018-06-13T13:55:10.978Z",
"createdAt": "2018-06-13T13:55:10.978Z"
}
options
列出的时间太长,但他们没有提到型号名称。
答案 0 :(得分:2)
你在哪个版本的续集?对于v4,这是有效的
const Sequelize = require('sequelize');
const sequelize = new Sequelize('test_db', 'postgres', 'postgres', {
host: '127.0.0.1',
dialect: 'postgres',
define: {
hooks: {
beforeCreate: (model, options) => {
console.log(model.constructor.name);
}
}
}
});
const User = sequelize.define('user', {
username: Sequelize.STRING,
});
User.create({
username: 'UserOne',
projects: {
projectName: 'projectOne'
}
}).then((user) => {
console.log(user);
})