-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathpaginate.js
35 lines (29 loc) · 1.15 KB
/
paginate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// This snippet file was generated by processing the source file:
// ./firestore-next/test.firestore.js
//
// To update the snippets in this file, edit the source and then run
// 'npm run snippets'.
// [START paginate_modular]
import { collection, query, orderBy, startAfter, limit, getDocs } from "firebase/firestore";
// Query the first page of docs
const first = query(collection(db, "cities"), orderBy("population"), limit(25));
const documentSnapshots = await getDocs(first);
// Get the last visible document
const lastVisible = documentSnapshots.docs[documentSnapshots.docs.length-1];
console.log("last", lastVisible);
// Construct a new query starting at this document,
// get the next 25 cities.
const next = query(collection(db, "cities"),
orderBy("population"),
startAfter(lastVisible),
limit(25));
// Get the first visible document
const firstVisible = documentSnapshots.docs[0];
console.log("first", firstVisible);
// Construct a new query starting at this document,
// get the previous 25 cities.
const previous = query(collection(db, "cities"),
orderBy("population"),
endBefore(lastVisible),
limitToLast(25));
// [END paginate_modular]