Skip to content
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

Synchronous result stream processing #770

Closed
dendrochronology opened this issue Apr 13, 2018 · 2 comments
Closed

Synchronous result stream processing #770

dendrochronology opened this issue Apr 13, 2018 · 2 comments

Comments

@dendrochronology
Copy link

I'm having trouble finding clear examples of how to perform a query and process the result stream synchronously. I've tried the following example from this comment but thePromise hangs, so the script never executes the next line, and node just sits there without exiting (never reaches the connection close line either):

const thingsWeCareAbout = new Map();

await new Promise((resolve, reject) => {
  const s1 = connection.query('SELECT a lot of stuff').stream();
  s1.on('result', row => {
    // logic to determine if we care about this row
    thingsWeCareAbout.set('foreignKeyFromQuery', ['stuff', 'to', 'process', 'later']);
  });
  s1.on('end', resolve);
  s1.on('error', reject);
});

doMoreStuff(thingsWeCareAbout); // this never executes; script is hung

I need to pull millions of rows, but don't want to load the whole thing into memory. I also can't move on to the next step until all the rows have been examined by a function that determines if we need to do something in the next step (it's a chain of ETL functions for a data warehouse).

@dendrochronology
Copy link
Author

A colleague suggested removing .stream() from the query, and it works! Did I miss that in the docs somewhere?

@sidorares
Copy link
Owner

Because you are not composing streams there is no point in using .stream()

I think what happens is no one reads from other side of the stream and it pauses itself ( backpressure handling ) -

if (!stream.push(row)) {

In order to just read rows without buffering them in memory .query() with no callback and 'result' events handler is enough

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants