Created
December 11, 2016 21:41
-
-
Save myndzi/943dec8ba7181152b597aa1612c3c6fb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
foo | bar | |
---|---|---|
1 | 2 | |
3 | 4 | |
5 | 6 | |
7 | 8 |
This file contains hidden or 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
foo | bar | |
---|---|---|
9 | 10 | |
11 | 12 | |
13 | 14 |
This file contains hidden or 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
foo | bar | |
---|---|---|
15 | 16 | |
17 | 18 | |
19 | 20 | |
21 | 22 | |
23 | 24 |
This file contains hidden or 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
'use strict'; | |
const csv = require('csv'); | |
const fs = require('fs'), | |
PATH = require('path'); | |
const _ = require('highland'); | |
var seq = 0; | |
// write some value to the database, return a promise | |
function writeToDatabase(value) { | |
console.log('writing value', value); | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
console.log('done writing', value); | |
resolve(seq++); | |
}, 100); | |
}); | |
} | |
_(fs.readdirSync('.')) | |
.flatten() | |
.filter(v => /\.csv$/.test(v)) | |
.flatMap(v => { | |
console.log('loading file:', v); | |
let csvStream = fs.createReadStream(PATH.join(__dirname, v)) | |
.pipe(csv.parse()); | |
csvStream.once('end', function () { | |
console.log('closed file:', v); | |
}); | |
return _(csvStream).drop(1); // skip header row; | |
}) | |
.flatMap(v => _(writeToDatabase(v))) | |
.each(v => { }) // consume the stream |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment