AWS DynamoDB: List All Items in a Table Using NodeJS

9/17/2021 10:54:45 PM

// package.json dependencies: "aws-sdk": "2.893.0"

let AWS = require("aws-sdk");
let db_client = new AWS.DynamoDB.DocumentClient({ region: "us-east-1" });


async function dynamodb__list_all_items(table_name)
{    
    let items = [];
    let last_evaluated_key = null;

    do
    {
        let list_params = {
            TableName: table_name,
            ExclusiveStartKey: last_evaluated_key
            //Limit: 1000,
        };


        let scan_result = await db_client.scan(list_params).promise();
        // console.log(scan_result);        
        // console.log("scan_result.Count", scan_result.Count);        
        // console.log("scan_result.LastEvaluatedKey", scan_result.LastEvaluatedKey);

        if (scan_result.Count > 0)
        {
            items = [...items, ...scan_result.Items];
        }        

        last_evaluated_key = scan_result.LastEvaluatedKey;        
    } while (last_evaluated_key)


    //console.log("db items length: " + items.length);
    return items;
}