Skip to content

Commit

Permalink
fix paginator endCursor when isDone (#440)
Browse files Browse the repository at this point in the history
fix paginator endCursor when isDone
  • Loading branch information
ldanilek authored Feb 7, 2025
2 parents e31e3cd + 8366611 commit c24fe3b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 9 additions & 0 deletions packages/convex-helpers/server/pagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ describe("paginator", () => {
await ctx.db.insert("foo", { a: 1, b: 3, c: 1 });
await ctx.db.insert("foo", { a: 1, b: 4, c: 1 });
await ctx.db.insert("foo", { a: 1, b: 4, c: 3 });
// First page has two items in it.
const result1 = await paginator(ctx.db, schema)
.query("foo").withIndex("abc", q => q.eq("a", 1).gt("b", 3).lte("b", 5))
.paginate({ cursor: null, numItems: 2 });
Expand All @@ -380,6 +381,7 @@ describe("paginator", () => {
{ a: 1, b: 4, c: 3 },
]);
expect(result1.isDone).toBe(false);
// Add an item to the first page and refetch with endCursor.
await ctx.db.insert("foo", { a: 1, b: 4, c: 2 });
const result2 = await paginator(ctx.db, schema)
.query("foo").withIndex("abc", q => q.eq("a", 1).gt("b", 3).lte("b", 5))
Expand All @@ -391,18 +393,25 @@ describe("paginator", () => {
]);
expect(result2.isDone).toBe(false);
expect(result1.continueCursor).toStrictEqual(result2.continueCursor);

// Second page has one item in it.
const result3 = await paginator(ctx.db, schema)
.query("foo").withIndex("abc", q => q.eq("a", 1).gt("b", 3).lte("b", 5))
.paginate({ cursor: result2.continueCursor, numItems: 2 });
expect(result3.page.map(stripSystemFields)).toEqual([
{ a: 1, b: 5, c: 1 },
]);
expect(result3.isDone).toBe(true);
// Add two items to the second page and refetch with endCursor.
await ctx.db.insert("foo", { a: 1, b: 5, c: 2 });
await ctx.db.insert("foo", { a: 1, b: 5, c: 3 });
const result4 = await paginator(ctx.db, schema)
.query("foo").withIndex("abc", q => q.eq("a", 1).gt("b", 3).lte("b", 5))
.paginate({ cursor: result2.continueCursor, endCursor: result3.continueCursor, numItems: 2 });
expect(result4.page.map(stripSystemFields)).toEqual([
{ a: 1, b: 5, c: 1 },
{ a: 1, b: 5, c: 2 },
{ a: 1, b: 5, c: 3 },
]);
expect(result4.isDone).toBe(true);
});
Expand Down
8 changes: 5 additions & 3 deletions packages/convex-helpers/server/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +545,12 @@ export class OrderedPaginatorQuery<
let endIndexKey = this.endIndexKey;
let endInclusive = this.endInclusive;
let absoluteMaxRows: number | undefined = opts.numItems;
if (opts.endCursor && opts.endCursor !== END_CURSOR) {
endIndexKey = jsonToConvex(JSON.parse(opts.endCursor)) as IndexKey;
endInclusive = true;
if (opts.endCursor) {
absoluteMaxRows = undefined;
if (opts.endCursor !== END_CURSOR) {
endIndexKey = jsonToConvex(JSON.parse(opts.endCursor)) as IndexKey;
endInclusive = true;
}
}
const {
page, hasMore, indexKeys,
Expand Down

0 comments on commit c24fe3b

Please sign in to comment.