I am using pg-promise to handle my Postgres queries and I am having troubles finding a solution to the below query:
I am trying to create a single method for batch upserting many rows at once, This is my code:
massUpsert: (orgId, entities) => db.tx((t) => {
const queries = [];
entities.forEach((entity) => {
const { id, name, age, type } = entity;
queries.push(
t.one(`INSERT INTO public.table (id, name, age, type)
VALUES ($1, $2, $3, $4)
ON CONFLICT (id) DO update
SET
name = $2,
age = $3,
type = $4
RETURNING *`,
[id, name, age, type]));
});
return t.batch(queries);
}),
Now the problem is that in some cases I get null for one or more of the fields and I want the DB to remain with old values instead of replacing it with a null.
Any suggestions?