How to do Bulk insert using Sequelize and node.js
I utilized the cargo
utility of the async library to load in up to 1000 rows at a time. See the following code for loading a csv into a database:
var fs = require('fs'),
async = require('async'),
csv = require('csv');
var input = fs.createReadStream(filename);
var parser = csv.parse({
columns: true,
relax: true
});
var inserter = async.cargo(function(tasks, inserterCallback) {
model.bulkCreate(tasks).then(function() {
inserterCallback();
}
);
},
1000
);
parser.on('readable', function () {
while(line = parser.read()) {
inserter.push(line);
}
});
parser.on('end', function (count) {
inserter.drain = function() {
doneLoadingCallback();
}
});
input.pipe(parser);
You can use Sequelize's built in bulkCreate
method to achieve this.
User.bulkCreate([
{ username: 'barfooz', isAdmin: true },
{ username: 'foo', isAdmin: true },
{ username: 'bar', isAdmin: false }
]).then(() => { // Notice: There are no arguments here, as of right now you'll have to...
return User.findAll();
}).then(users => {
console.log(users) // ... in order to get the array of user objects
})
Sequelize | Bulk Create and Update