OData REST API - Small Tricks (Part 1)
The OData protocol has many hidden advantages (although there are enough shortcomings). Starting with this article, we would like to share some small useful tricks of using the OData protocol.
All further examples use the following simple data scheme.
The test database is available on the databoom.space website .
Test data can be viewed using the OData URL samples.databoom.space/api1/sampledb/collections/allobjects
Suppose we want to get a list of books that a person likes.
To get a person with id 'person1', we can write
To get books that this person likes, add the likes property name to the URL
Now we can go further and find all the publishers that have published books that person1 likes
In the previous example, we got a list of books that people like with id 'person1'.
Now we want to get all the people who like the book with id 'book2'. Pay attention to the person through the likes property refers to books that he loves, and books do not refer to people who love them.
To get a book with id 'book2', we can write
We cannot write ... / books (book1) / persons_who_likes - the book does not have such a property
Then we write
_backlink (likes) is a kind of reverse link for the likes
likes link - that they love
_backlink (likes) - who loves
Some OData implementations assume that the developer must create a pseudo property named for example persons_who_likes and write a handler for such requests on his own. The _backlink operator is not part of the standard, but can sometimes greatly simplify the work.
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 library.
In the next post we will tell you how to filter objects according to their relationships.
All further examples use the following simple data scheme.
- persons
- books
- companies
person properties:
| book properties:
| company properties:
|
The test database is available on the databoom.space website .
Test data can be viewed using the OData URL samples.databoom.space/api1/sampledb/collections/allobjects
1. How to get an array of objects that someone refers to
Suppose we want to get a list of books that a person likes.
To get a person with id 'person1', we can write
... / persons (person1)
To get books that this person likes, add the likes property name to the URL
... / persons (person1) / likes
Now we can go further and find all the publishers that have published books that person1 likes
... / persons (person1) / likes / publisher
2. How to get an array of objects that refer to someone
In the previous example, we got a list of books that people like with id 'person1'.
Now we want to get all the people who like the book with id 'book2'. Pay attention to the person through the likes property refers to books that he loves, and books do not refer to people who love them.
To get a book with id 'book2', we can write
... / books (book2)
We cannot write ... / books (book1) / persons_who_likes - the book does not have such a property
Then we write
... / books (book1) / _ backlink (likes)
_backlink (likes) is a kind of reverse link for the likes
likes link - that they love
_backlink (likes) - who loves
Some OData implementations assume that the developer must create a pseudo property named for example persons_who_likes and write a handler for such requests on his own. The _backlink operator is not part of the standard, but can sometimes greatly simplify the work.
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 library.
In the next post we will tell you how to filter objects according to their relationships.