Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save abhishekhugetech/67296304ac013fb8bba8eb71565cd1e0 to your computer and use it in GitHub Desktop.
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.
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