我目前正在使用带有 node.js 的 sequelize v5。我正在尝试使用 upsert 创建新行或更新现有行,但我希望仅在满足条件时才触发更新。
await Model.upsert({values});
在模型表中,我有一个 bool 列。如果 bool 值为真,我希望更新不会覆盖现有行。我想也许我可以为 beforeUpdate 或 beforeSave 使用钩子(Hook),但我想不通。编辑:如果这很重要,我正在使用 postgresql。
最佳答案
假设您的模型名为 User
你声明了模型类
class User extends Model {}
User.init({
username: DataTypes.STRING,
mood: {
type: DataTypes.ENUM,
values: ['happy', 'sad', 'neutral']
}
});
你可以像那样做广告钩子(Hook)User.beforeUpsert(user, options) => {
if(user.condition){
... do stuff
}
});
关于node.js - 以 upsert 为条件进行 Sequelize ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63366436/