Created
May 31, 2020 16:57
-
-
Save abhishekhugetech/67296304ac013fb8bba8eb71565cd1e0 to your computer and use it in GitHub Desktop.
Uses the query function of DynamoDB DocumentClient class to count all the items from a table. Use index with only keys projected for best performance.
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
const AWS = require("aws-sdk"); | |
const documentClient = new AWS.DynamoDB.DocumentClient(); | |
let itemCount = 0; | |
var params = { | |
TableName: 'table_name', | |
IndexName: 'part-range-index', | |
KeyConditionExpression: '#t = :hkey', | |
ExpressionAttributeNames: { "#t": "type" }, | |
ExpressionAttributeValues: { ':hkey': 'Solution' }, | |
Limit: 1000, | |
Select: 'COUNT' | |
}; | |
const onScan = (err, data) => { | |
if (err) { | |
console.log(err); | |
console.log(`Total items found ${itemCount}`); | |
} else { | |
if (data) { | |
itemCount += data.Count; | |
if ('LastEvaluatedKey' in data) { | |
params.ExclusiveStartKey = data.LastEvaluatedKey; | |
documentClient.query(params, onScan); | |
} else { | |
console.log(`COMPLETE!! Total Iems found ${itemCount}`); | |
} | |
} | |
} | |
} | |
documentClient.query(params, onScan); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment