How to get / view all items from `AWS dynamodb` using node.js
How to get / view all items from AWS dynamodb
using node.js
. I am posting my code here.
var docClient = new aws.DynamoDB.DocumentClient();
var params = {
TableName:"users",
KeyConditionExpression:"user_status=:status",
ExpressionAttributeValues: {
":status": "Y"
}
};
var queryExecute = function(callback) {
docClient.query(params,function(err,result) {
if(err) {
console.log(err)
callback(err);
} else {
console.log(result);
if(result.LastEvaluatedKey) {
params.ExclusiveStartKey = result.LastEvaluatedKey;
queryExecute(callback);
} else {
callback(err,items);
}
}
});
}
queryExecute(callback);
It gives me below error.
ValidationException: Query condition missed key schema element: `user_id`.
Here the primary key is user_id. I don't want to use it with my query condition because I need to set the value if I mentioned the primary key in KeyConditionExpression
. Maybe I'm wrong. However, please suggest me a good way to get all items from dynamodb
that hasuser_status = "Y"
source to share
If you want to get data from DynamoDB without using the Hash key value, you need to use Scan API
.
Note. ... The scan API reads all elements of the table to get the results. Thus, this is an expensive operation in DynamoDB.
Alternative Approach: Use GSI
Scan code for 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);
}
}
}
source to share
This works 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;
};
source to share
Express node solution that returns data in JSON format:
let datapack=[];
item = {
TableName: ddbTable,
FilterExpression: "aws = :e AND begins_with ( Id, :t )",
ExpressionAttributeValues: {
":t" : "contact",
":e" : aws
},
ProjectionExpression: "Id,FirstName,LastName,cEmail",
};
docClient.scan(item, onScan);
function onScan(err, data) {
if (err) {
console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
} else {
datapack = datapack.concat(data.Items);
});
if (typeof data.LastEvaluatedKey != "undefined") {
item.ExclusiveStartKey = data.LastEvaluatedKey;
docClient.scan(item, onScan);
} else {
res.json(datapack);
}
}
}
source to share
Here is an answer that will give indexed results rather than an expensive scan, also in node / express JSON format. Note the use of docClient.query:
datapack=[];
item = {
TableName: ddbTable,
IndexName: "cEmailIndex",
KeyConditionExpression : "aws = :e AND begins_with ( cEmail, :t )",
ExpressionAttributeValues: {
":t" : search,
":e" : aws
},
ProjectionExpression: "Id,FirstName,LastName,cEmail",
};
docClient.query(item, onScan);
function onScan(err, data) {
if (err) {
console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
} else {
datapack = datapack.concat(data.Items);
if (typeof data.LastEvaluatedKey != "undefined") {
item.ExclusiveStartKey = data.LastEvaluatedKey;
docClient.query(item, onScan);
} else {
// console.log(JSON.stringify(datapack));
res.json(datapack);
}
}
}
source to share