I want to paginate my query with Firestore because I have a very large collection (more than 20k documents growing). By pagination I mean displaying an array of numbers to my user, if he clicks on one, it redirects him to a specific chunk of my collection and only fetches this chunk when he clicks. For instance, if I have 100 documents and I want to display 10 per page, I have an array of numbers from 1 to 10. Each time the user clicks, it loads 10 products.
I have implemented infinite scrolling with ease thanks to the startAfter()
method with this doc on query cursors.
However, trying to follow the same documentation for pagination I just don't get it. I also tried to follow this tutorial.
This is my code so far:
let filteredProductsCollection = new Promise(function(resolve, reject) {
productsCollection
.orderBy("productName")
.where(
req.query.where.field,
req.query.where.comparison,
req.query.where.value
)
.startAfter(
req.query.lastProduct
)
.limit(req.query.limit)
.get()
.then(collection => resolve(collection))
.catch(err => reject(err));
}
Then I take the promise and save the last document
.then(snapshot => {
let snapshots = {
last
};
snapshot.forEach(doc => {
snapshots[doc.id] = {
...doc.data(),
id: doc.id
};
});
res.status(200).send(data)
})
This fetch the next chunk of products considering the current chunk (saved in lastProduct), allowing me to make an infinite scrolling that is working perfectly.
My main problem is that I don't have any clue how to jump from that to a pagination feature. Two main issues:
- I don't know how many documents I have in total in my query so I can't display an array of numbers to my user
- Even if I had this array, how would I retrieve a specific chunk that is not directly next to the previous chunk that has been loaded? Example, how to query page 8 if my user is currently on page 1?
It seems that all the tutorials are directly aimed to describe how to do pagination but I can't get it. Is my definition of pagination wrong? Or are they calling this feature pagination when it is really a 'get next chunk of data' feature...?