Hmza
Hmza4mo ago

i had to calculate data from db. and i ended up using pagination with mutation. fallbacks ?

convex export const getAllDataWithDetails = mutation({ args: { paginationOpts: paginationOptsValidator, filterType: v.string() }, handler: async (ctx, args) => { const { filterType, paginationOpts } = args; const data = await ctx.db .query("data") .filter(q => q.eq(q.field("type"), filterType)) .order("desc") .paginate(paginationOpts); return data; }, }); react const getAllDataWithDetails = useMutation(api.data.export.getAllDataWithDetails); let allData = []; let hasMore = true; let cursor = null; while (hasMore) { const result = await getAllDataWithDetails({ filterType: selectedFilter, paginationOpts: { cursor, numItems: 5000 }, }); allData = [...allData, ...result.page]; hasMore = !result.isDone; cursor = result.continueCursor; } is this all right todo? any fallbacks or suggestions ?
2 Replies
lee
lee4mo ago
if you really need all the data, this is a fine pattern. A couple notes: 1. That can be a query instead of a mutation. If you don't want it to be reactive (and call loadMore) you can do const convex = useConvex(); and then do convex.query in place of the mutation. 2. That filter looks like it could be an index
Hmza
HmzaOP4mo ago
eureka

Did you find this page helpful?