Help me better understand paginated queries
I'm having trouble running a paginated query in server using the
filter
from the convex-helpers
library when, in client, I give a small value to initialNumItems
.
I have 2 tables:
table campaigns
table members
Now I want to find all the campaigns where "user_id" is part of, using a paginated query. So I write this function:
The last query will return data only if I give a bigger value to initialNumItems
: with value 3
it returns 2 records, but with 10
then it find the 3 records I'm expecting.
It seems to me that the filtering I'm applying is messing up the cursor and perhaps the first record found by the pagination is beyond the initial cursor?6 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
To most efficiently fetch this data, paginate over the members table, then do a
db.get
for all the associated campaigns. As is, it's walking the entire campaigns table and applying the filter to each row, so it might be getting exhausted in how many rows it can fetch before it just returns what it's found so far.ah yeah, the code for paginate in the filter helper is just applying the filter to the paginated results: https://github.com/get-convex/convex-helpers/blob/main/packages/convex-helpers/server/filter.ts#L55
GitHub
convex-helpers/packages/convex-helpers/server/filter.ts at main · g...
A collection of useful code to complement the official packages. - get-convex/convex-helpers
@ian Thanks for the reply. I refactored my code to paginate over the members table. It is working as expected. But the fact that
paginate
applies the filter after collecting the data, doesn't this lead to errors? For example in my case, given the user_id, these are the 4 data in the members table:
Now with initialNumItems = 3
and the below (simplified) query:
If I run the above query without adding .order("descr")
, I'm observing that the records mem_4, mem_3, mem_2
are selected. The consequence is the query returns only 2 records (because mem_4
is not associated with a campaign). But if I apply .order("descr")
then I get the 3 expected records: so the order of pagination matters. I would expect convex
to, first, find all records of the filter and then apply pagination (that's how to preview system I used - Hasura + Postgres
- worked, unless I'm mistaken).
The next observation: I decided to not user the helper function filter
and reverted to the built-in ctx.db.query
. And it seems to work as expected even without applying .order("descr")
. The below query does return 3 records even without order:
not sure if this is a typo, but
.order("descr")
isn't a thing I know of. .order("desc")
is the way to reverse the ordering. I'm not sure why you're seeing different results with different orders, maybe to do with the typo, but ordering shouldn't affect anything. If you have a repro of that it'd be great to get to the bottom of. Glad the pagination is working for you!yes, that's just a typo, in my code I don't have a typo.