Skip to content

Instantly share code, notes, and snippets.

@islishude
Created September 7, 2020 10:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save islishude/4a481e990229fa060056a53e820a71fb to your computer and use it in GitHub Desktop.
Save islishude/4a481e990229fa060056a53e820a71fb to your computer and use it in GitHub Desktop.
node-mysql2 query stream with Promise
// https://github.com/sidorares/node-mysql2/issues/677
const mysql = require("mysql2/promise");
const stream = require("stream");
const dbconn = mysql.createPool({
connectionLimit: 10,
host: "127.0.0.1",
user: "user",
password: "password",
database: "database",
});
const Reader = dbconn.pool.query(`select * from tname limit 10;`).stream();
const Writer = new stream.Writable({
objectMode: true,
write(data, enc, cb) {
console.log(data);
cb();
},
});
stream.pipeline(Reader, Writer, (err) => {
if (err) {
console.log(err);
}
dbconn.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment