Skip to content

Instantly share code, notes, and snippets.

@adamfortuno
Created July 1, 2019 16:57
Show Gist options
  • Save adamfortuno/9649307f2ff23f364bb50cfc6d3435f4 to your computer and use it in GitHub Desktop.
Save adamfortuno/9649307f2ff23f364bb50cfc6d3435f4 to your computer and use it in GitHub Desktop.
Paginate the results of a larger DynamoDB query or scan. This script scans the Failures table in Oregon returning some 3xx odd records as well as a record count and LastEvaluatedKey property. The LastEvaluateKey property holds the failureKey for the last item returned. The script uses that to resume a query query starting with the record followi…
'use strict'
process.env.AWS_SDK_LOAD_CONFIG = true;
process.env.AWS_PROFILE = "development";
const AWS = require('AWS-SDK');
AWS.config.update( { region: "us-west-2"} );
const ddbc = new AWS.DynamoDB.DocumentClient();
let TableName = "Failures";
const p = {
TableName,
PageSize: 5
};
ddbc.scan(p, (error, data) => {
console.log("Starting Scan...");
if (error) {
console.log(error);
} else {
if (typeof data.LastEvaluatedKey != "undefined") {
console.log("Last Item: %s", data.LastEvaluatedKey.failureKey);
console.log("Records Retrieved: %s", data.Count);
scanIt(TableName, p, data.LastEvaluatedKey);
}
}
});
const scanIt = (TableName, Parameters, ExclusiveStartKey) => {
p.ExclusiveStartKey = ExclusiveStartKey;
ddbc.scan(p, (error, data) => {
if (error) {
console.log(error);
} else {
if (typeof data.LastEvaluatedKey != "undefined") {
console.log("Last Item: %s", data.LastEvaluatedKey.failureKey);
console.log("Records Retrieved: %s", data.Count);
scanIt(TableName, Parameters, data.LastEvaluatedKey);
} else {
console.log("Records Retrieved: %s", data.Count);
console.log("Complete...");
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment