How to use node-mysql without loads all the rows into the memory? Ask Question
snippet in sql

How to use node-mysql without loads all the rows into the memory? Ask Question

user6463

const mysql = require('mysql');
const stream = require('stream');

connection.query('SELECT * FROM `bigdata`')
  .on('error', function(err) {
    // Do something about error in the query
  })
  .stream() // Stream allows you to get records one-by-one
  .pipe(new stream.Transform({
    objectMode: true,
    transform: function (row, encoding, callback) {
      // Do something with the row of data

      callback();
    }
  }))
  .on('finish', function() {
    connection.end();
  });