Closed Smtih closed 4 years ago
If you give the mixin the nodes: true
option, query response will give you nodes that each contain a cursor. You can then use that cursor to get results before or after the node.
See quick start in the README for an example.
The issue is the cursor is based by the orderBy
fields.
Search is ordered by relevance to the search term so the cursor wouldn't make sense when used in a list ordered by date?
Perhaps this assumption is wrong?
Ok, in that case your current approach looks to be on the right track. Indeed, the cursor is based on the orderBy
fields, so you can do some tricks with where
statements to do what you want. If your chat messages have unique ids, then you can do the following:
const message = await Messages.query()
.where('id', /*messageId*/)
.where('chatId', /*chatId*/)
.orderBy('postDate'); // orderBy statement needed for cursor
where
statement to our liking, but the orderBy
statements must be the same:
const list = await Messages.query()
.where('chatId', /*chatId*/)
.orderBy('postDate')
.nextCursorPage(message.pageInfo.next);
Great, thanks for the info, will let you know how this approach goes.
I've got a question whether it is possible with this library to create a paginated list starting from a specific item. My use case is, a user does a search through chat messages. when they select a chat message, I then want to navigate to the chat at that specific message, allowing the user to paginate backwards or forwards.
From my understanding of cursor based pagination I think this should be possible, but I was wondering if there's a straightforward way to do it.
My current approach that I plan to try is something like this.
I'll report back if this even works, but if there's a smarter way to do this would love to hear.