Node-postgres: Trying to query without prior connect should throw an error

Created on 21 Mar 2019  路  5Comments  路  Source: brianc/node-postgres

I forgot to connect with client.connect() and spent more time than I should've when my queries silently failed.

Should an error be thrown when trying to use query without prior connect?

If so, I could probably whip up a PR.

Thanks!

All 5 comments

If you follow the recommended approach of using query on the connection object from the pool, then it becomes impossible to make a query before getting the connection first.

And what you're referring to is the obsolete approach to using the global query method, which you should no longer use anyhow.

@brianc This library should start producing a deprecation warning into the console upon the first call into the global query method, if it is still there. Or just remove it altogether.

I know I should've read the whole documentation (mea culpa), but the Welcome section of the docs uses the Client#query method in the examples and it doesn't mention that it is deprecated. I understand that a Hello world example should be as simple as possible, yet I don't think it should use obsolete approaches.

pg.connect and pg.query are already gone. Client#query isn鈥檛 deprecated. The client has a query queue that lets queries be issued before connecting, but I think it鈥檚 a good idea to remove that.

This burned me pretty bad today.

It appears if you call client.query() before you call client.connect(), this queues up a query to be executed whenever you may happen to call client.connect() in the future. If you forgot to call client.connect() (an honest mistake), this means you get back a promise that just never resolves.

This should really throw an error though. There is no connection to perform a query over. The intended feature of enqueuing queries to be performed after an upcoming connection event should reside under a different method, like enqueueQuery(). It's asking the client to perform a different sort of job. And I have to imagine this feature is only rarely used as intended.

I agree the query queue is weird....it's _very_ old. I think I based it on the original design of the mysql lib which has also undergone extensive changes throughout the years. I'm in favor of removing it as well...it seemed like a good idea at the time, but it wasn't. It's just a weird artifact from long ago. Obviously a breaking change to remove it, but probably wont bite that much since like you I doubt many people use it other than accidentally.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AhmedBHameed picture AhmedBHameed  路  3Comments

ossdev07 picture ossdev07  路  3Comments

chrisjensen picture chrisjensen  路  4Comments

gpanainte picture gpanainte  路  3Comments

KeynesYouDigIt picture KeynesYouDigIt  路  3Comments