Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Streams in MySQL with Node

Following the example on Piping results with Streams2, I'm trying to stream results from MySQL to stdout in node.js.

Code looks like this:

connection.query('SELECT * FROM table')
      .stream()
      .pipe(process.stdout);

I get this error: TypeError: invalid data

like image 318
Waylon Flinn Avatar asked Mar 12 '15 15:03

Waylon Flinn


1 Answers

Explanation

From this github issue for the project:

.stream() returns stream in "objectMode". You can't pipe it to stdout or network socket because "data" events have rows as payload, not Buffer chunks

Fix

You can fix this using the csv-stringify module.

var stringify = require('csv-stringify');

var stringifier = stringify();


connection.query('SELECT * FROM table')
    .stream()
    .pipe(stringifier).pipe(process.stdout);

notice the extra .pipe(stringifier) before the .pipe(process.stdout)

like image 102
Waylon Flinn Avatar answered Sep 30 '22 20:09

Waylon Flinn