G
G
gelerum2022-01-25 13:45:15
Software design
gelerum, 2022-01-25 13:45:15

How to update the user in the database?

No attachment to language.
I have api, I send json with data for updating via endpoint (username, name, mail. password).
The problem is that not all data can be present in json, but only those that need to be updated.

I got the idea to check what data is present in the json file. And Separately, for each json field, call the update function of the corresponding column. But it's not efficient, is it? It is more efficient to send 1 request to update 4 columns.

How to implement a high-level update function?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2022-02-02
@Akela_wolf

If you have an ORM, then you load the entity, update the fields that came in the request, and save it to the database. Two requests (read and update).
If you work at the level of ordinary SQL queries, then you do something like:

UPDATE table SET field1=value1, field2=value2, ... WHERE id=idvalue;

where the block after SET is assembled dynamically depending on what came in JSON (and this is not at all necessary to do with text, for example, a library such as jooq provides a convenient API for building such requests)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question