node.js - How to export in nodejs a csv file from nodejs with a PostgreSQL -


The node file is on the haloku and I am using PostgreSQL as a database. I export a CSV file from a view I would like to:

// My PostgreSQL query

  var requires copyTo = ('Page-Copy-Stream'). Var CSV = Requirement ('CSV'); Var FS = Requirement ('FS'); Var stream = client.query (copyTo ('copy (from beam * v_metric) with CTV header as STDOUT DELIMITER as \' | \ ''));  

// Export in CSV

  csv () From (stream.pipe (process.stdout, {end: false)). (FS .createWriteStream ('sample.csv'))  

I have no results, sample.csv is empty.

Any thoughts?

Thank you in advance

STDOOT of SQL server compared to your node process Will be different. You can try:

  var query = client.query (select 'v_metric' from); Query.on ('line', function (line) {// handle the line here}};  

Then if your data is not complicated in your handler (i.e. no delimiter or double quote) you can leave it using csv and change them Repeat the columns again in a string that you write to the stream you write. It may be easy to put a column name in an array that you can pass in the form of a SQL (connecting with ',') and can be repeated on the handler, but you Object.keys (update row: your Example based on comment:

  var column = ['country_cd', 'product_name', . 

'product_lvel', 'MONTH_YEAR ',' METRIC_NAME ',' val ']; var ws = fs.createWriteStream (' sample.csv '); var query = client.query (' SELECT '+ columnss.join (', ') +' From Users') ; Query.on ('line', function (line) {var value = []; // process column value; If you need to do special formatting (i.e., date) then there is no loop and instead change each column in particular. ForEach (function (call) {values ​​= row [col];}); ws.write (Values.join ('|'));}; Query.on ('end', function (result) {ws.close ();});

If you select Csv , you can create a stream that you write by handler and in PP csv .

Another note, , is the default delimiter, if you have anything else, Like | , you must specify that in the option


Comments