Paginating DynamoDB Scans and Queries easily using DynamoDB Document Client example.
Trying to find an example on how to use AWS's Dynamo DB paginateScan feature, I stumped acros a lot of small documents and no-example code issues on github, giving me de idea of writing this small article on how-to do it. Let's get to it!
Unfortunately, the pattern chosen by AWS's engineers is not the friendliest one and I found out to be quite unintuitive.
First, instead of using the pagination directly inside each command or have a special configuration to set it up, the library itself @aws-sdk/lib-dynamodb
carries a paginateScan
and a paginatedQuery
object to help us achieve such functionality.
import {
paginateScan,
ScanCommandInput,
UpdateCommand,
} from '@aws-sdk/lib-dynamodb';
Using the paginateScan or paginatedQuery commands will enable the pagination. How to use them? Well, let's look at this piece of code:
This is not a paginated request and it'll return all (20) records requested without a way to paginate it. Now, let's see how this same method will be if paginated:
It's imperative to notice that this won't work if you're using an older version of the @aws-sdk/lib-dynamodb
(It needs to be ^3.172.0) because the issue "Incorrectly marshalls non-object types" fixed recently.
In the case above, by setting up a pageSize and a startingToken, the paginateScan object will handle the pagination easily and make your (the developer's) live easier.