Skip to content

Instantly share code, notes, and snippets.

@myndzi
Created December 11, 2016 21:41
Show Gist options
  • Save myndzi/943dec8ba7181152b597aa1612c3c6fb to your computer and use it in GitHub Desktop.
Save myndzi/943dec8ba7181152b597aa1612c3c6fb to your computer and use it in GitHub Desktop.
foo bar
1 2
3 4
5 6
7 8
foo bar
9 10
11 12
13 14
foo bar
15 16
17 18
19 20
21 22
23 24
'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