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

Doesn't seem to work with pool.query #159

Open
tswaters opened this issue Oct 24, 2024 · 1 comment
Open

Doesn't seem to work with pool.query #159

tswaters opened this issue Oct 24, 2024 · 1 comment

Comments

@tswaters
Copy link

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.

@jeromew
Copy link
Collaborator

jeromew commented Oct 24, 2024

Hello,
thanks for your message.

In fact, this pool.query issue has already been discussed in the past. cf #144 (comment)

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 ?

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