limiting json_agg

Having a bit of a skill issue limiting the amount of data returned from json_agg

      const query = ctx.db
        .select({
          ...groupByFilters,
          rowCount: sql<number>`count(${schema.returns.id})`.as(
            "row_count",
          ),
          data: sql`json_agg(${schema.returns})`,
        })
        .from(schema.returns)
        .groupBy(...Object.values(groupByFilters))
        .limit(limit + 1); // limit the amount of groups returned


I tried to add a subquery, but I can't seem to get it right
Was this page helpful?