As a addition to my post here, and Azure tutorial here. what if I want to shape the returned data of GET operation to certain table. If you look at SCRIPT menu on a table, you’ll find “Read” operation. Here where you can put server script to shape returned data.
Let’s say you want to return only not-completed todo items. There’s a column in the TodoItem called “complete” that indicates the todo is completed or not. Value of “false” means not completed. So, you can write this server script on “Read” operation:
1 2 3 4 5 6 7 8 9 |
function read(query, user, request) { query.where(function() { return !this.complete; }); request.execute(); } |
Boom, and you’re done!
Off course you can define query from the client-side. For iOS app client, you can specify NSPredicate like this (just like the sample code):
1 |
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"complete == NO"]; |
and you get the same behavior.
So why should you use server script? Well, if your API service is public and accessed by non-controllable clients, you can only control the server-side. Put the logic in the server-side is the only way you can do to make sure the client apps play along with your rules.
That’s it. Enjoy!