最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sequelize Transaction Bulk Update followed by Bulk Create - Stack Overflow

programmeradmin3浏览0评论

I am working with Sequelize transaction and would like to know how to do a bulk update before sequentially doing a bulk create.

My current code is something like this:

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 })
});

I am working with Sequelize transaction and would like to know how to do a bulk update before sequentially doing a bulk create.

My current code is something like this:

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 })
});

Share Improve this question asked May 14, 2017 at 18:18 Chua Bing QuanChua Bing Quan 351 gold badge2 silver badges7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I believe you're just after promise chaining with then? The first line should return a promise - so just call then on the result:

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.
  }); 
});

Note: now your function will return a promise, so whatever calls your function will have to use then to get a handle on the result.

发布评论

评论列表(0)

  1. 暂无评论