Skip to content

.query(...).stream().pipe(process.stdout) throws TypeError: invalid data #741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JHKennedy4 opened this issue Feb 26, 2014 · 6 comments
Closed
Labels

Comments

@JHKennedy4
Copy link

I'm using the following code to test out the functionality of .stream().pipe() and I'm getting an error when I pass fairly typical stream outputs I've seem used elsewhere to .pipe(). I'm wondering if I'm totally off base or if this functionality is no longer supported.

pool.getConnection(function (err, connection) {
    connection.query('SELECT * FROM wp_users WHERE ID = ?', [req.params.id])
    .stream({highWaterMark: 5})
    .pipe(process.stdout);
});

I get "TypeError: first argument must be a string or Buffer" when I call .pipe(res).

@sidorares
Copy link
Member

.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. You need to serialize as text/buffer manually

@ZJONSSON
Copy link
Contributor

Exactly. Here is an example of a stringify transform:

var stream = require('stream'),
    util = require('util');

function StringifyStream(options) {
  if (!(this instanceof StringifyStream))
    return new StringifyStream(options);

  options = options || {};
  options.objectMode = true;

  stream.Transform.call(this,options);
}

util.inherits(StringifyStream,stream.Transform);

StringifyStream.prototype._transform = function(d,e,callback) {
  this.push(JSON.stringify(d));
  callback();
};

which can be inserted into your pipeline like this:

pool.getConnection(function (err, connection) {
    connection.query('SELECT * FROM wp_users WHERE ID = ?', [req.params.id])
    .stream({highWaterMark: 5})
    .pipe(StringifyStream())
    .pipe(process.stdout);
});

@JHKennedy4
Copy link
Author

Thank you so much for the clarification and example.

@waylonflinn
Copy link

You can also use the csv-stringify module.

at the command line in the root of your project, run:

npm install csv-stringify

then, in your file:

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

var stringifier = stringify();

pool.getConnection(function (err, connection) {
    connection.query('SELECT * FROM wp_users WHERE ID = ?', [req.params.id])
    .stream({highWaterMark: 5})
    .pipe(stringifier).pipe(process.stdout);
});

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

@gentunian
Copy link

gentunian commented Aug 27, 2017

Excellent examples and documentation. I've tried this for piping to stdout and works ok. The only issue I'm having is that I wan't to pipe large datasets. For some reason, highWaterMark option will only pipe the specified amount of objects, as the nodejs documentations states:

From buffering

For streams operating in object mode, the highWaterMark specifies a total number of objects.

From stream options

highWaterMark Buffer level when stream.write() starts returning false. Defaults to 16384 (16kb), or 16 for objectMode streams.

Saying that and without knowing how many rows should I return (most than 1.000), how could I pipe this into another process?

EDIT: nevermind, I thought that default highWaterMark will return only 16 objects but seems to works without setting any value (default). I found documentation a little bit odd. Sadly I needed to go for a try and error approach to find out this. Anyway, I would love if anyone may explain this not-documented behaviour or what thing I missunderstood of nodejs docs.

@germansokolov13
Copy link

@sidorares I think you should write this fact in here:
https://github.com/mysqljs/mysql#piping-results-with-streams
In this paragraph it doesn't say what becomes data in the data event of streams, a row, a collection of rows, a byte or what. It becomes clear only with your comment in here.

dveeden pushed a commit to dveeden/mysql that referenced this issue Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

7 participants