Node-postgres: Query events with Pool

Created on 8 Jul 2016  路  8Comments  路  Source: brianc/node-postgres

With a client we can use the "row" event of a query.
client.query("select id from table").on("row", row => console.log(row))

{ id: 1 }
{ id: 2 }
{ id: 3 }
{ id: 4 }
{ id: 5 }

But with a pool we can't do this.
pool.query("select id from table").on("row", row => console.log(row))
TypeError: pool.query(...).on is not a function

Is it possible to use the query events with pool?

Most helpful comment

The pool supports returning a client if you want to do something like that:

pool.connect(function(err, client, done) {
  var query = client.query('whatever')
  query.on('row', //etc)
  //...etc
})

That API ^^ has existed for years and is still there. 馃槃

pool.query is a helper method for the most common operation which is "I want to run a query and get the rows back." If you want to do something "fancier" with a client (transactions, row events, listen/notify, pg-query-stream, etc) then pool.query isn't for you. I don't really want to extend the pool.query method with even more functionality...

Curious: why are you using query.on('row') in the first place?

All 8 comments

I'm facing the same problem here. I don't want to refactor all code using loops instead of .on('row') to change pg.client to pg.pool

I have the same error in my application. Is there any way to fix this?

The pool supports returning a client if you want to do something like that:

pool.connect(function(err, client, done) {
  var query = client.query('whatever')
  query.on('row', //etc)
  //...etc
})

That API ^^ has existed for years and is still there. 馃槃

pool.query is a helper method for the most common operation which is "I want to run a query and get the rows back." If you want to do something "fancier" with a client (transactions, row events, listen/notify, pg-query-stream, etc) then pool.query isn't for you. I don't really want to extend the pool.query method with even more functionality...

Curious: why are you using query.on('row') in the first place?

@brianc I'm using query.on('row') because I need to do something for each line returned and with the row event I don't need to write loops in my code.

@brianc Thank you for the example, it helped me. I get that pool.query is supposed to be a helper method but perhaps it should use a different name to avoid confusion/inconsistency between client.query and pool.query. Btw I use query.on('row') to "stream" data through websocket in realtime.

Discouraged for Client.prototype.query as well in pg 7.

I know this is an old thread but I'm wanted to do something like what @jtuu is describing.

I want to use pool so I don't have to manage a warm pool of db connections (which is what I understand the point of pool to be). I want to stream results to clients though and the row event seems like a much better option that polling the cursor.

What is the best way of accomplishing this?

I want to stream results to clients though and the row event seems like a much better option that polling the cursor.

@jcuenod That鈥檚 not what the row event did, but it鈥檚 an easy mistake to make (and why it鈥檚 not part of the public API now). You will probably want to use a cursor, but it doesn鈥檛 have to be directly.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tonylukasavage picture tonylukasavage  路  4Comments

dipakdas99 picture dipakdas99  路  3Comments

gregallenvt picture gregallenvt  路  3Comments

dindurthy picture dindurthy  路  4Comments

wrod7 picture wrod7  路  4Comments