DEV Community

Generating and Downloading CSV files using express JS

David Okonji on May 18, 2020

While working on a node JS project, I had the need to pull certain data from my database and represent values in a CSV file, This guide aims at sim...
Collapse
 
spilex profile image
Alex Solonenko •

Actually, I'm not a Front End dev, and my knowledge of js and node is quite a little, but I can help you to improve your solution.
Postgres has a very nice feature called "COPY FROM". It can copy any data from your database in the CSV format with the header as well.
So basically you don't need to have a json_to_converter and any orm at all.
Just using the "COPY TO STDOUT" and js streams you are able to build a very powerful and nice solution, which will help you to get any data from the database no matter how it will be large in size. you will be limited only with your bandwidth.

Collapse
 
davidokonji profile image
David Okonji •

Thank you, Alex, Yeah sure an ORM is not necessary, I would surely try out the alternate solution suggested.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • • Edited

You should also streaming, especially if CSV is large.

Maybe by piping CSV stream.

Collapse
 
ndohjapan profile image
Joel Ndoh •

Hello, I love the article. But I do have some questions.

In a situation where I gotten the data I need from mongodb and I applied .populate

However, I would like to get access to a certain field in the populated field. How do I do that.

Collapse
 
davidokonji profile image
David Okonji •

Hello Joel, hope I’m not too late, if you use populate then you would have a nested object, the library supports dots notation, for example, you have a user object with Id key, it would be value: 'user.id'

Collapse
 
esealli profile image
Ese •

This was very helpful David, Thanks. Just wondering how it will be handled in the frontend.

Collapse
 
davidokonji profile image
David Okonji •

From the frontend, it can be sent as Formdata payload

Collapse
 
esealli profile image
Ese •

Thank you!

Collapse
 
devkumar profile image
Kumar •

Much needed one for me. Thanks :)

Collapse
 
awwal1999_72 profile image
Akanbi Lawal Tunde •

Nice read!!

Collapse
 
cavdy profile image
Cavdy •

This is beautiful... thanks

Collapse
 
donaldng profile image
Donald Ng •

Thank you this is really helpful!

Collapse
 
thisdotmedia_staff profile image
This Dot Media •

This is awesome! Thanks David 😁

Collapse
 
luancoleto profile image
Luan Mota Coleto •

Thanks, it was really helpful

Collapse
 
eliyahukriel profile image
eliyahuKriel •

thanks!! very helpfull
how can i set the file to be support hebrew lang? when download the file its look like
%$#@$R@F

thanks

Collapse
 
davidokonji profile image
David Okonji •

Hello, I am not quite sure, it depends on how the data is stored in the database.