HmzaH
Convex Community2y ago
2 replies
Hmza

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 ?
Was this page helpful?