Last active
March 18, 2018 17:34
-
-
Save dmeehan1968/3c776ed7e582930b1ecdb16fc2d3c4d0 to your computer and use it in GitHub Desktop.
CSV to Mongo Stream musings
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
fs.createReadStream(argv.heart) | |
.pipe(csv.parse({ objectMode: true })) | |
.pipe(csv.transform({ | |
header: columns => { | |
const properties = { | |
Start: 'start', | |
End: 'end', | |
'Heart Rate (count/min)': 'heartRate', | |
} | |
return columns.map(column => properties[column] || column) | |
}, | |
row: row => { | |
const properties = { | |
start: date => new Date(date), | |
end: date => new Date(date), | |
heartRate: rate => Number(rate), | |
} | |
return row.map(({column, value}) => properties[column](value) || value) | |
}, | |
})) | |
.pipe(MongoStream.insert({ | |
url: '', | |
db: '', | |
collection: '', | |
})) | |
.on('init', (collection, next) => { | |
return collection.drop().then(next) | |
}) | |
.on('end', (collection, next) => { | |
return collection.count({}).then(count => console.log(count)).then(next) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment