Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support output to stream or string #12

Closed
pke opened this issue Sep 5, 2018 · 6 comments
Closed

Support output to stream or string #12

pke opened this issue Sep 5, 2018 · 6 comments
Labels

Comments

@pke
Copy link

pke commented Sep 5, 2018

Sometimes (in a webserver scenario) one does not need to write a file. Instead the CSV could be streamed or written to a string.
Possible to add this here?

@ryu1kn
Copy link
Owner

ryu1kn commented Sep 5, 2018

Hi @pke , we already have CsvStringifier, its usage is on the README. Does it suffice your needs?

With this, you can also easily create a transform stream like this:

const {Transform} = require('stream');
const createCsvStringifier = require('csv-writer').createObjectCsvStringifier;

class PeopleTransformStream extends Transform {
    constructor() {
        super({objectMode: true});

        this._firstRecord = true;
        this._csvStringifier = createCsvStringifier({
            header: [
                {id: 'name', title: 'NAME'},
                {id: 'lang', title: 'LANGUAGE'}
            ]
        });
    }

    // Here I'm assuming readable stream gives me people one-by-one (i.e. person)
    // If the readable stream passes multiple people, change it accordingly.
    _transform(person, encoding, callback) {
        const personLine = this._csvStringifier.stringifyRecords([person]);
        if (this._firstRecord) {
            this._firstRecord = false;
            callback(null, this._csvStringifier.getHeaderString() + personLine);
        } else {
            callback(null, personLine);
        }
    }
}

And if you already have readable & writable streams, you can pipe them

const transformStream = new PeopleTransformStream();

readStream.pipe(transformStream).pipe(writeStream);

Just make sure your readable stream is also working with objectMode 😉

@ryu1kn ryu1kn added the question label Sep 5, 2018
@pke
Copy link
Author

pke commented Sep 5, 2018

This looks good, thanks for the swift reply. How could I not see it in the readme?

@offero
Copy link

offero commented Apr 16, 2020

This would be a great utility to include in the lib.

@ryu1kn
Copy link
Owner

ryu1kn commented Apr 17, 2020

@offero I've been thinking about that too 😀

@ryu1kn
Copy link
Owner

ryu1kn commented Apr 30, 2020

Another instance where not providing the file write part in one step could have been better: #43

@ryu1kn
Copy link
Owner

ryu1kn commented May 29, 2021

Yet another one: #71

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants