Skip to content

Instantly share code, notes, and snippets.

@mshakhomirov
Created November 28, 2021 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mshakhomirov/d300ef636910235669150a10deefb6bf to your computer and use it in GitHub Desktop.
Save mshakhomirov/d300ef636910235669150a10deefb6bf to your computer and use it in GitHub Desktop.
const mysql = require('mysql2/promise');
const csvstringify = require('csv-stringify');
...
const outputStream = fs.createWriteStream('output.csv', { encoding: 'utf8' }); // writable stream to save query results locally.
const s1 = connection.connection.query(sql); // establish a connection
s1.stream({ highWaterMark: BATCH_SIZE }) // readable stream from MySQL db. stream() is just to wrap into pipeable object, and not to enable 'result' events, they are emitted anyway.
.pipe(csvstringify({ header: true }))
.pipe(outputStream)
.on('finish', () => { resolve('saved data locally'); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment