fast-csv icon indicating copy to clipboard operation
fast-csv copied to clipboard

[FEATURE] New Row Type: Array of Objects

Open daedalus28 opened this issue 5 years ago • 2 comments

Parsing or Formatting?

  • [x] Formatting
  • [ ] Parsing

Is your feature request related to a problem? Please describe. This library is awesome, but writing an array of objects requires me to iterate over them one by one and stream out each record.

Describe the solution you'd like I'd like fast-csv to support a Row Type that is an array of objects (which I think is [{[string]: any}] using the type notation from the docs).

Describe alternatives you've considered I can work around this by converting it to an array of arrays, but this requires me to rebuild some of the logic from fast-csv (e.g. ensuring the columns are in the right order, etc).

Additional context I'm attempting to use this library to deprecate as much of the csv logic as possible in contexture-export. There are just a few missing features, but I'm optimistic this library can allow us to remove all of it 😄

daedalus28 avatar Dec 23 '20 00:12 daedalus28

As a follow-up to this - it would be awesome if fast-csv could also support calling write with an async iterable (where the iterable returns one of the support row types).

For reference, an async iterable that returns "pages" (arrays of objects) of data on each iteration requires me to do this:

for await (let page of iterable)
  for (let record of page)
    fastCsvStream.write(record)

Ideally, I'd just be able to do this:

fastCsvStream.write(iterable)

I'd also be ok with a separate method, like:

fastCsvStream.writeAll(iterable)

daedalus28 avatar Dec 23 '20 00:12 daedalus28

I've got a WIP wrapper on my branch that implements all of the features I'd like to see added to fast-csv. My wrapper probably doesn't quite work in the general case (which is why it's not a PR) since I was only concerned with my use case of arrays of objects, but it might be helpful to take a look a the file:

https://github.com/smartprocure/contexture-export/blob/refactoring/src/modern/fast-csv-wrapper.js

daedalus28 avatar Dec 24 '20 21:12 daedalus28