[英]Sequelize Transaction Bulk Update followed by Bulk Create
I am working with Sequelize transaction and would like to know how to do a bulk update before sequentially doing a bulk create. 我正在使用Sequelize事务,并且想知道在顺序执行批量创建之前如何进行批量更新。
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 believe you're just after promise chaining with then
? 我相信你的承诺只是后链接then
? The first line should return a promise - so just call then
on the result: 第一行应返回一个承诺-所以只需调用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.
});
});
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. 注:现在你的函数会返回一个承诺,所以无论您的通话功能将不得不使用then
获得对结果的句柄。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.