We continue a series of posts about the features of using the OData protocol (see
Part 1 ,
Part 2 ).
OData stipulates that objects can be created and updated using the standard http POST, PATCH and PUT request. The request body must contain a JSON description of the object that we want to update or add to the database.
If the added object does not exist, then a new object is created.
If the object exists, then the behavior is different:
- PUT - replaces the object with a new one.
- PATCH - update field objects with new values
- POST - behavior is not specified in the standard (in databoom POST is similar to PATCH)
4. add complex objects along with related objects
In the text of the OData standard, several important phrases are lost:
It is a request to create a “deep insert” ...
As a result, not everyone knows that OData allows you to add objects along with child objects. For example, we can add a new book to the collection of books right away with its authors:
')
{ "id": "book1", "title": "Cannibal's Cookbook", "author":[ { "id": "person45", "collections": [{ "id": "persons" }, { "id": "writers" }], "firstname": "John", "lastname": "Doe", "age": 69, "likes": [{ "id": "book55" }, { "id": "book19" }, { "id": "book66" }] }, { "id": "person191", "collections": [{ "id": "persons" }, { "id": "experts" }], "firstname": "Lamar", "lastname": "Courtenay", "age": 37, "likes": [{ "id": "book186" }, { "id": "book18" }, { "id": "book31" }] } ] }
In accordance with the standard, all necessary objects will be created (or updated) and a connection will be established between them. After that, you can get a book, or a book with its authors, get a list of people (which will include new people), writers, experts, get people with their books, etc. (see
part 1 ,
part 2 )
5. establishing connections between objects
1. Having the ability to add objects along with nested objects, you can
simply establish links between them .
In the above example, we added a book with an author who had a field like
"Likes": [{"id": "book186"}, {"id": "book18"}, {"id": "book31"}]
In accordance with the standard, all books will be created (or updated if they already exist) and connections are established between the person and the favorite books. But since the books have only the id field, there is nothing to update - all that remains is to make connections.
2. There is
another way to establish relationships .
A list of favorite books of a person with id = 'person191' is available via URL
... / persons (person191) / likeswe can add books by sending a PUT, POST or PATCH request using this URL
Note: OData assumes that objects can refer to many other objects and many objects can refer to them at once. A book can have several authors, and an author can write many books. A person can be both a student and a teacher at the same time. Objects can belong to many collections at once.
If you are interested in this post, you can also see our documentation and examples of using the
REST API , as well as examples using the
JavaScript libraryTo be continued…