Get all Items using the Query API through AWS Amplify ('LastEvaluatedKey')
See original GitHub issue** Which Category is your question related to? ** AWS Amplify GET API ** What AWS Services are you utilizing? ** Amplify, NodeJS ** Provide additional details e.g. code snippets **
How can we get all the items by invoking dynamodb.query?
The documentation states that we need to look for the presence of ‘LastEvaluatedKey’. Just wondering how we could aggregate all the Items in an efficient way?
app.get(path, function (req, res) {
var allItems = [];
var params = {
TableName: tableName,
"IndexName": "status-index",
"KeyConditionExpression": "#attrib_name = :attrib_value",
"ExpressionAttributeNames": { "#attrib_name": "status" },
"ExpressionAttributeValues": { ":attrib_value": req.query.status },
"ScanIndexForward": false
}
dynamodb.query(params, onQuery);
function onQuery(err, data) {
if (err) {
res.json({ error: 'Could not load items: ' + err });
} else {
// Should I be aggregating all the items like this?
allItems = allItems.concat(data.Items);
// Then should I set it to res like this to return all the items?
res.json(allItems);
if (typeof data.LastEvaluatedKey != 'undefined') {
params.ExclusiveStartKey = data.LastEvaluatedKey;
dynamodb.query(params, onQuery);
}
}
}
});
Please look at comments within the code. That is where I think we need to have the appropriate code to aggregate all the items and return back the response.
I have written NodeJS code to insert items good enough to see that LastEvaluatedKey is returned in the response. However, I have not found a way to debug this over the DynamodDB on AWS online yet as I’m fairly new to DynamoDB and AWS Amplify. Let me as well know if there is an easier way to debug this in an AWS amplify backed up GET API. Not sure how to point the amplify app to local DynamoDB to test locally with it. How do you folks debug in general?
Please ignore the above paragraph if it is digressing from the main issue. Just need some immediate help with the query part though!
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (1 by maintainers)
Top GitHub Comments
@sammartinez Yes I’m using REST APIs created through Amplify. GraphQL wasn’t there when I started with Amplify.
Thank you for your suggestions on aggregating the dynamodb items, I’ll go with option 1.
I’m surprised when you say NodeJS is not officially supported for Amplify. The Amplify CLI for RESTful APIs creates the NodeJS code, isn’t it? Where can I find the supportability matrix for Amplify JS?
This issue has been automatically locked since there hasn’t been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server
*-help
channels or Discussions for those types of questions.