I am trying to add some pagination functionality to my Express + Firebase Realtime Database SDK but I am not sure how to achieve this, and the documentation is not helping me. Also, all the examples I found are regarding Firestore.
Just as an example I have this model User which is accessed by database.ref('users'). Imagine I have 10 users whose IDs go from 1 to 10 respectively and I want to paginate 5 per page.
What I expect is get users whose keys go from 1 to 5, and then when someone clicks on page 2, it will get users whose keys go from 6 to 10.
According to the documentation, I understood that I should add something like the following:
(req, res) => {
const { key } = req.query;
let ref = database.ref('users')
.orderByChild('createdAt')
.limitToLast(5);
if (key) {
ref = ref.startAt(key);
}
ref.once('value')
.then(snapshot => ...);
}
What I get until now is that the difference between limitToLast() and limitToFirst() is the sorting, it's like ORDER BY createdAt DESC and ORDER BY createdAt ASC respectively.
If I set ref.startAt(5), the previous code doesn't work since I am getting the first five users (1 to 5).
What approach should I use? Thanks in advance.
Edit:
I got that if I do database.ref('users').orderByChild('createdAt').limitToLast(5).startAt(5) I get documents where createdAt is greater that 5 which is wrong. I should sort by date after getting those documents whose keys are next to 5.