punn
punn2y ago

Pagination with sort

Is there a way to have a paginated query return items in some specific sorted order. I'm trying to query messages using paginate() and sort them by messageId instead of asc/desc in creation time
4 Replies
lee
lee2y ago
you can query with a sorted order by adding an index on messageId and querying withIndex, then calling paginate() on that should give you the paginated messages in that order
punn
punnOP2y ago
I have two properties conversationId and messageId. My messages are indexed by conversationId, so when I query using withIndex, so the returned array of documents aren't sorted by messageId.
lee
lee2y ago
You can make a multi-field index on ["conversationId", "messageId"], and do the query
db.query("messages").withIndex("by_conversation", q => q.eq("conversationId", conversation)).order("desc").paginate()
db.query("messages").withIndex("by_conversation", q => q.eq("conversationId", conversation)).order("desc").paginate()
to get pages of the messages within a conversation by message id descending
punn
punnOP2y ago
ah okay makes sense thank you let me give that a shot

Did you find this page helpful?