Skip to content

filter helper when used in conjunction with pagination and a simple text filter misses results #864

@willemclarke

Description

@willemclarke

In this discord thread (https://discord.com/channels/1019350475847499849/1385358143437017178) I surfaced a problem I was having whereby using the filter helper on a query thats paginated will miss results when doing a simple string filter.

Given a query like this:

export const paginate = query({
  args: {
    paginationOpts: paginationOptsValidator,
    searchTerm: v.optional(v.string()),
  },
  handler: async (ctx, args) => {
    const term = args.searchTerm?.toLowerCase();

    return filter(ctx.db.query("users"), (user) => {
      return term ? user.name.toLowerCase().includes(term) : true;
    })
      .order("asc")
      .paginate(args.paginationOpts);
  },
});

And a callsite like this:

 export default function Home() {
  const [searchTerm, setSearchTerm] = useState("");

  const term = searchTerm === "" ? undefined : searchTerm;

  const { results: users, isLoading } = usePaginatedQuery(
    api.queries.paginate,
    {
      searchTerm: term,
    },
    { initialNumItems: 10 }
  );

...
}

Versions:

 "dependencies": {
    "convex": "^1.29.3",
    "convex-helpers": "^0.1.106"
  }

When I search for a user thats not on the first page, it never seems to find a result. I have seeded 50 users, showing 10 for the initial page size. If I search for anyone thats not on the first page, but is definitely in the data, I can never find them.

Reproduction repo:
https://github.com/willemclarke/convex-filters-pagination-reproduction

Steps:

  • git clone
  • bun install or npm install
  • npx convex dev
  • npx convex import --table users sampleUsers.jsonl
  • bun dev or npm run dev
  • Search for "Kevin White" or anyone below "Julia Jackson" in the sampleUsers.jsonl file -> it should not find them.

Now I really want to apologise if I'm blatantly mis-configuring anything here, but I essentially took this code from https://stack.convex.dev/complex-filters-in-convex#escape-hatch-pagination. Any help is appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions