mirror of
https://github.com/trailbaseio/trailbase.git
synced 2026-05-05 16:09:40 -05:00
Add offset pagination parameter to JS/TS client and docs.
This commit is contained in:
@@ -285,7 +285,9 @@ Parameters:
|
||||
|
||||
* Pagination can be controlled via the following query parameters:
|
||||
* `limit=N`, with a built-in hard limit of 1024 to avoid abuse.
|
||||
* `cursor=<primary key>` to offset into results.
|
||||
* `cursor=<primary key>` to offset into results using a cursor. Significantly
|
||||
less expensive than `OFFSET`-based pagination.
|
||||
* `offset=N` to offset into results.
|
||||
* `count=true` will yield a `total_count` of records in the result. This can
|
||||
be used together with `limit` and `cursor` to build pagination UIs.
|
||||
* Ordering can be controlled using the `order=[[+-]?<column_name>]+` parameter, e.g.
|
||||
|
||||
@@ -16,6 +16,7 @@ export type User = {
|
||||
export type Pagination = {
|
||||
cursor?: string;
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
};
|
||||
|
||||
export type ListResponse<T> = {
|
||||
@@ -167,6 +168,9 @@ export class RecordApi {
|
||||
|
||||
const limit = pagination.limit;
|
||||
if (limit) params.append("limit", limit.toString());
|
||||
|
||||
const offset = pagination.offset;
|
||||
if (offset) params.append("offset", offset.toString());
|
||||
}
|
||||
const order = opts?.order;
|
||||
if (order) params.append("order", order.join(","));
|
||||
|
||||
@@ -227,6 +227,33 @@ test("expand foreign records", async () => {
|
||||
expect(comment.author.data?.name).toBe("SecondUser");
|
||||
expect(comment.post.data?.title).toBe("first post");
|
||||
}
|
||||
|
||||
{
|
||||
const response = await api.list<Comment>({
|
||||
expand: ["author", "post"],
|
||||
order: ["-id"],
|
||||
pagination: {
|
||||
limit: 2,
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.records.length).toBe(2);
|
||||
const second = response.records[1];
|
||||
|
||||
const offsetResponse = await api.list<Comment>({
|
||||
expand: ["author", "post"],
|
||||
order: ["-id"],
|
||||
pagination: {
|
||||
limit: 1,
|
||||
offset: 1,
|
||||
},
|
||||
});
|
||||
|
||||
expect(offsetResponse.records.length).toBe(1);
|
||||
const offsetFirst = offsetResponse.records[0];
|
||||
|
||||
expect(second).toStrictEqual(offsetFirst);
|
||||
}
|
||||
});
|
||||
|
||||
test("record error tests", async () => {
|
||||
|
||||
Reference in New Issue
Block a user