0

Let say I have a simple database as below:

Table 1. Car_registration_id (PK) | Owner | Registration_date
Table 2. Car_serial_number (PK) | Car_model | Car_registration_id (FK)

A user makes queries to retrieve a arbitrary views as below (it can be any other different views too).

# This view has no identifying column

View 1. Car_model | Owner 

# This view has at least 1 identifying column

View 2. Car_registration_id | Car_serial_number | Owner | Car_model

The constraints are:

  1. The front end is not aware of database schemas. Only send request via a provided API.

  2. All processing is left to the back end.

The question is, what is a strategy/pattern to design queries along with API endpoints in which a user can simply go to a view table on the front end, modify any number of cells (UPDATE or REMOVE only), then reflect the changes on database itself? The modification can be applied to different tables, columns, rows at once. In my use case, the database can have more than 10 tables so number of columns is not small.

I'm currently thinking of 2 approaches, but they don't seem to work:

  1. Create endpoints for every possible combinations, for request with or without identifying columns information. This will work, but at the cost of exponential amount of code to maintain, so not reasonable.

  2. Deliver identifying columns to the client no matter it appears in the view or not (just store in memory). Then use all information from client's request to construct queries respectively. However, as a user can modify an arbitrary amount of column, this leads to a complex condition/combination to build a query so it's also an issue. Also, how to separate the endpoints logically in this use case?

I'm using TypeORM, but using raw SQL is fine too. Any help is appreciated!

2
  • If I understand you correctly: I have built an API that searches "unknown" parameters .. I built 2 sets of communication .. 1) asks the "back end" what columns are in what table , and it returns a JSON object of the data structure to the front-end. 2) I then dynamically build the search form based on those fields, and 3) the user submits back to the API the parameters to "build" the query.
    – Zak
    Commented Dec 8, 2021 at 19:42
  • @Zak This may not be applicable to my use case. As in constraint stated above, front-end is not aware of the schema, so asking for columns is a violation. The schema is abstracted away via a data transfer object. Now, just let's theoretically say a front end is allowed to see the schema, what would be a strategy to efficiently/dynamically build a query then?
    – Hung Vu
    Commented Dec 8, 2021 at 20:47

0

Your Answer

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