I have the following code for pg-promise that works:
function putUser(req, res, next) {
var values = req.body
var query = 'insert into users(fullname, fname, lname, email, region, area, labname, labcode) values(${fullname}, ${fname}, ${lname}, ${email}, ${region}, ${area}, ${labname}, ${labcode})'
db.none(query, values)
...
When I try to add a second user w/ same fullname I get --> "duplicate key value violates unique constraint "users_fullname_key"
Which is sorta what I want, but I actually don't want it to error, just update the user and report back OK.
I read this SO post --> how do I insert many, if it already exists, dont do anything with PG-promise
and it states I should be able to add ON DUPLICATE KEY UPDATE
but when I add that, I get "error near DUPLICATE" message.
I also read about pgp.helpers
. but I'd prefer to just run straight SQL, not use helpers as I'm trying to avoid "middlemen" as much as possible, hence why I moved away from using an ORM like TypeORM.
Here is my setup SQL Statement FYI:
CREATE TABLE users (
ID SERIAL PRIMARY KEY,
fullname VARCHAR UNIQUE NOT NULL,
...