Consider a table:
id is a integer, name is varchar and age is also an integer
id |name | age
______________
1. | one | 12
--------------
2. | two | 15
--------------
3. | thre| 16
--------------
In my node code, I have a json object:
const jsonFromNode = {
id: 1,
name: "new_one",
age: 12
}
I'm using a query builder, knex , and I'd like to use this json directly to update the row in the table, I was thinking something like this in node(I'm not sure if this will work though, and the following is in pseudocode):
const string = `
do $$
DECLARE
myjson JSON;
BEGIN
myjson := '${jsonFromNode}';
-- some psql method/function/procedure to update
-- the row where id=1 and age= 12
SomePlPgsqlFunctionOrStepOfProcedures(myjson); //update by passing json
END;
$$ language 'plpgsql';
`
So, the major problems are:
Is this possible that my idea will work?
What is the syntax used to fetch value of data in my json variable. E.g. x = {'a': 'b'} is my variable in psql above, how do I do something like this in psql: y = x['b']
What are the steps or functions that would replace:
SomePlPgsqlFunctionOrStepOfProceduresabove