zemirco / json2csv-stream

Transform stream from json to csv
52 stars 17 forks source link

Improve Documentation - new stream isn't reusable #14

Closed chriszrc closed 6 years ago

chriszrc commented 6 years ago

I just lost almost a day of debugging streams to finally realize that I couldn't do this:

var fs = require('fs');
var MyStream = require('json2csv-stream');

// create the transform stream
var parser = new MyStream();

app.get('/', function (req, res) {
  let reader = fs.createReadStream('data.json');
  let writer = fs.createWriteStream('out.csv');

  reader.pipe(parser).pipe(writer).pipe(res);
})

I didn't realize that MyStream needs to be instantiated for each use. So that example worked great on the first request, and then stopped.

This then worked great:

var fs = require('fs');
var MyStream = require('json2csv-stream');

app.get('/', function (req, res) {
  let reader = fs.createReadStream('data.json');
  let writer = fs.createWriteStream('out.csv');

  reader.pipe(new MyStream()).pipe(writer).pipe(res);
})

A heads up in the docs about reuse of the MyStream object I think would be greatly appreciated-

knownasilya commented 6 years ago

I'd totally merge a pull request if you are willing to submit it.

chriszrc commented 6 years ago

Ok, I added a pull request - https://github.com/zemirco/json2csv-stream/pull/15

Could I also draw your attention to this issue to :) https://github.com/zemirco/json2csv-stream/issues/13