question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: Named parameters in prepared statements / parameterized queries

See original GitHub issue

I would like it very much if we could name parameters in queries, instead of the current $1, $2, $3… nonsense. For example:

client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['John', 68, new Date(1944, 10, 13)]);

would become:

client.query("INSERT INTO beatles(name, height, birthday) values(:name, :height, :birthday)", {
  name: 'John',
  height: 68,
  birthday: new Date(1944, 10, 13)
});

It would allow for much cleaner code in more complex cases.

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Reactions:37
  • Comments:19 (12 by maintainers)

github_iconTop GitHub Comments

10reactions
bwestergardcommented, Nov 30, 2013

I’ve followed brianc’s advice and put together a monkeypatching solution that should work with node-postgres and node-postgres-pure alike.

https://github.com/bwestergard/node-postgres-named

5reactions
pihvicommented, Nov 16, 2016

Here’s another “cleaner” way to solve this with a library. No need for monkeypatching:

var sql = require('yesql').pg;

client.query(sql("INSERT INTO beatles(name, height, birthday) values(:name, :height, :birthday)")({
  name: 'John',
  height: 68,
  birthday: new Date(1944, 10, 13)
}));

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using parameterized queries - Amazon Athena
Prepared statements require two separate SQL statements: PREPARE and EXECUTE . First, you define the parameters in the PREPARE statement. Then, you run...
Read more >
Named Parameters for PreparedStatement - InfoWorld
A mapping is kept between parameter names and their indices. This mapping is referred to when the parameters are injected. The two classes...
Read more >
Using Prepared Statements - JDBC Basics - Oracle Help Center
This JDBC Java tutorial describes how to use JDBC API to create, insert into, update, and query tables. You will also learn how...
Read more >
Named parameters in JDBC [duplicate] - java - Stack Overflow
Reuse a parameter in a PreparedStatement? 3 · Sql query with bind variables execution in Jdbc · 5 · How to use named...
Read more >
Query Parameterization - OWASP Cheat Sheet Series
SQL Injection is best prevented through the use of parameterized queries. ... db.prepare "INSERT INTO users (name, age, gender) VALUES (?, ? ,?)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found