Skip to content

Commit 5a3f3e3

Browse files
authored
Add pagination section (#84)
1 parent 1509ad7 commit 5a3f3e3

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

read.mdx

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ const db = new Polybase({ defaultNamespace: "your-namespace" });
2222
const collectionReference = db.collection("cities");
2323

2424
async function getRecord () {
25-
const { data, block } = await collectionReference.record("id").get();
25+
const record = await collectionReference.record("id").get();
26+
27+
// Get data from the record
28+
const { data } = record; // or const data = record.data
29+
30+
// Record is CollectionRecordResponse instance, so you can also get again to refresh
31+
const updatedRecord = record.get();
2632
}
2733
```
2834

@@ -74,6 +80,12 @@ const collectionReference = db.collection("cities");
7480

7581
export async function listRecordsWithFilter () {
7682
const records = await collectionReference.where("country", "==", "UK").get();
83+
84+
// Array of records is available under the data property
85+
const { data, cursor } = records;
86+
87+
// Records is QueryResponse, so we can use it to get the next page of results
88+
await records.next();
7789
}
7890

7991
```
@@ -110,3 +122,43 @@ const collectionReference = db
110122
}
111123
);
112124
```
125+
126+
## Pagination
127+
128+
You can paginate through your results using the cursor returned from the
129+
`.get()` method, or by using the built-in `.next()`
130+
131+
### Pagination with cursor
132+
133+
Use the cursor response with `.before()` and `.after()` to paginate through
134+
collection data.
135+
136+
```js
137+
const db = new Polybase({ defaultNamespace: "your-namespace" });
138+
const collectionReference = await db.collection("cities");
139+
140+
// First page
141+
const { data, cursor } = await collectionReference.get();
142+
143+
// Next page
144+
const next = await collectionReference.after(cursor.after).get();
145+
146+
// Previous page
147+
const previous = await collectionReference.before(cursor.before).get();
148+
```
149+
150+
### Pagination with next() or previous()
151+
152+
To simplify this process, a `next()` and `previous()` helper method is provided on the response.
153+
154+
```js
155+
const db = new Polybase({ defaultNamespace: "your-namespace" });
156+
const collectionReference = await db.collection("cities");
157+
158+
// First page
159+
const first = await collectionReference.get();
160+
161+
// Next pages
162+
const second = await first.next();
163+
const third = await second.next();
164+
```

0 commit comments

Comments
 (0)