Skip to content

Fix pagination continueCursor at end of query #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion convex/pagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test("paginate", async () => {
{ author: "sarah", body: "hello2" },
]);
expect(isDone).toEqual(false);
const { isDone: isDone2, page: page2 } = await t.query(api.pagination.list, {
const { continueCursor: continueCursor2, isDone: isDone2, page: page2 } = await t.query(api.pagination.list, {
author: "sarah",
paginationOptions: {
cursor: continueCursor,
Expand All @@ -39,4 +39,15 @@ test("paginate", async () => {
{ author: "sarah", body: "hello5" },
]);
expect(isDone2).toEqual(true);

// Querying after done should return nothing.
const { isDone: isDone3, page: page3 } = await t.query(api.pagination.list, {
author: "sarah",
paginationOptions: {
cursor: continueCursor2,
numItems: 4,
},
});
expect(page3).toMatchObject([]);
expect(isDone3).toEqual(true);
});
5 changes: 4 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ class DatabaseFake {
const { value, done } = this.queryNext(queryId);
if (done) {
isDone = true;
// We have reached the end of the query. Return a cursor that indicates
// "end query", which we can do with any string that isn't a valid _id.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is magical but I'll take it

continueCursor = "_end_cursor";
break;
}
if (isInPage) {
Expand All @@ -387,7 +390,7 @@ class DatabaseFake {
return {
page,
isDone,
continueCursor,
continueCursor: continueCursor!,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible there won't be any result & it'll still be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope it's an infinite loop with two breaks and we set the variable before each break

};
}

Expand Down