node-pg-copy-streams icon indicating copy to clipboard operation
node-pg-copy-streams copied to clipboard

Doesn't seem to work with `pool.query`

Open tswaters opened this issue 1 year ago • 1 comments

The examples don't make reference to this at all, but reading through the pg docs, there is a convenience utility on pool called query that is described here: https://node-postgres.com/apis/pool#poolquery

Often we only need to run a single query on the database, so as convenience the pool has a method to run a query on the first available idle client and return its result.

If used with pg-copy-stream, the result of the query will be a promise, not a stream.

const stream = pool.query(copyFrom(query))
stream.on // undefined :(

I looked at it with a debugger on, and it looks like a pending promise... I tried to await it, and it didn't come back, even with SELECT 1 as the query.

Here's some code that can repro this:

const pool = new pg.Pool({....etc })
const stream = pool.query(copyFrom(query))
stream.setEncoding('utf-8') // stream.setEncoding is not a function

I'm not sure if this is a bug necessarily, but maybe a note somewhere in the readme would save some struggling from future devs.

tswaters avatar Oct 24 '24 17:10 tswaters

Hello, thanks for your message.

In fact, this pool.query issue has already been discussed in the past. cf https://github.com/brianc/node-pg-copy-streams/issues/144#issuecomment-1456254690

the pool.query shorthand cannot be used with pg-query-stream because it does not handle the Submittable interface that client.query has.

my strategy was to document the async/await example explicitely using client.query but you are right that a supplementary note seems necessary to avoid this pitfall.

could you send a PR on a README modification to add the note/warning that you would have like to find and that would have avoided the issue for you ?

jeromew avatar Oct 24 '24 17:10 jeromew