Search code examples
node.jssequelize.jsnodes

node sequelize: can I upsert object array at once?


object array like [{id: 1, a: 1, b: 2}, {id: 2, a: 3, b:4}]. and I hope to upsert the array to database using sequelize upsert at once. can it support array or should use loop to do it?


Solution

  • you can use updateOnDuplicate along with bulkCreate method as follows

    model.bulkCreate(dataArray, { 
      updateOnDuplicate: [ 'a', 'b' ] // array of fields to update 
    }).then(()=> {});
    

    this is only supported by mysql

    reference bulkCreate