Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination in firestore api

I am trying to paginate documents with firestore beta. I am following the official docs

I see there is a pageSize param to say how many documents you want to show, but i dont see any offset option. Does anybody know how can i do a pagination system?

thank you

like image 597
Dani Avatar asked Feb 20 '18 11:02

Dani


2 Answers

From your documentation link, I assume that you're using the REST API. In the REST API, there is a pageToken parameter, which you can specify. This can be derived from the nextPageToken returned from the previous request.

Previous response

{
  "documents": [
    {
      object(Document)
    }
  ],
  "nextPageToken": ABCDEF1234567890,
}

Next request

 projects/my-project/databases/my-database/documents or projects/my-project/databases/my-database/documents/chatrooms?pageSize=20&pageToken=ABCDEF1234567890
like image 113
Jason Berryman Avatar answered Oct 16 '22 03:10

Jason Berryman


Firestore range queries are based on an having anchor document. So you must know the (order-by properties of the) document that the range start with, and then use ref.startAfter(anchorDdoc).limit(10) (or ref.startAfter(valueOfAnchorDoc).limit(10)) to get the next page.

The Firestore server-side Admin SDKs have an offset() call that allows the server to determine the document to starts at. But the client-side SDKs do not have this method.

like image 1
Frank van Puffelen Avatar answered Oct 16 '22 02:10

Frank van Puffelen