Skip to content

Instantly share code, notes, and snippets.

@MrDavidChz
Forked from evansiroky/sequelizeBulkInsert.js
Created November 1, 2019 17:42
Show Gist options
  • Save MrDavidChz/c90ec2eadd9d79ac80dd2154fbd6b714 to your computer and use it in GitHub Desktop.
Save MrDavidChz/c90ec2eadd9d79ac80dd2154fbd6b714 to your computer and use it in GitHub Desktop.
Insert CSV using sequelize bulk insert
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment