javascript - 如何将两个更新请求合并为一个?

标签 javascript orm sequelize.js nodes

我现在有一个有效的代码。

await Users.update({ selected: false }, { where: { userId: req.body.userId } });
await Users.update(
  {
    selected: req.body.selected,
    descr: req.body.note
  },
  {
    where:
    {
      entId: req.body.id,
      userId: req.body.userId
    }
  }
);
但是,如果可以将这两个查询合二为一呢?我需要我传递的“选定”和“注释”字段有条件地在表中更改。并且表中用户固有的所有其他“选定”字段都变为假。
不幸的是,我在文档中没有找到类似的东西。预先感谢您的帮助!

最佳答案

不幸的是,Sequelize 中没有像 bulkUpdate 这样的方法,因此您需要调用 update 两次,最好使用事务将这两个查询作为一个原子操作进行。

await Sequelize.transaction(async transaction => {
  await Users.update({ selected: false }, { where: { userId: req.body.userId }, transaction });
  await Users.update(
    {
      selected: req.body.selected,
      descr: req.body.note
    },
    {
      where:
      {
        entId: req.body.id,
        userId: req.body.userId
      },
      transaction
    }
  );
});

关于javascript - 如何将两个更新请求合并为一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66909391/

相关文章:

php - Jquery 无法将变量传递给 php

javascript - 限制DatePicker选择的最小年份

linux - 使用 Silverlight 访问在 Linux 下运行的 postgres 数据库?

mysql - 惯用语 (MySQL) - 原始查询 - 显示表类似

node.js - 我想使用 typedi 在 Service 构造函数中注入(inject) Repositories 容器

javascript - JQuery-ui 组件在加载时不显示

javascript - 如何在 Javascript 的链函数中执行第二个函数 firSTLy?

database - 我如何在 Redis 中组织汽车的制造商、型号和生成

node.js - Sequelize js - 限制和排序错误

mysql - 将 SQL 查询转换为包含多个表的 sequelize 查询