question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implementing pagination with DynamoDB

See original GitHub issue

I’ve decided to make a pagination in my app. But I’ve troubles with the backward paginating.

My resolvers are connected to the DynamoDB. When you want to get the previous “page” of your data you will need specify "scanIndexForward": false in your query to switch scan direction throw the data table. This attribute works only with "Query" operation not with "Scan". And the troubles become here because in "expression" attribute you’ll need set expression with the following condition:

The partition key equality test is required, and must be specified in the following format:

partitionKeyName = :partitionkeyval

If you also want to provide a condition for the sort key, it must be combined using AND with the condition for the sort key. Following is an example, using the = comparison operator for the sort key:

partitionKeyName = :partitionkeyval AND sortKeyName = :sortkeyval

as says DynamoDB reference. So when I’m specifying partitionKeyName = :partitionkeyval I’ll getting only one item and I can’t get multiple items because of “=” operator in "expression". Operation "Scan" returns multiple items but it doesn’t support "scanIndexForward" option.

Am I doing all right? Or is there another way to implement prev pagination?

And is it possible to implement pagination with the numbered pages not only previous/next ones?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

14reactions
ffxsamcommented, Mar 3, 2018

I’m no DynamoDB expert, but doesn’t scanIndexForward simply reverse the list of results? So if your data comes back A-Z, scanIndexForward: false will make it come back Z-A. It doesn’t paginate backwards.

I’m using pagination in my Vue app with AppSync, so maybe I can help with a couple things.

First off, you hardly ever want to use the scan operation on DynamoDB. It reads every single item in your table, which will wind up consuming your read capacity. In 95% of the cases, you want to use query to list a bunch of things.

This leads to the next question: how do you query a bunch of items instead of just a single item (partition key)? Your data may not be structured correctly. As a rudimentary example, let’s say you have a database of gadgets, and each gadget is owned by a single user. Thinking in terms of traditional databases, you might have the primary key be gadgetId, but in DynamoDB this is not correct. This is how you might structure it:

userId (partition key) gadgetId (sort key) name
0515dc74 84418926 PlayStation 4
0515dc74 1db1ec1a thermal camera
0515dc74 c9e2c1a8 EMP gun
0515dc74 4905669b laser

So the user with ID 0515dc74 logs in, and you want to show them all the gadgets they own. You would do a query operation on just the partition key, userId = 0515dc74. This will return the four items above. If you want to grab just one specific one, you’d do a GetItem request on both the partition key and sort key.

Now onto pagination.

You’re correct that you cannot jump to a specific page number. With DynamoDB pagination, you can only move forwards or backwards. So what you’ll have to do as a user pages through their items is keep a history of all nextTokens given to you. Maybe store them in an array (tokens.push()) as they page through, and use tokens.pop() to pop off a token when they page backwards.

The other thing to be wary of with AppSync is that (currently, anyway), it’s not always correct when it thinks there are more pages. A scenario where it will falsely give you a nextToken is when (for example) you have 40 items total, and you’re showing 10 per page. On the 4th page, AppSync has no idea if there are more items or not, so you’ll get a nextToken which will yield an empty result set if you query using it. The workaround is to always do a “lookahead” query using nextToken and manually check if that result set is empty. If it is, then don’t allow the user to page forward. It’s a bit hacky, but the team is looking into this.

Hope some of that is helpful!

0reactions
ebisbecommented, May 20, 2020

It’s hard to find some detailed responses like @ffxsam one. So I really appreciate it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Paginating table query results - Amazon DynamoDB
DynamoDB paginates the results from Query operations. With pagination, the Query results are divided into "pages" of data that are 1 MB in...
Read more >
DynamoDB Pagination - The Ultimate Guide (with Example)
Pagination, in general, is a technique to split the data records retrieved from DynamoDB into multiple segments. As a database that supports ...
Read more >
How to implement pagination when using amazon Dynamo ...
What you would do is a scan on dynamodb. For my use case, I just need 1 record at a time. Once I...
Read more >
Implementing Pagination WIth Dynamo DB
Amazon DynamoDB documentation says that DynamoDB paginates the results from scan/query operations. With pagination, the scan results are divided into “pages” of ...
Read more >
DynamoDB Pagination
Amazon DynamoDB documentation says that DynamoDB paginates the results from scan/query operations. With pagination, the scan results are divided into ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found