wdavidw/node-csv — pull request #148 by colwilson (2014-01-12 09:44:33+00:00)
Title: allow stringifier to handle mongo _ids etc and other objects

csv = require("csv")
fs = require("fs")
async = require("async")
mongo = require("mongodb")
MongoClient = require('mongodb').MongoClient
lu = require("./postcode_areas")

MongoClient.connect("mongodb://127.0.0.1:27017/dummy", (err, db) ->
if(err)
throw err;

instream = fs.createReadStream(__dirname + "/cqc_locations_export.csv")

collectionName = "locations"
collection = db.collection(collectionName)
db.createIndex(collectionName, {city: 1, region: 1}, (err, db) ->
queue = async.queue(collection.insert.bind(collection), 5)

csv().from.stream(instream
).transform((row, index, cb) ->
data =
name: row[0]
address: row[2]
postcode: row[3]
phone: row[4]
website: row[5]

queue.push data, (err, res) ->
return err if err
cb null, res[0]

).on("end", (count) ->
console.log "Number of lines: " + count
).on "error", (error) ->
console.log error.message
)
)
queue.push data, (err, res) ->
return err if err
cb null, res[0]

).on("end", (count) ->
console.log "Number of lines: " + count
).on "error", (error) ->
console.log error.message
)
)