Created
June 21, 2022 07:28
-
-
Save Dzhuneyt/8eb444287fda95586183cf305247626f to your computer and use it in GitHub Desktop.
(TypeScript) Recursively scan a DynamoDB table until all documents are returned
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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