Skip to content

Instantly share code, notes, and snippets.

Created June 21, 2022 07:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
(TypeScript) Recursively scan a DynamoDB table until all documents are returned
import {DynamoDB} from 'aws-sdk';
import {Key} from 'aws-sdk/clients/dynamodb';
async function dynamodbRecursiveScan(tableName: string, startFromKey?: Key) {
const results: DynamoDB.ItemList = [];
const dynamodb = new DynamoDB();
const items = await dynamodb
TableName: tableName,
ExclusiveStartKey: startFromKey,
if (items.$response.error) {
throw new Error(items.$response.error.message);
results.push(...(items.Items?.length ? items.Items : []));
// If there's another page, recursively fetch it, and keep fetching further pages till there are no more results
if (items.LastEvaluatedKey) {
results.push(...(await dynamodbRecursiveScan(tableName, items.LastEvaluatedKey)));
return results;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment