DynamoDB: get ALL the records using node

Elvis Ciotti
Oct 11, 2023

--

With the SDK v3

npm install @aws-sdk/client-dynamodb

const { DynamoDBClient, ScanCommand } = require("@aws-sdk/client-dynamodb");

async function getAllRecords() {
const client = new DynamoDBClient({ region: 'your-region' }); // Replace 'your-region' with your AWS region.
const tableName = 'YourTableName'; // Replace with your table name.
const allItems = [];

let exclusiveStartKey = undefined;

do {
const scanParams = {
TableName: tableName,
ExclusiveStartKey: exclusiveStartKey,
};

const scanCommand = new ScanCommand(scanParams);

try {
const response = await client.send(scanCommand);
allItems.push(...response.Items);
exclusiveStartKey = response.LastEvaluatedKey;
} catch (error) {
console.error("Error scanning DynamoDB table:", error);
break;
}
} while (exclusiveStartKey);

return allItems;
}

(async () => {
const allRecords = await getAllRecords();
console.log("All records:", allRecords);
})();

tested in a lambda function

--

--

Elvis Ciotti
Elvis Ciotti

Written by Elvis Ciotti

Software Contractor — Java, Spring, k8s, AWS, Javascript @ London - hire me at https://elvisciotti.github.io/

No responses yet