Last active
March 29, 2021 02:20
-
-
Save mrcoles/c1ddc253214f0271e07496625564aef7 to your computer and use it in GitHub Desktop.
Snippet for doing auto-pagination of a DynamoDB scan with an async generator in NodeJS
This file contains 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 { DocumentClient } from "aws-sdk/lib/dynamodb/document_client"; | |
export async function* autoPaginateScan<I extends DocumentClient.AttributeMap>( | |
docClient: DocumentClient, | |
params: DocumentClient.ScanInput | |
) { | |
while (true) { | |
const data = await docClient.scan(params).promise(); | |
if (data.Items && data.Items.length) { | |
const items = data.Items as I[]; | |
yield* items; | |
} | |
if (data.LastEvaluatedKey === undefined) { | |
break; | |
} | |
params = { ...params, ExclusiveStartKey: data.LastEvaluatedKey }; | |
} | |
} |
This file contains 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
type Item = { id: string; name: string; description?: string }; | |
const itemSeq = fullScanSeq<Item>(docClient, { | |
TableName: "items", | |
FilterExpression: "attribute_not_exists(#d)", | |
ExpressionAttributeNames: { "#d": "description" } | |
}); | |
for await (let item of items) { | |
console.log(`item: id=${item.id}, name=${item.name}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These snippets are for: