I am implementing 'PATCH' on the server-side for partial updates to my resources.
Assuming I do not expose my SQL database schema in JSON requests/responses, i.e. there exists a separate mapping between keys in JSON and columns of a table, how do I best figure out which column(s) to update in SQL given the JSON of a partial update?
For example, suppose my table has 3 columns: col_a
, col_b
, and col_c
, and the mapping between JSON keys to table columns is: a -> col_a, b -> col_b, c -> col_c
. Given JSON-PATCH
data:
[
{"op": "replace", "path": "/b", "value": "some_new_value"}
]
What is the best way to programmatically apply this partial update to col_b
of the table corresponding to my resource?
Of course I can hardcode these mappings in a keys_to_columns
dict somewhere, and upon each request with some patch_data
, I can do sth like:
mapped_updates = {keys_to_columns[p['path'].split('/')[-1]]: p['value'] for p in patch_data}
then use mapped_updates
to construct the SQL statement for DB update. If the above throws a KeyError
I know the request data is invalid and can throw it away. And I will need to do this for every table/resource I have.
I wonder if there is a better way.