From c7f2589746f0537a1d060a907cdaba2596028ce2 Mon Sep 17 00:00:00 2001 From: Lee Danilek Date: Tue, 3 Sep 2024 23:58:04 -0400 Subject: [PATCH 1/2] pagination fix --- convex/pagination.test.ts | 14 +++++++++++++- index.ts | 3 ++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/convex/pagination.test.ts b/convex/pagination.test.ts index d5289f0..f3dc629 100644 --- a/convex/pagination.test.ts +++ b/convex/pagination.test.ts @@ -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, @@ -38,5 +38,17 @@ test("paginate", async () => { { author: "sarah", body: "hello4" }, { author: "sarah", body: "hello5" }, ]); + expect(continueCursor2).toEqual("_end_cursor"); 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); }); diff --git a/index.ts b/index.ts index 4596eb3..91eecb3 100644 --- a/index.ts +++ b/index.ts @@ -371,6 +371,7 @@ class DatabaseFake { const { value, done } = this.queryNext(queryId); if (done) { isDone = true; + continueCursor = "_end_cursor"; break; } if (isInPage) { @@ -387,7 +388,7 @@ class DatabaseFake { return { page, isDone, - continueCursor, + continueCursor: continueCursor!, }; } From 630e281c42ddd8b72c707ba77d4c1fc12649f799 Mon Sep 17 00:00:00 2001 From: Lee Danilek Date: Wed, 4 Sep 2024 00:00:51 -0400 Subject: [PATCH 2/2] improve comment --- convex/pagination.test.ts | 1 - index.ts | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/convex/pagination.test.ts b/convex/pagination.test.ts index f3dc629..c1f9755 100644 --- a/convex/pagination.test.ts +++ b/convex/pagination.test.ts @@ -38,7 +38,6 @@ test("paginate", async () => { { author: "sarah", body: "hello4" }, { author: "sarah", body: "hello5" }, ]); - expect(continueCursor2).toEqual("_end_cursor"); expect(isDone2).toEqual(true); // Querying after done should return nothing. diff --git a/index.ts b/index.ts index 91eecb3..eddd1cf 100644 --- a/index.ts +++ b/index.ts @@ -371,6 +371,8 @@ 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. continueCursor = "_end_cursor"; break; }