What is the best way to implement a many-to-many relationship that contains meta, RESTfully?

When presenting data models via the RESTful interface, it is understood the creation of top-level endpoints that are associated with a type / group of objects:

  • of users
  • / cars

We can reuse HTTP verbs to activate actions on these groups (GET to list, POST to create, etc.). And when you imagine a model with a β€œdependency” (since it cannot exist without a β€œparent”), we can create deeper endpoints to represent this dependency relationship:

  • / users / [: identifier] / tokens

In this case, it makes sense not to have a top-level endpoint /tokens, since they cannot exist without a user.

Many-to-many relationships are becoming more complex. If two models can have many-to-many relationships, but can also really exist on their own, it makes sense to give both objects a top-level endpoint and a deeper endpoint to define this relationship:

  • of users
  • / cars
  • / users / [: identifier] / cars
  • / Cars / [: identifier] / users

Then we can use the PUT and DELETE methods to determine these relationships through the HTTP: interface PUT /users/[:user_id]/cars/[:car_id]. It makes sense that performing this PUT operation would create a data model that somehow connects two objects (for example, a connection table in a relational database).


, .

GET (GET /users/[:user_id]/cars/[:car_id])? , "" GET /cars/[:id]?

, , " " ? ?

, , , ? ? , REST , , ? - ?

Pls.: P ( , )

+4
2

IMHO.

/users/ [: user_id]/cars/, /cars/ [: car_id], .

, , " ?" , /users/ [: user_id]/cars/[: car_id].

, . , , , .

+3

. - users/[:user_id]/cars/[:car_id]/stats, {drivings_count: 123}. , GET .

+3

Source: https://habr.com/ru/post/1529003/


All Articles