sveltefire
sveltefire copied to clipboard
Pagination when sort values are not unique
A common case for pagination is when the sort field is not always unique. For example, the city field could be the same for many members of a club or church. Or the last name field could contain many different "Smiths". See this Firestore video https://youtu.be/poqTHxtDXwU?t=277
The example given in the SvelteFire docs under the heading "Reactive" works perfectly well when all of the sort values are unique. However, when the sort field is not unique the pagination can skip records. For example, take the case where the sort field is city and there are one hundred records for that city. If the pagination shows 10 records at a time, a minimum of 90 records will be skipped because as soon as the first set for that city is displayed, the next page goes to the next city. The Firestore documentation actually talks about this case under the heading "Paginate a Query" on this page: https://firebase.google.com/docs/firestore/query-data/query-cursors
In this case the value assigned used in startAfter is not a sort value but the last record. In the "Reactive" example we see startAfter(last.flavor) instead of startAfter(last).
The work around is to use the code from the Firestore documentation and not use the Collection in SvelteFire in cases of non-unique sort fields.