Last active
December 30, 2015 02:49
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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