node-csv-stream icon indicating copy to clipboard operation
node-csv-stream copied to clipboard

cannot write objects

Open Morriz opened this issue 12 years ago • 5 comments

Hi, thanks for your little handy lib first of all :)

I have a question though: why is there no documentation available on the write part?

Looking at your documentation I would assume it would consume object hashes, so it would internally convert them to whatever it needs to handle.

So I tried using:

csvsream.write({some: object})

but the code expects a buffer as input, which seems very inconsistent to me.

What are your ideas?

Morriz avatar Oct 10 '13 10:10 Morriz

Hi @Morriz,

If you want to create a CSV string from multiple objects, this library is not appropriate. This library is the stream version of CSV.parse, not a stream version of CSV.stringify. I supposed we could add this stream to this lib. So we will have two streams one for parsing and another for "stringifying".

Something like that,

CSV string (input Buffer|String) to objects (output Object)

var csv = require('csv-stream');
var stream = csv.createParseStream(); // equivalent to the method `csv.createStream()` now

objects (input Object) to CSV string (output String|Buffer)

var csv = require('csv-stream');
var stream = csv.createStringifyStream();

If we do this, that's a good time to redo the lib with stream3. :)

lbdremy avatar Oct 10 '13 11:10 lbdremy

No, I meant to write objects to a csv file not read them ;)

Maurice

On 10 okt. 2013, at 13:25, Remy Loubradou [email protected] wrote:

Hi @Morriz,

If you want to create a CSV string from multiple objects, this library is not appropriate. This library is the stream version of CSV.parse, not a stream version of CSV.stringify. I supposed we could add this stream to this lib. So we will have two streams one for parsing and another for "stringifying".

Something like that,

CSV string (input Buffer|String) to objects (output Object)

var csv = require('csv-stream'); var stream = csv.createParseStream(); // equivalent to the method csv.createStream() now objects (input Object) to CSV string (output String|Buffer)

var csv = require('csv-stream'); var stream = csv.createStringifyStream(); If we do this, that's a good time to redo the lib with stream3. :)

— Reply to this email directly or view it on GitHub.

Morriz avatar Oct 10 '13 12:10 Morriz

Yes I understood, but at the moment that's not what the createStream() is for. That's the reason why I proposed something like createStringifyStream() that would do the job you are asking for.

lbdremy avatar Oct 10 '13 14:10 lbdremy

Aha, tnx for thinking along and responding so quickly. I am not able to work off-work hours for a while, so I can't contribute. I hope to find an off the shelve object to csv writer today, otherwise I just have to do it the hacky way for my current employer :|

Morriz avatar Oct 10 '13 15:10 Morriz

If you use node v0.10 that's really easy to implement a transform stream, have look to the doc here http://nodejs.org/api/stream.html#stream_class_stream_transform_1. Basically you just need to create a class, inherits from the Transform stream; implement _transform() method where you put the transformation logic and here you go.

lbdremy avatar Oct 10 '13 15:10 lbdremy