I am new to NodeJs and tried to use pg-promise to do all the requests to my PG database. I want to be able to update columns dynamically, meaning sometimes I will update only two columns for a row, sometimes I will update all of them, etc ... My input will be a JSON.
Since I want the endpoint to be able to update multiple rows, I tried using helpers namespace with ColumnSet.
Here is my Javascript code (inspired from previous stackoverflow answers):
/* logic for skipping columns: */
const skip = c => !c.exists || c.value === undefined;
/* all the columns of the table */
const column_structure = new dbconfig.pgp.helpers.ColumnSet(
[ '?id',
{name: 'firstname', skip},
{name: 'surname', skip},
{name: 'yob', skip}, // year of birth
{name: 'defensive_skill', skip},
{name: 'offensive_skill', skip},
{name: 'login', skip},
{name: 'password', skip}
],
{table: 'players'});
Here is the JSON I am feeding to the endpoint :
[{
"id" : 25,
"firstname": "Stephen",
"surname": "Harrison",
"yob": 1991,
"defensive_skill": 5,
"offensive_skill": 3,
"login": "harry",
"password": "123456"
},
{
"id": 26,
"firstname": "Chris",
"surname": "Jackson",
"defensive_skill": 5,
"offensive_skill": 4,
"login": "chris",
"password": "123456"
}
]
And here is the error :
Property 'yob' doesn't exist.
As you can see, in the second object of my array, I didn't specify the field 'yob'. I was expecting that for the second object, all the columns will be updated except the 'yob'. Is there something that I am doing wrong?