Rayy
Rayy10mo ago

How to get results from paginate Queries?

I want to paginate the results of my query, I was unable to find any examples in the docs so can someone please help me with it? The query I want to paginate -
export const getChunks = internalQuery({
args: { chatId: v.id("chatbook") },
handler: async (ctx, args) => {
const chunks = await ctx.db
.query("chatbookChunks")
.withIndex("by_chatId", (q) => q.eq("chatId", args.chatId))
.take(50);
return chunks;
},
});
export const getChunks = internalQuery({
args: { chatId: v.id("chatbook") },
handler: async (ctx, args) => {
const chunks = await ctx.db
.query("chatbookChunks")
.withIndex("by_chatId", (q) => q.eq("chatId", args.chatId))
.take(50);
return chunks;
},
});
I want to get all the results of a specific chatId till we have reached the end, how can I get the results in here?
const chunks = await ctx.runQuery(internal.helper.chunks.getChunks, {
chatId: args.chatId,
});
const chunks = await ctx.runQuery(internal.helper.chunks.getChunks, {
chatId: args.chatId,
});
2 Replies
erquhart
erquhart10mo ago
You'll use .paginate() instead of .take() in your query, and you'll need to loop over ctx.runQuery() to get all of the pages. Check out the pagination docs here: https://docs.convex.dev/database/pagination
Rayy
RayyOP10mo ago
Ahh got it, just had to scroll all the way to the bottom to check how to use cursor. Thanks tho.

Did you find this page helpful?