'node.js + sequelize bulk insert
I am new to node.js and sequelize. I have already built a mysql db with sequelize that matches my needs for the actual project.
Then I searched for possibilities to populate the db with sequelize, and the approach to combine it with csv files seem to be the best solution for me.
I have three csv files for three separate tables, and found Evan's script on github https://gist.github.com/evansiroky/167f218bb5342bff80c6#file-sequelizebulkinsert-js
Can anybody help me out how I need to implement it to do the job? I know that this must be a very basic question but I don't know how.
Solution 1:[1]
If you just want to populate your DB with the data, you can still do it the old fashioned way, using mysql's mysqlimport or other tools.
If this is something that you will do frequently, maybe you can try creating a function that will call the sequelizeBulkInsert script, assuming it is working fine (not tested):
// sequelizeBulkInsert.js
var fs = require('fs');
var async = require('async');
var csv = require('csv');
module.exports = function(Model){
this.importFile = function(filename, doneLoadingCallback) {
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);
}
}
Then, in another file
var Bulk = require('/path/to/sequelizeBulkInsert');
var bulk = new Bulk(Model);
bulk.importFile('path_to_data.csv', function(){
//data is imported
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |