No batch insert and update query support
Want to pass multiple insert values in to single query. Is there any other way to do it?
//yes you can simply do that using bluebird and pg var promise = require('bluebird'); var options = { promiseLib: promise }; var pgp = require('pg-promise')(options); var db = pgp(cn); //cn is your connection string
// array is you array of values and first argument to t.query is your query using individual values of array in u
db.tx(function(t){
var queries=array.map(function(u){
return t.query('', u);
});
return t.batch(queries);
})
.then(function(){
//res
})
.catch(function(error){
//error response
});
Want to pass multiple insert values in to single query. Is there any other way to do it?
You can concatenate inserts into a single insert query. For example, see here.
I am not aware of a way to batch update into a single query. For batch insert, see #957.
https://github.com/brianc/node-postgres/issues/957#issuecomment-295583050
case this still helps: you can use temp tables to achieve this - like running those two statements:
create temp table coffeeTmp on commit drop as select 'Homer' as name, 'latte' as coffee union select 'Marge' as name, 'expresso' as coffee
update coffeeOrders c
set coffee=t.coffee
from coffeeTmp t
where c.name=t.name
when running via node-postgres, it would become sth like
c.query('begin')
c.query('create temp table coffeeTmp on commit drop as select $1 as name, $2 as coffee union select $3 as name, $4 as coffee', ['Homer', 'latte', 'Marge', 'expresso'])
c.query('update coffeeOrders c set coffee=t.coffee from coffeeTmp t where c.name=t.name')
// (more here)
c.query('commit')
just BTW: insert would work as well - and so would upsert by running above update followed by an insert like
insert into coffeeOrders (name, coffee)
select t.name, t.coffee from coffeeTmp t left join coffeeOrders c on t.name=c.name where c.name is null