Last active
May 13, 2022 11:00
-
-
Save aaronshaf/cff7246a6dbe3ae8e12b9c8155d17362 to your computer and use it in GitHub Desktop.
Use async generators and async iterators with DynamoDB's scan
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
const { docClient } = require('../services/dynamodb') | |
exports.findAllItems = async function* () { | |
let response = {} | |
let ExclusiveStartKey | |
do { | |
response = await docClient.scan({ | |
TableName: 'mytable', | |
Limit: 500, | |
ExclusiveStartKey | |
}).promise() | |
const items = response.Items | |
for (let item of items) { | |
yield item | |
} | |
ExclusiveStartKey = response.LastEvaluatedKey | |
} while (response.LastEvaluatedKey) | |
} |
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
const findAllItems = require('./generator.js') | |
for await (const item of findAllItems()) { | |
console.log(item) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this code is so hip, have a star.