I'm using DynamoDB and NodeJS to enlist some objects on the UI. The list is long, and since DynamoDB can scan/query at most 1MB of data at a time, I've decided to use pagination so at the front end I have Previous
and Next
buttons to paginate back and forth from the current page.
My problem is I want to retrieve 20 items at a time from Table X
by using DynamoDB Query
feature based on a chosen DynamoDB Index
.
Let's say I've just fetched the initial 20 results (0-20) so on clicking next button I want to fetch results: 21-40 and so on. Also, I want to enable backwards paginating so when I'm on a page which shows results: 41-60, the Back
button would again fetch results: 21-40.
As per my understanding, DynamoDB doesn't support numerical offset.
How do I implement backward and forward pagination? I am a newbie in DynamoDB, please help me out.
Does DynamoDB support pagination? Yes. It supports pagination. But not in the traditional means, where you will get the total number of records to derive the page count.
ExclusiveStartKey. The primary key of the first item that this operation will evaluate. Use the value that was returned for LastEvaluatedKey in the previous operation. The data type for ExclusiveStartKey must be String, Number, or Binary. No set data types are allowed.
The Query operation took less than 65 milliseconds at the 95 percentile, compared to 650 milliseconds for Scan. In comparison to a query that conducts a straight lookup based on the partition key, response times vary substantially depending on the item and how the scan process works.
DynamoDB supports two different types of read operations, which are query and scan. A query is a lookup based on either the primary key or an index key. A scan is, as the name indicates, a read call that scans the entire table in order to find a particular result.
While DynamoDB will Paginate your data in forward direction only, You'll have to deal with the Backward Pagination in Front End.
For large Tables
(exceeding 1MB size), what DynamoDB
does:
LastEvaluatedKey
to fetch the next set of data or the Next Page. This value is used as Pagination Key in Front End to paginate back and forth.
LastEvaluatedKey
holds the value of the last object fetched from DynamoDB during aScan
orQuery
.
What you need to do (in Back End):
LIMIT
property of DynamoDB Query
to specify you want only 20 items.ExclusiveStartKey
property of DynamoDB Query
to specify that next set of data would start from the specified value of this property. What you need to do (in Front End):
Keep an array of objects arr[]
to host Pagination Keys.
Keep a variable page
initialized to -1, whose value will indicate the current page user is on.
LastEvaluatedKey
, push it into the arr
and increment page
.Now, you have a single page and
page
indicates you're on Page 0 andarr
contains Page Key of next page.
Code of Next
Button should follow the logic:
Request your server to fetch the next page using ExclusiveStartKey = arr
[page
]
When results of next page arrives, you'll again have another LastEvaluatedKey
, so again push it into arr
and increment page
. So you get the picture here, how we save the Page Keys.
Code of Back
Button should follow the logic:
Since page
variable indicates the Current Page so page - 1
would indicate the previous page. So:
if (page-1
>=0) Request your server to fetch the next page using ExclusiveStartKey = arr
[page - 1
]
You'll have to manage when Back
& Next
Buttons are available for clicking by using arr[]
and page
variables after each page is fetched.
The pagination feature in DynamoDB relies on the LastEvaluatedKey. You should be able to do what you want with that and use the page size to always be 20 items. It just won'y be numerical offset per se.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With