Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize: insert in bulk

I'm using Node.js, MySQL and Sequelize. I'd like to insert some 10k rows into a table at once. The table has custom primaryKey field, that is being set manually. The data are downloaded from web and are overlapping.

I'd like to have a version of bulkCreate that wouldn't fail if any of the rows in data have unique keys that are already present in the table. Such kind of things is done in MySQL via INSERT ... ON DUPLICATE KEY UPDATE construct.

How do I do it in Sequelize?

like image 384
polkovnikov.ph Avatar asked Feb 01 '15 02:02

polkovnikov.ph


People also ask

How do I add bulk data to Sequelize?

When you need to insert multiple rows to your SQL database table, you can use the Sequelize bulkCreate() method. The bulkCreate() method allows you to insert multiple records to your database table with a single function call.

How do I update bulk records in Sequelize?

While Sequelize doesn't provide a bulkUpdate() method, both update() and bulkCreate() methods allow you to update multiple rows with a single method. When you need to update multiple rows with different values, you can use the bulkCreate() method.

How do I bulkCreate in Sequelize?

A very useful shortcut: the create method​ Sequelize provides the create method, which combines the build and save methods shown above into a single method: const jane = await User. create({ name: "Jane" }); // Jane exists in the database now!

How do I add a column to a Sequelize?

To add or delete columns in Sequelize CLI, we can use the sequelize migration:create command to create a migration file. Then we call addColumn to add a column and removeColumn to remove a column in the migration file. to create a migration file. in the migration file.


2 Answers

Pass in an options object to bulkCreate with ignoreDuplicates set to true

bulkCreate([...], { ignoreDuplicates: true })
like image 139
Yuri Zarubin Avatar answered Sep 22 '22 17:09

Yuri Zarubin


Yuri's answer will ignore duplicates... there is an option for updateOnDuplicate:

Fields to update if row key already exists (on duplicate key update)? (only supported by mysql & mariadb). By default, all fields are updated.

http://docs.sequelizejs.com/en/latest/api/model/#bulkcreaterecords-options-promisearrayinstance

like image 37
Sean Avatar answered Sep 19 '22 17:09

Sean