Stanley
Stanley

Reputation: 2806

Is there a more efficient way of bulk update in sequelize?

Suppose I have an array of user objects and a sequelize model User with id and name. Is there a more efficient way of doing update than the below?

let users = [{id: 1, name: 'John Doe'},{id: 2, name: 'Mary'}...]
users.map(user => {
   User.update(user, { where: { id: user.id } });
});

Upvotes: 0

Views: 467

Answers (1)

Stefan Hariton
Stefan Hariton

Reputation: 357

Try to use bulkCreate

Your case can be rewritten like:

User.bulkCreate(users, {updateOnDuplicate: true})

Docs

Upvotes: 2

Related Questions