I have REST API implemented following the principle that rest just gives back the basic documents and refs in those documents howto get other things etc etc
For example a /car/5 will give me model:blabla , user_id: 1 and then if you need the owner you'll get /user/1 to get user data..
This way JOINS and stuff are avoided in DB..all things are linking between themselves and data interconnected on rest client part - keeping things simple easy to cache/drop cache, scale etc.
But what happens when you need sorting?
Imagine we have some view on frontend to display following data: Car model, User name, etc... and you wanna sort by user name for example.
You can't really tell the /car/5 to sort by username cause it only knows of user ids...
One option I see is sorting from user /user/list?sortby=username and then interconnecting which of those returned ids actually refer to car. but this means we need to get ALL users..and only use fraction of those which seems killer performance bottleneck.
Thanks for any pointers
Filtering, Sorting, and Pagination to Retrieve the Data Requested. Filtering, sorting, and pagination are critical features of successful REST APIs. They allow you to retrieve only the data you want in a simple, efficient manner. Filtering allows you to retrieve only those resources needed by your application.
Like filtering, sorting is an important feature for any API endpoint that returns a lot of data. If you're returning a list of users, your API users may want to sort by last modified date or by email.
I think you're trying to avoid joining for all the wrong reasons, as by taking this approach, you're going to have to service a lot more requests.
If you instead bring back all the information that you'd show (i.e. do the joins database side), then the clients are going to have to make less queries, and you could do your sort without having to (lazy) load all the child objects.
The other option would be to bring back the children objects as child XML elements (in a similar manner to how OpenStreetMap's RESTful interface works). You still have a single hit from the client, and you should be able to optimise the queries to minimise load on the DB.
If you are avoiding all joins in the server and leaving it up to the client then in this case you must leave sorting to the client too. This is because a join has to take place in order to sort the cars by Username.
You can create another resource called CarsAndOwners or similar which returns the joined list, at which point it becomes reasonable to sort on the server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With