gary
gary3mo ago

long running queries

hi all, any idea how can i fix "This query or mutation function ran multiple paginated queries. Convex only supports a single paginated query in each function."?
export const queryItems = query({
args: {
pagination: paginationValidator,
},
handler: async (ctx, args) => {
let rawItemsA: {
category: string
label: string
value: number
quantity: number
imageUrl: string
}[] = []
let rawItemsB: {
category: string
name: string
value: number
logoUrl: string
}[] = []

for (const category of SUPPORTED_CATEGORIES) {
const itemsAChunk = await ctx.db
.query("itemsA")
.withIndex("by_category", (q) => q.eq("category", category))
.paginate(args.pagination)
const itemsBChunk = await ctx.db
.query("itemsB")
.withIndex("by_category", (q) => q.eq("category", category))
.paginate(args.pagination)
rawItemsA = [...rawItemsA, ...itemsAChunk.page]
rawItemsB = [...rawItemsB, ...itemsBChunk.page]
}

const groupedA = aggregateItemsA(rawItemsA)
const groupedB = aggregateItemsB(rawItemsB)

const combinedItems: CombinedItem[] = [
...Object.values(groupedA),
...Object.values(groupedB),
].sort((a, b) => {
const byValue = b.value - a.value
if (byValue !== 0) return byValue
return a.label.localeCompare(b.label)
})

return combinedItems
},
})
export const queryItems = query({
args: {
pagination: paginationValidator,
},
handler: async (ctx, args) => {
let rawItemsA: {
category: string
label: string
value: number
quantity: number
imageUrl: string
}[] = []
let rawItemsB: {
category: string
name: string
value: number
logoUrl: string
}[] = []

for (const category of SUPPORTED_CATEGORIES) {
const itemsAChunk = await ctx.db
.query("itemsA")
.withIndex("by_category", (q) => q.eq("category", category))
.paginate(args.pagination)
const itemsBChunk = await ctx.db
.query("itemsB")
.withIndex("by_category", (q) => q.eq("category", category))
.paginate(args.pagination)
rawItemsA = [...rawItemsA, ...itemsAChunk.page]
rawItemsB = [...rawItemsB, ...itemsBChunk.page]
}

const groupedA = aggregateItemsA(rawItemsA)
const groupedB = aggregateItemsB(rawItemsB)

const combinedItems: CombinedItem[] = [
...Object.values(groupedA),
...Object.values(groupedB),
].sort((a, b) => {
const byValue = b.value - a.value
if (byValue !== 0) return byValue
return a.label.localeCompare(b.label)
})

return combinedItems
},
})
3 Replies
Convex Bot
Convex Bot3mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Mordsith
Mordsith3mo ago
Yes. you can't paginate() more than once per function. I've faced this problem also. Can you explain what you're trying to achieve, maybe there can be a better way around your problem; the code looks like it may cause problems later...
erquhart
erquhart2mo ago
GitHub
convex-helpers/packages/convex-helpers at main · get-convex/convex...
A collection of useful code to complement the official packages. - get-convex/convex-helpers

Did you find this page helpful?