1

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 not found a way to debug this 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.

SuperStar518
  • 2,814
  • 2
  • 20
  • 35
stayingcool
  • 2,324
  • 1
  • 21
  • 24

2 Answers2

0

This is not a direct answer to you question, but a suggestion. I wrote an article "How To Use AWS AppSync in Lambda Functions ".

The TLDR of it is:

  • Create a Lambda function, which uses the AppSync client to perform GraphQL operations. Use polyfills and install all necessary dependencies.
  • Ensure the Lambda function has the right execution policy.
  • Use AppSync’s multi auth to allow both requests that are signed by Amazon Cognito User Pools as well as requests that are signed using Amazon’s IAM. This way, both the client and the server (aka. the Lambda function) will be authenticated and can have different CRUD permissions.

If I were you and wanted to access my data base through a Lambda function, I would follow that tutorial and do it using AppSync. One of the advantages which matters to you is that you don't have to care about LastEvaluatedKey and you can instead use AppSync's nextToken which is way more safe.

J. Hesters
  • 13,117
  • 31
  • 133
  • 249
  • I like the approach, nice and detailed article!. My next project is going to be in GraphQL and AppSync. Can't wait for that! For now, I'm going to stick to what we have and need to find a way to get all the records in an optimized fashion. – stayingcool Jul 01 '19 at 00:44
0

Query returns paginated results - if you want all data then you need to keep querying and aggregating until your LastEvaluatedKey is empty.

Refer: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html

dDarkLORD
  • 624
  • 7
  • 25
  • The sample code already does what you have said. Just reaching out to know the right way to aggregate and send it back to amplify backed up lambda function (res object). – stayingcool Jul 01 '19 at 04:14