Sequelize update and deduct multiple rows with different id

by unknown   Last Updated August 14, 2019 05:26 AM - source

I have a product table that needs to update multiple rows at once with different ids. still, don't have any idea on how can I achieve.

module.exports = sequelize.define('products', {

  product_id: {
    type: Sequelize.INTEGER(20),
  },
  product_quantity: {
    type: Sequelize.INTEGER(225),
  },  
})

var cart_list = [
  { id: 1, curr_quantity: 1 },
  { id: 2, curr_quantity: 4 },
  { id: 3, curr_quantity: 5 },
  { id: 4, curr_quantity: 2 }
]

Product.update({
  product_quantity
})
.then(product => {

})


Answers 1


As i have checked your code you have car_list array with multiple id and different quantity b'coz of that we have use loop as well as promise.

    let promises = [];
    var cart_list = [
        { id: 1, curr_quantity: 1 },
        { id: 2, curr_quantity: 4 },
        { id: 3, curr_quantity: 5 },
        { id: 4, curr_quantity: 2 }
    ]

    cart_list.forEach(element => {
        promises.push(Product.update({product_quantity : element.curr_quantity},{where:{'product_id' : element.id}}))
    });

    Promise.all(promises).then(resultdata=> {
        console.log('Success-------->',resultdata)
    }).catch(err =>{        
        console.log('Failed---------------->',err);
    });
Priyank
Priyank
August 14, 2019 05:25 AM

Related Questions


why validation is not working in mysql?

Updated August 19, 2018 10:26 AM


How to use sequelize migration models when querying?

Updated January 05, 2019 08:26 AM

Back up Sequelize (MySQL)

Updated September 04, 2018 09:26 AM

how to change sequelize inner join output format

Updated September 14, 2018 05:26 AM