Nodejs: mysql stream query to CSV file along with column names

Viewed 176

I am currently streaming an SQL query to a CSV file using the mysql npm package.

const fs = require('fs')
const mysql = require('mysql')
const {pipeline} = require('stream')
const stringify = require('csv-stringify')

const stringifier = stringify()
const output = fs.createWriteStream('query.csv')

const connection = mysql.createConnection(...)

const input = connection.query('SELECT * FROM employees').stream()

pipeline(input, stringifier, output, err => {
  if (err) {
    console.log(err)
  } else {
    console.log('Output complete')
  }
})

This code works fine and streams the query to the csv file.

What I need to do is also get the column names and add that to the top of the CSV file.

Any help would be much appreciated.

0 Answers
Related