Issue
I am trying to bulk update using sequlize ORM but i have to run loop for that that i dont want to so how i can do without loop
i have list of object in team that i have to update in database
code is
bulkupdate_transaction = async(contest_team_data:any,flag:number):Promise<any> =>{
return new Promise(async (resolve, reject) =>{
try{
let data = []
for(let index = 0; index < contest_team_data.length;index++){
data.push({
team_id: contest_team_data[index].team_id,
team_name: contest_team_data[index].team_name,
user_id: contest_team_data[index].user_id,
contest_id: contest_team_data[index].contest_id,
contest_date: contest_team_data[index].contest_date,
selected_symbols: contest_team_data[index].selected_symbols,
status: contest_team_data[index].status,
fees_paid: contest_team_data[index].fees_paid,
po_id: contest_team_data[index].po_id,
points: contest_team_data[index].points,
bonus_deduction: contest_team_data[index].bonus_deduction,
cash_deduction: contest_team_data[index].cash_deduction,
winning_deduction: contest_team_data[index].winning_deduction,
winning_prize: contest_team_data[index].winning_prize,
win: (contest_team_data[index].winning_prize > 0) ? true : false,
rank: contest_team_data[index].rank,
createdat: contest_team_data[index].createdat,
updatedat: new Date()
})
}
await Teams.bulkCreate(data , {
updateOnDuplicate: ['points', 'rank','win','winning_prize']
});
resolve({success:true})
}catch(error){
console.log(error)
reject({success:false})
}
})
}
as you can see i have variable named contest_team_data that have value like that
[
teams {
dataValues: {
team_id: '4e1a430c-c697-457b-8b6f-b71e6990b6c3',
po_id: 'ff1bdd59-272f-4c18-97bf-b54dbd8d0020',
user_id: '15218145-d032-4971-b3f9-d2cb2a09adc9',
selected_symbols: [Array],
team_name: 'test 9',
contest_id: '5c8fbd6f-fa57-40f8-97e6-ccc47f537764',
contest_date: '2022-07-25',
status: 'Live',
fees_paid: 50,
bonus_deduction: 5,
winning_deduction: 0,
cash_deduction: 45,
createdat: 2022-07-25T05:38:40.824Z,
purchaseOrder: [purchase_orders],
points: '-137472',
rank: 14
}
},
teams {
dataValues: {
team_id: '5f73f75b-51c7-46c5-bdf5-0c61d25471f7',
po_id: 'ff1bdd59-272f-4c18-97bf-b54dbd8d0020',
user_id: '15218145-d032-4971-b3f9-d2cb2a09adc9',
selected_symbols: [Array],
team_name: 'test 14',
contest_id: '5c8fbd6f-fa57-40f8-97e6-ccc47f537764',
contest_date: '2022-07-25',
status: 'Live',
fees_paid: 50,
bonus_deduction: 5,
winning_deduction: 0,
cash_deduction: 45,
createdat: 2022-08-26T05:38:40.824Z,
purchaseOrder: [purchase_orders],
points: '-130864',
rank: 13
}
}
]
when i trying to pass contest_team_data in sequlize for bulk update getting error for getting null but i have value in object
now please any one can help to how to solve this.
Solution
Unfortunately you should use a for loop to update all records based on a given array because Sequelize does not support such feature (basically most if not every DBMS do not support multi-record update for each record individually).
Answered By – Anatoly
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0