【问题标题】:Sequelize Transaction Bulk Update followed by Bulk CreateSequelize Transaction Bulk Update,然后是 Bulk Create
【发布时间】:2017-10-13 12:32:44
【问题描述】:

我正在处理 Sequelize 事务,想知道如何在按顺序进行批量创建之前进行批量更新。

我当前的代码是这样的:

return sequelize.transaction(function(t){
  return sequelize.Promise.each(arrToUpdate, function(itemToUpdate){
    model.update(itemToUpdate, { transaction: t })
  }); 
  //How to do a sequential bulk create after the bulk update is successful in sequelize
  //transaction?
  //Bulk Create code would be return model.bulkCreate(itemsArray, { transaction: t })
});

【问题讨论】:

    标签: javascript mysql node.js sequelize.js


    【解决方案1】:

    我相信你只是在承诺与then 链接之后?第一行应该返回一个承诺 - 所以只需在结果上调用 then

    return sequelize.transaction(function(t){
      return sequelize.Promise.each(arrToUpdate, function(itemToUpdate){
        model.update(itemToUpdate, { transaction: t })
      }).then((updateResult) => {
        return model.bulkCreate(itemsArray, { transaction: t })
      }, (err) => {
        // if update throws an error, handle it here.
      }); 
    });
    

    注意:现在您的函数将返回一个承诺,因此无论您的函数调用什么,都必须使用 then 来获取结果句柄。

    【讨论】:

    • 谢谢,我没想到!过于关注循环内的返回 model.update()。
    猜你喜欢
    • 1970-01-01
    • 2016-06-11
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    相关资源
    最近更新 更多