Skip to content
Open
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
155 changes: 155 additions & 0 deletions convex/pagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,158 @@ test("paginate", async () => {
expect(page3).toMatchObject([]);
expect(isDone3).toEqual(true);
});

test("paginate with deletes", async () => {
const t = convexTest(schema);
await t.run(async (ctx) => {
await ctx.db.insert("messages", { author: "sarah", body: "hello1" });
await ctx.db.insert("messages", { author: "michal", body: "boo" });
await ctx.db.insert("messages", { author: "sarah", body: "hello2" });
await ctx.db.insert("messages", { author: "sarah", body: "hello3" });
await ctx.db.insert("messages", { author: "sarah", body: "hello4" });
await ctx.db.insert("messages", { author: "michal", body: "boing" });
await ctx.db.insert("messages", { author: "sarah", body: "hello5" });
});
const { continueCursor, isDone, page } = await t.query(api.pagination.list, {
author: "sarah",
paginationOptions: {
cursor: null,
numItems: 2,
},
});
expect(page).toMatchObject([
{ author: "sarah", body: "hello1" },
{ author: "sarah", body: "hello2" },
]);
expect(isDone).toEqual(false);
await t.run(async (ctx) => {
await ctx.db.delete(page[1]._id);
});
const { isDone: isDone2, page: page2 } = await t.query(api.pagination.list, {
author: "sarah",
paginationOptions: {
cursor: continueCursor,
numItems: 4,
},
});
expect(page2).toMatchObject([
{ author: "sarah", body: "hello3" },
{ author: "sarah", body: "hello4" },
{ author: "sarah", body: "hello5" },
]);
expect(isDone2).toEqual(true);
});

test("paginate with deletes and indexes", async () => {
const t = convexTest(schema);
await t.run(async (ctx) => {
await ctx.db.insert("messages", { author: "sarah", body: "hello1" });
await ctx.db.insert("messages", { author: "jordan", body: "hello1" });
await ctx.db.insert("messages", { author: "jordan", body: "hello2" });
await ctx.db.insert("messages", { author: "jordan", body: "hello3" });
await ctx.db.insert("messages", { author: "jordan", body: "hello4" });
});
const { continueCursor, isDone, page } = await t.query(
api.pagination.listWithIndex,
{
author: "jordan",
paginationOptions: {
cursor: null,
numItems: 1,
},
},
);
expect(page).toMatchObject([{ author: "jordan", body: "hello1" }]);
expect(isDone).toEqual(false);
await t.run(async (ctx) => {
await ctx.db.delete(page[0]._id);
});
const {
isDone: isDone2,
page: page2,
continueCursor: continueCursor2,
} = await t.query(api.pagination.listWithIndex, {
author: "jordan",
paginationOptions: {
cursor: continueCursor,
numItems: 2,
},
});
expect(page2).toMatchObject([
{ author: "jordan", body: "hello2" },
{ author: "jordan", body: "hello3" },
]);
expect(isDone2).toEqual(false);
await t.run(async (ctx) => {
await ctx.db.delete(page2[1]._id);
});
const { isDone: isDone3, page: page3 } = await t.query(
api.pagination.listWithIndex,
{
author: "jordan",
paginationOptions: {
cursor: continueCursor2,
numItems: 1,
},
},
);
expect(page3).toMatchObject([{ author: "jordan", body: "hello4" }]);
expect(isDone3).toEqual(true);
});

test("paginate with deletes and composite index", async () => {
const t = convexTest(schema);
await t.run(async (ctx) => {
await ctx.db.insert("messages", { author: "sarah", body: "hello1" });
await ctx.db.insert("messages", { author: "jordan", body: "hello2" });
await ctx.db.insert("messages", { author: "jordan", body: "hello4" });
await ctx.db.insert("messages", { author: "jordan", body: "hello3" });
await ctx.db.insert("messages", { author: "jordan", body: "hello1" });
});
const { continueCursor, isDone, page } = await t.query(
api.pagination.listWithCompositeIndex,
{
author: "jordan",
paginationOptions: {
cursor: null,
numItems: 1,
},
},
);
expect(page).toMatchObject([{ author: "jordan", body: "hello1" }]);
expect(isDone).toEqual(false);
await t.run(async (ctx) => {
await ctx.db.delete(page[0]._id);
});
const {
isDone: isDone2,
page: page2,
continueCursor: continueCursor2,
} = await t.query(api.pagination.listWithCompositeIndex, {
author: "jordan",
paginationOptions: {
cursor: continueCursor,
numItems: 2,
},
});
expect(page2).toMatchObject([
{ author: "jordan", body: "hello2" },
{ author: "jordan", body: "hello3" },
]);
expect(isDone2).toEqual(false);
await t.run(async (ctx) => {
await ctx.db.delete(page2[1]._id);
});
const { isDone: isDone3, page: page3 } = await t.query(
api.pagination.listWithCompositeIndex,
{
author: "jordan",
paginationOptions: {
cursor: continueCursor2,
numItems: 1,
},
},
);
expect(page3).toMatchObject([{ author: "jordan", body: "hello4" }]);
expect(isDone3).toEqual(true);
});
26 changes: 26 additions & 0 deletions convex/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,29 @@ export const list = query({
.paginate(args.paginationOptions);
},
});

export const listWithIndex = query({
args: {
author: v.string(),
paginationOptions: paginationOptsValidator,
},
handler: async (ctx, args) => {
return await ctx.db
.query("messages")
.withIndex("author", (q) => q.eq("author", args.author))
.paginate(args.paginationOptions);
},
});

export const listWithCompositeIndex = query({
args: {
author: v.string(),
paginationOptions: paginationOptsValidator,
},
handler: async (ctx, args) => {
return await ctx.db
.query("messages")
.withIndex("author_body", (q) => q.eq("author", args.author))
.paginate(args.paginationOptions);
},
});
1 change: 1 addition & 0 deletions convex/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineSchema({
score: v.optional(v.number()),
})
.index("author", ["author"])
.index("author_body", ["author", "body"])
.searchIndex("body", {
searchField: "body",
filterFields: ["author"],
Expand Down
Loading