Continuing a series of posts about the features of using OData protocol (see part 1 and part 2).
OData stipulates that the objects can be created and updated using standard HTTP request POST, PATCH and PUT. The request body should contain JSON description of the object that we want to update or add to the database.
If the object being added does not exist a new object is created.
If the object exists – the behavior is different:
- PUT – replaces the object with a new one
- PATCH – updates object fields with new values
- POST – behavior is not specified in the standard (in databoom POST is similar to PATCH)
4. Adding complex objects together with the linked objects
A few important phrases are lost in the text of the OData standard.
A request to create an entity that includes related entities, represented using the appropriate inline representation, is referred to as a “deep insert” … On success, the service MUST create all entities and relate them.
As a result, not everyone knows that OData enables you to add the objects with the child objects. For example, we can add a new book into the existing collection of books directly 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 the necessary objects will be created (or updated) and the links will be established between them. Then you can get a book, or a book with its authors; get the list of people (writers, experts), get people with their books, etc. (see. part 1, part 2)
5. Making connections between objects
- Having the ability to add objects with embedded objects, you can easily establish links between them.
In the example above, we added the book with the author, who had the property ‘likes’
“likes”: [{ “id”: “book186” }, { “id”: “book18” }, { “id”: “book31” }]
In accordance with the standard all the books will be created (or updated if they already exist), and all the relationships will be established between persons and their favorite books. As soon as the books have only ‘id’ field, it is nothing to update, it is enough just to set up the links.
- There is another way of setting up the links. List of favorite books of a person with id = ‘person191’ accessible via URL
…/persons(person191)/likes
We can add books by sending a request PUT, POST, or PATCH with this URL
Note: OData admits that an object can refer to many objects, and it can be referenced by many objects. For example, the book can have multiple authors as well as the author can write many books; a person may be 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 look through our documentation, REST API usage examples, and examples of using JavaScript library.
To be continued…