How to fetch/scan all items from AWS dynamodb
using node.js
. I am posting my code here.
var docClient = new aws.DynamoDB.DocumentCl
Using Promises and async
const aws = require('aws-sdk');
aws.config.update({ region: 'us-east-1' });
const documentClient = new aws.DynamoDB.DocumentClient();
const scanAll = async (params) => {
let lastEvaluatedKey = 'dummy'; // string must not be empty
const itemsAll = [];
while (lastEvaluatedKey) {
const data = await documentClient.scan(params).promise();
itemsAll.push(...data.Items);
lastEvaluatedKey = data.LastEvaluatedKey;
if (lastEvaluatedKey) {
params.ExclusiveStartKey = lastEvaluatedKey;
}
}
return itemsAll;
}
Use like this
const itemsAll = scanAll(params);
The code is the same for query (just replace scan with query)