0

I have an object with fields like ID, CODE, NAME, etc., and it has its own database table with each field. And I am working on implementing the REST API for this object. I want to expose all these fields but also the additional field TYPE, whose value depends on the CODE field. If CODE is in a valid list, it will have the value 'I' otherwise it will have the value 'C' (Custom).

In the create operation, the user should be able to specify the TYPE field and pass the CODE value accordingly.

My question is: What would be the standard approach to exposing this new TYPE field so the object can also scale with any new changes? Should I handle it from the backend code only by adding a new field TYPE and validating and setting its value based on the CODE value, or should I also add a new column in the table as TYPE to store the value based on the CODE value and expose it in the API? I believe this way application and database would have proper mapping between the fields.

Appreciate for any suggestions. Thank you.

1
  • Why would you want the user to set the TYPE when the clear rule of its mapping is already present in the back-end ?
    – S.D.
    Sep 13, 2023 at 7:13

1 Answer 1

1

There is no relation to how information is given on a REST API and how information is stored in a database.

It is entirely normal and accepted that a REST API presents data from multiple database tables (or even multiple databases) as a single entity and also that there are calculated properties in the entity.

As the TYPE field can be completely calculated from the existing information, it should not be stored in the database but re-calculated when needed. Depending on if it is needed by the internal logic of your backend or not, that calculation can be done as soon as the information is read from the database, or as late as when building the response in the REST API.

When a client tries to set a value for TYPE, you can silently ignore it, give an error that the field is read-only or give an error when the provided value is inconsistent with the value you would calculate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.