node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

Named Parameters - now supported in PostGres v9.2+

Open nfloersch opened this issue 11 years ago • 5 comments

It seems that node-postgres does still not support named parameters in queries. I realize that this was a problem a few years ago because PostGres itself did not support such a thing. But it does now. I would prefer to not have to use a different library/tool that extends node-postgres just to have better access to this functionality. Thanks!

nfloersch avatar Aug 19 '15 17:08 nfloersch

PostgreSQL doesn't support named parameters. It only supports positional parameters ($1, $2, ...etc) though you can reference the same parameter more than once. Ex: SELECT * FROM foo WHERE bar = $1 AND baz = $1

There's a couple of npm modules that wrap node-postgres to provide named parameter support listed as "Extras" on the README. Check out pg-db (I wrote it). It handles auto-returning connections to the pool, transactions, as well as named parameters. See here for some examples of using it with named parameters.

sehrope avatar Aug 19 '15 21:08 sehrope

So... what about...

http://www.postgresql.org/docs/devel/static/sql-syntax-calling-funcs.html

"PostgreSQL allows functions that have named parameters to be called using either positional or named notation."

Or any of the stuff in "4.3.2. Using Named Notation" of that document?

The named notation was added in 9.2, although the syntax has changed slightly since it was introduced.

I admit to being a non-guru with PostGreSQL... so maybe I just mis-understand the terminology.

nfloersch avatar Aug 19 '15 22:08 nfloersch

That's for user defined functions, not arbitrary SQL commands. When you execute SQL commands from node-postgres, or really any client including psql, you specify a SQL command such as a SELECT, INSERT, etc. Those can't have named parameters as the wire protocol only supports only numbered parameters.

sehrope avatar Aug 19 '15 22:08 sehrope

can't we build an abstract to transform named parameters in positional parameters?

sibelius avatar Jun 09 '17 11:06 sibelius

@sibelius There's plenty of existing libraries that support named parameters but it's beyond the scope of the driver as PostgreSQL itself (the server) does not support them. It, the server, only supports positional parameters and thus that's the interface that the driver provides.

sehrope avatar Jun 09 '17 12:06 sehrope