noob saibotN
Convex Community10mo ago
3 replies
noob saibot

offset-based pagination with filtering

How can I implement an offset-based pagination with filter? User would like to fetch paginated data but have a away to limit the data by status.
Here is what I've implemented so far. First the 2 tables and the aggregation:
// tables
campaigns: {
  id,
  name, 
}
campaign_members: {
  id,
  user_id, 
  campaign_id,
  status: active, paused, completed, exited...etc
}
// aggregate
export const campaign_members_aggregate = new TableAggregate<{
  Key: number;
  Namespace: Id<"campaigns"> | undefined;
  DataModel: DataModel;
  TableName: "campaign_members";
}>(components.campaign_members_agg, {
  namespace: doc => doc.campaign_id,
  sortKey: doc => doc._creationTime,
});

Then the paginated query which is passed the offset and an optional status argument:
export const members_of_campaign = query({
 args: {
  campaign_id: v.id("campaigns"),
  offset: v.number(),  
  status: // optional status passed from client UI
 },
 handler: async (ctx, { campaign_id, offset, status }) => {
   // what's the approach to further filter by status
   const {} = await aggregate.at(ctx, offset, { namespace: campaign_id } )
 } 
})
Was this page helpful?