Hi, I'm trying to do a bulk update to an MSSQL database from Node using sequelize. I still don't quite understand all this stuff about Promises, .then() etc.
What I am needing is a function called by POST, which receives an array of JSON objects:
[{F1:'123',F2:'a',F3:'b',F4:'Z'},
{F1:'124',F2:'a',F3:'b',F4:'Z'},
{F1:'125',F2:'a',F3:'b',F4:'Z'},
{F1:'126',F2:'a',F3:'b',F4:'Z'},]
Where F1 is the id of the object and F4 the field to update. Do an update on all received rows and send a response. For a single Object, I seamlessly do the following:
MODEL.update(
{
att4: art['F4'],
},
{
where:{
att1: {$eq: art['F1']}
}
}).then(function(result)
{
res.status(200).send();
}).catch(function(err)
{
res.status(500).send(err);
})
And everything is perfect. However, it would need all the updates to be carried out massively, and then receive a response on the client, either with the number of rows affected, or the corresponding error.
I have tried putting the MODEL.update([...]) inside a function that receives a JSON object and with a Foreach iterate the array and make a call to this function for each object, but I have not achieved what I need.
I hope you can help me. Thank you :)
Ok, I have researched and came across
Promise.all
.I was able to do the massive update, and by the way I put it in a transaction to maintain the atomicity of the query. Leaving something like this:
I hope it helps someone else.
NOTE: Knowing how to handle Promises is very important