Node-postgres: how to prepare a statement without executing a request?

I want to create a “prepared statement” in postgres using the node-postgres module. I want to create it without binding to parameters, because the binding will be done in a loop.

In the documentation, I read:

query(object config, optional function callback) : Query
If _text_ and _name_ are provided within the config, the query will result in the creation of a prepared statement.

I tried

client.query({"name":"mystatement", "text":"select id from mytable where id=$1"});

but when I try to pass only the text and names in the config object, I get an exception:

A (translated) message is a required parameter 0, but a prepared statement expects 1

Is there something I am missing? How to create / prepare an instruction without reference to a certain value in order to avoid repeated preparation of an instruction at each step of the cycle?

+5
source share
3 answers

node -postgres.

node -postgres , . "" .

node -postgres , . libpq / ( javascript), API. , API - . , , ( ).

+9

: , . .

; "" , , , . , , . , , .

+2

You can use pg-prepared for this:

var prep = require('pg-prepared')

// First prepare statement without binding parameters
var item = prep('select id from mytable where id=${id}')

// Then execute the query and bind parameters in loop
for (i in [1,2,3]) {
  client.query(item({id: i}), function(err, result) {...})
}
0
source

All Articles