I am using AWS for hosting an API with API Gateway and DynamoDB direct integration.
I am trying now to add the pagination feature for my app, and I am having a hard time to implement it 100%. The problem that I'm facing is when I scan backwards, and I'll give you an example to understand the problem better.
Imagine that I have a list of 20 items and a page size 5.
- When I query my table, the first query will return
1, 2, 3, 4, 5
. Expected behavior! - When I query my table, passing it the lastEvaluatedKey from the fifth element, it returns
6, 7, 8, 9, 10
. Expected behavior. - Now, my problem stands when I query using the flag
ScanIndexForward=false
. This means that I don't want to move forward. Instead, I want the previous items from thelastEvaluatedKey
. The thing is, if I use thelastEvaluatedKey
retrieved from my last query, instead of having something like5, 4, 3, 2, 1
, I have4, 3, 2, 1
.
The element from the lastEvaluatedKey
or call it head
if you want, is skipped.
If you imagine a normal behavior inside a table, where you're clicking next page
and previous page
, that would mean that when you go to the second page, and come back to the first page, you'll get only 4 items, instead of one, for the proposed scenario.
Finally, I have tried the same query with the AWS CLI
and the result is the same. After having a chat with the Support guys, they confirmed that this is the expected behavior.
I just want to know how people treat this scenario, since I know that Amazon uses it in production, so there should be a way of doing that.