postgres
postgres copied to clipboard
Dynamic on conflict definition
I am struggling to set up the SET part of my on conflict definition dynamically. Everything else working smoothly, but failing to pass upsert information. Would welcome any help.
const targetTable = "test"
const columns = ["id", "name", "geometry"]
const onConflict = { keys: ["id"], update: ["name", "geometry"]}
await sql`
INSERT INTO ${sql(targetTable)} ${sql(data, columns)}
ON CONFLICT (${sql(onConflict.keys.join(","))}) DO UPDATE
SET ${sql(onConflict.update.map(column => `${column} = EXCLUDED.${column}`).join(",")};
`
I'm also interested