Skip to content

Instantly share code, notes, and snippets.

@evansiroky
Created September 29, 2015 19:13
Show Gist options
  • Save evansiroky/167f218bb5342bff80c6 to your computer and use it in GitHub Desktop.
Save evansiroky/167f218bb5342bff80c6 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