Created
November 28, 2021 14:41
-
-
Save mshakhomirov/d300ef636910235669150a10deefb6bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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