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
1 Answer
Reset to default 5I 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.