I’m sorry that you are struggling to get a working solution. Can you provide some of the queries that you have developed so far? We often find that specific details of a query help to find a solution.
Fauna is a consistent database. Unlike the query model used by traditional relational databases, there is no cached temporary result for a query that can be paginated. When you receive a page of results, fetching the next page necessarily gets the latest values, since they might have changed between the first and second queries.
Typically, you don’t have to track details within a cursor. Simply use the value of the after
or before
cursor (depending on which direction you want to move) in subsequent queries.
For example:
Value page1 = client.query(
Map(
Paginate(Documents(Collection("People")))
.size(3),
Lambda("ref", Get(Var("ref")))
)
).get();
System.out.println("Page1:");
System.out.println(page1);
Value page2 = client.query(
Map(
Paginate(Documents(Collection("People")))
.size(3)
.after(page1.get(Field.at("after"))),
Lambda("ref", Get(Var("ref")))
)
).get();
System.out.println("Page2:");
System.out.println(page2);
With the “People” documents created in the Indexing tutorial, the output is:
Page1:
{after: [ref(id = "323053725221388800", collection = ref(id = "People", collection = ref(id = "collections")))], data: [{ref: ref(id = "323053725206708736", collection = ref(id = "People", collection = ref(id = "collections"))), ts: 1644346890610000, data: {first: "Alan", last: "Perlis", age: 97, degrees: ["BA", "MA", "PhD"], letter: "A"}}, {ref: ref(id = "323053725210903040", collection = ref(id = "People", collection = ref(id = "collections"))), ts: 1644346890610000, data: {first: "Alan", last: "Turing", age: 107, degrees: ["BA", "MA", "MS", "PhD"], letter: "B"}}, {ref: ref(id = "323053725217194496", collection = ref(id = "People", collection = ref(id = "collections"))), ts: 1644346890610000, data: {first: "Grace", last: "Hopper", age: 119, degrees: ["BA", "MA", "PhD"], letter: "C"}}]}
Page2:
{before: [ref(id = "323053725221388800", collection = ref(id = "People", collection = ref(id = "collections")))], after: [ref(id = "323053725233971712", collection = ref(id = "People", collection = ref(id = "collections")))], data: [{ref: ref(id = "323053725221388800", collection = ref(id = "People", collection = ref(id = "collections"))), ts: 1644346890610000, data: {first: "Leslie", last: "Lamport", age: 80, degrees: ["BS", "MA", "PhD"]}}, {ref: ref(id = "323053725225583104", collection = ref(id = "People", collection = ref(id = "collections"))), ts: 1644346890610000, data: {first: "Marvin", last: "Minsky", age: 92, degrees: ["BA", "PhD"], letter: 1}}, {ref: ref(id = "323053725229777408", collection = ref(id = "People", collection = ref(id = "collections"))), ts: 1644346890610000, data: {first: "Stephen", last: "Cook", age: 81, degrees: ["BS", "PhD"], letter: "F"}}]}
Notice that in the first page, only the after
cursor is present, since there are only pages following the current page in the result set. In the second page, both the before
and after
cursors are present, since there is additional data (from the perspective of page 2) in either direction.
All that changed between the first page and second page queries is the addition of .after(page1.get(Field.at("after")))
to the second page query. That inject the value of the after
cursor from the first page results into the query for the second page.
If you are concerned that page x has refreshed since you first fetched it, and you want to keep the pagination consistent in your UI, keep track of the cursors returned from each page, and then re-run page x’s query.
We definitely want to improve the developer experience when using our drivers. If you have any suggested code samples that would notably improve the experience, please add them here.