Skip to content

Instantly share code, notes, and snippets.

@Dzhuneyt
Created June 21, 2022 07:28
Show Gist options
  • Save Dzhuneyt/8eb444287fda95586183cf305247626f to your computer and use it in GitHub Desktop.
Save Dzhuneyt/8eb444287fda95586183cf305247626f to your computer and use it in GitHub Desktop.
(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
.scan({
TableName: tableName,
ExclusiveStartKey: startFromKey,
})
.promise();
if (items.$response.error) {
console.error(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