Paginating DynamoDB Scans and Queries easily using DynamoDB Document Client example.

Eduardo Elias Saleh
1 min readSep 19, 2022

--

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:

Not paginated query for 20 items of a specific dynamodb table

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:

A paginated request, handling pageSize and startingToken information and passing them ahead.

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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Eduardo Elias Saleh
Eduardo Elias Saleh

Written by Eduardo Elias Saleh

Brazilian, 80’s kid, Lily’s father. In love with JS, PHP, C# and Baby Yoda. Dev since 97'. Board gamer always up for an Eclipse match. We created and killed God

No responses yet

Write a response