Implementation of list pagination

scala

#1

Hello,
I’m looking for ideas on how i can implement service supporting list pagination as explained here
My service description look like

...
service MyService {    
    rpc GetItems (GetItemsRequest) returns (GetItemsResponse) {
        option (google.api.http) = {
            get: "/api/items/{itemId}"
        };
    };
}

message GetItemsRequest {
    string itemId = 1;
    int32 page_size = 2;
    string page_token = 3;
}

message GetItemsResponse {
    repeated Item itemsList = 1;
    string next_page_token = 2;
}

message Item {
    string identifier = 1;
    int64 effectiveDate = 2;
    string itemLabel = 3;
}
...

Does this mean, that for each request my service have to manages a status ?


(Tim Moore) #2

The way you would implement this probably depends on the database you’re using. For example, Cassandra natively supports a serializable paging state that you could use in the page token.


#3

Hi Tim,
First of all sorry for the late response ;)

Unfortunately i’m using google cloud spanner whitch who does not support pagging state, so i changed the design of API, by binding page and size parameters to LIMIT & OFFSET sql query params.

Thank you


(Tim Moore) #4

That works. Another thing you could do would be to encode the page & size into a custom token format (it could even just be a string in a format like page=1&size=10) that you then unpack again in the call implementation, which would decouple the client from the internal implementation. The benefit of that would be that if you ever change to another database in the future that doesn’t support limit and offset queries, you wouldn’t have to change the client API, which just treats the next page token as some opaque string.