Skip to content

Instantly share code, notes, and snippets.

@sunny1304
Last active December 30, 2015 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunny1304/7765291 to your computer and use it in GitHub Desktop.
Save sunny1304/7765291 to your computer and use it in GitHub Desktop.
Read Product list from a csv file and save them using async.js
var csv = require('csv'),
async = require('async'),
fs = require('fs'),
path = require('path'),
root = __dirname,
data = [],
maxconcurrency = 5;
var mongoose = require('mongoose');
mongoose.set('debug', true);
mongoose.connect('mongodb://localhost/products');
var Schema = mongoose.Schema;
var productSchema = new Schema({
name: {type: String, required: true, unique: true},
price: {type: Number, required: true}
});
var Product = mongoose.model('Product', productSchema);
var create_product = function(product,callback){
Product.create(product, callback);
}
var queue = async.queue(create_product,maxconcurrency);
csv()
.from.path(path.join(root, 'list1.csv'),{delimiter: ',', escape: '"'})
.transform(function(row){
row.unshift();
return row;
})
.on('record', function(row,index){
data.push({name: row[0], price: row[1]});
})
.on('end', function(){
data.splice(0,1);
data.forEach(function(product){
queue.push(product, function(err, product){
if (err) {
console.warn(err);
}
else{
console.log(product.name)
};
});
});
});
queue.empty= function(){
mongoose.disconnect();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment