How to fetch/scan all items from AWS dynamodb
using node.js
. I am posting my code here.
var docClient = new aws.DynamoDB.DocumentCl
This is working for me:
export const scanTable = async (tableName) => {
const params = {
TableName: tableName,
};
let scanResults = [];
let items;
do{
items = await documentClient.scan(params).promise();
items.Items.forEach((item) => scanResults.push(item));
params.ExclusiveStartKey = items.LastEvaluatedKey;
}while(typeof items.LastEvaluatedKey != "undefined");
return scanResults;
};
You can use the ScanPaginator from @aws/dynamodb-query-iterator:
import { ScanPaginator } from '@aws/dynamodb-query-iterator';
import DynamoDB = require('aws-sdk/clients/dynamodb');
const paginator = new ScanPaginator(
new DynamoDB.DocumentClient(),
{
TableName: "users",
FilterExpression: "#user_status = :user_status_val",
ExpressionAttributeNames: {
"#user_status": "user_status",
},
ExpressionAttributeValues: { ":user_status_val": 'somestatus' }
}
);
for await (const page of paginator) {
// do something with `page`, e.g. myFunction(page.Items)
}
For those who are NOT USING AWS.DynamoDB.DocumentClient, this solution will work. I have split the functionality into multiple modules for easy readability and using async/await.
const AWS = require("aws-sdk");
AWS.config.update({
// update table region here
region: "us-west-2"
});
var dynamodb = new AWS.DynamoDB();
const performAsynScanOperation = (scanParams) => {
return new Promise((resolve, reject) => {
dynamodb.scan(scanParams, function (err, responseData) {
if (err) {
reject(err)
} else {
resolve(responseData)
}
})
})
}
const getAllRecords = async (tableName) => {
let allItems = [];
let LastEvaluatedKeyFlag = true;
let scanParams = { TableName: tableName }
while (LastEvaluatedKeyFlag) {
let responseData = await performAsynScanOperation(scanParams)
let batchItems = responseData.Items;
allItems = allItems.concat(batchItems);
if (responseData.LastEvaluatedKey) {
LastEvaluatedKeyFlag = true;
console.log('LastEvaluatedKey', responseData.LastEvaluatedKey)
scanParams.ExclusiveStartKey = responseData.LastEvaluatedKey
} else {
LastEvaluatedKeyFlag = false;
}
}
return allItems;
}
getAllRecords('<Name of table>').then((allItems)=>{
console.log(allItems)
})
If you would like to get the data from DynamoDB without using Hash key value, you need to use Scan API
.
Note: The Scan API reads all the items in the table to get the results. So, it is a costly operation in DynamoDB.
Alternate Approach : Use GSI
Scan Code for the above sceanario:-
var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: "users",
FilterExpression: "#user_status = :user_status_val",
ExpressionAttributeNames: {
"#user_status": "user_status",
},
ExpressionAttributeValues: { ":user_status_val": 'somestatus' }
};
docClient.scan(params, onScan);
var count = 0;
function onScan(err, data) {
if (err) {
console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Scan succeeded.");
data.Items.forEach(function(itemdata) {
console.log("Item :", ++count,JSON.stringify(itemdata));
});
// continue scanning if we have more items
if (typeof data.LastEvaluatedKey != "undefined") {
console.log("Scanning for more...");
params.ExclusiveStartKey = data.LastEvaluatedKey;
docClient.scan(params, onScan);
}
}
}
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({
// optional tuning - 50% faster(cold) / 20% faster(hot)
apiVersion: '2012-08-10',
sslEnabled: false,
paramValidation: false,
convertResponseTypes: false,
region: 'us-east-2' // put your region
});
const tableName = 'tableName'; // put your tablename
exports.handler = async (event, context, callback) => {
let params = { TableName: tableName };
let scanResults = [];
let items;
do {
items = await docClient.scan(params).promise();
items.Items.forEach((item) => scanResults.push(item));
params.ExclusiveStartKey = items.LastEvaluatedKey;
} while (typeof items.LastEvaluatedKey != "undefined");
callback(null, scanResults);
};
This is a drop-in replacement to scan all records:
const scanAll = async (params) => {
let all = [];
while (true) {
let data = await new Promise((resolve, reject) => {
db.scan(params, function (err, data) {
if (err)
reject(err);
else
resolve(data);
});
});
all = all.concat(data.Items);
if (data.LastEvaluatedKey)
params.ExclusiveStartKey = data.LastEvaluatedKey;
else
break;
}
return all;
};
Usage:
scanAll(query)
.catch((err) => {
})
.then((records) => {
});
}