zemirco / json2csv

Convert json to csv with column titles
http://zemirco.github.io/json2csv
MIT License
2.71k stars 366 forks source link

The value function in the fields does not support the async function #530

Closed Kehao33 closed 3 years ago

Kehao33 commented 3 years ago

import { parse } from "json2csv";

   const options = { 
          fields: [
            {
                label: "classLen", 
                value: async (row, _) =>
                  (
                    await this.someModel.find({classId: row?._id})
                  )?.filter((c) => c.students > 0)?.length,
                default: "0", 
              }
          ]
   }

const data = {classId: 12345};
console.log(parse(data, options));  

want: "classLen" "23"

but return: "classLen" "{}"

Filing an issue

"json2csv": "^5.0.6"

Thank you. The value function in the fields does not support the async function

juanjoDiaz commented 3 years ago

It does not.

This library convert JSON to CSV and allow some formatting features but not advanced data manipulation. Think of a dataset with millions of rows and hundreds of columns... The performance impact of allowing async function would be too high to make sense.

You can do the mapping by preprocessing data. But I still think that you should be re-thinking what you are doing... If you are doing API calls or db lookups you might be doing the same call a million times and DOSing your own services if the dataset gets big...

knownasilya commented 3 years ago

An alternative approach would be to find all of the db entities, and then look them up from that list instead.