I have a data-model on the front-end that I want to insert/update in the database which of the following approaches (or neither!) is considered best practice:
# in webserver
some_model = {json}
some_url_executed('/', db_exec("select update_model(some_model);")
-- in database
create function update_model(json)
/* multiple tables to update for the model */
insert/update tables from json data
or option 2:
# in webserver
some_model = {json};
db_exec("begin transaction");
foreach field in some_model {
db_exec("update some_table set value = some_model.value
}
dbexec("commit or rollback");
My data-model, if it matters, looks like:
{ fld1: {},
...
fld5: { 'sub-fields' : { 'nested':'values' } }
fld6: [ {...}, {...} ]
}
I like the idea of letting the database handle this because I can grant permissions on that function to the user/group in the database (they shouldn’t be allowed to navigate to the url anyway) but, yea – could use some guidance.
Back to the question, is it a bad idea to loop through a json object and insert/update from a stored procedure? Where does this logic belong? I’m using postgresql 9.5.