node.js - 以 upsert 为条件进行 Sequelize

标签 node.js sequelize.js

我目前正在使用带有 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/

相关文章:

sequelize.js - 如何在sequelize中查询多对多关系

apache - 在 Ec2 上使用 Apache Proxy 部署 NodeJS(错误?)并记录日志

node.js - 如何配置 package.json 以便某些软件包仅安装在 Windows 上?

javascript - 干燥 NodeJS 下 TypeScript 中的常见情况 require

express - findOne 不是函数

node.js - 按位添加并在 Sequelize 中添加列

javascript - express req.session 对象是如何持久化的?

javascript - Cypress Cucumber - 如何让我的步骤按顺序运行?

node.js - Sequelize 1.7.3 中的时间戳

mysql - 无法使用sequelize和无服务器框架从部署的 Node azure功能连接到mysql灵活服务器