brianc / node-pg-query-stream

Query results from node-postgres as a readable (object) stream
MIT License
311 stars 37 forks source link

Number of rows? #30

Closed vitaly-t closed 7 years ago

vitaly-t commented 7 years ago

When I did support of this library's v1.0 within pg-promise, I had to add a hack (code injection) for the read operation (method _fetch) in order to pull such basic information as the number of rows that went through the stream.

With version 1.1, do I have to write a new hack for it or is there now a civilized approach to getting the number of rows once the streaming has finished?


This is the hack that I had to do for v1.0:

    let stream, fetch, nRows = 0;
    try {
        stream = client.query(qs);
        fetch = stream._fetch;
        stream._fetch = (size, func) => {
            fetch.call(stream, size, (err, rows) => {
                if (!err && rows.length) {
                    nRows += rows.length;
                    if (error) {
                        stream.close();
                    }
                }
                return func(err, rows);
            });
        };
    } catch (err) {
        error = err;
    }
vitaly-t commented 7 years ago

This is directly related to: #12.

vitaly-t commented 7 years ago

I kind of have figured it out... by doing stream.on('data', () => { /* do internal increment */ }).

But I have run into another issue with this: #32.

Thanks to my own hack, I was able before to process pages of rows, but now I'm facing a very inefficient row-by-row data feed from the stream.

brianc commented 7 years ago

Yeah, if you are reading from a stream and want to know how many records are coming in from the stream its trivial to keep a counter of rows as you read them from the stream. That's the recommended approach AFAIK.

brianc commented 7 years ago

You could use something like through2 to pipe the stream into, and in your stream push all incoming data back out, but also increment a counter internally to that stream.