I’m writing a function in node.js to query a PostgreSQL table.
If the row exists, I want to return the id column from the row.
If it doesn’t exist, I want to insert it and return the id (insert into ... returning id).
I’ve been trying variations of case and if else statements and can’t seem to get it to work.
I would suggest doing the checking on the database side and just returning the id to nodejs.
Example:
And than on the Node.js-side (i’m using node-postgres in this example):