Created
April 26, 2017 17:15
-
-
Save ryanhanwu/1ed9cf4b25661ed9ef74d2fe2cab98dd to your computer and use it in GitHub Desktop.
A short snippet for scanning AWS DynamoDB table with AWS SDK and Promise
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
var params = { | |
TableName: 'MYTABLE', | |
FilterExpression: 'contains (myKey , :query)', | |
ExpressionAttributeValues: { | |
':query': query | |
} | |
} | |
var dynamoScan = new Promise(function(resolve, reject) { | |
var results = [] | |
var onScan = (err, data) => { | |
if (err) { | |
return reject(err) | |
} | |
results = results.concat(data.Items) | |
if (typeof data.LastEvaluatedKey != 'undefined') { | |
params.ExclusiveStartKey = data.LastEvaluatedKey | |
docClient.scan(params, onScan) | |
} else { | |
return resolve(results) | |
} | |
} | |
docClient.scan(params, onScan) | |
}) | |
dynamoScan | |
.then((results) => { | |
res.json(results) | |
}) | |
.catch((err) => { | |
Logger.error(err) | |
return next(err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment