How to make a query to DynamoDB from a React App using AWS Amplify

Viewed 18

I have created a simple react app with AWS Amplify and I have added Atuhentication and a REST API to add CRUD functionality with DynamoDB.

The code for the Lambda Function is the template generated automatically from AWS Amplify: 'CRUD function for DynamoDB (Integration with API Gateway)'

From the Lambda Function, the autogenerated code is the following one:

app.get(path + hashKeyPath, function(req, res) {
  const condition = {}
  condition[partitionKeyName] = {
    ComparisonOperator: 'EQ'
  }

  if (userIdPresent && req.apiGateway) {
    condition[partitionKeyName]['AttributeValueList'] = [req.apiGateway.event.requestContext.identity.cognitoIdentityId || UNAUTH ];
  } else {
    try {
      condition[partitionKeyName]['AttributeValueList'] = [ convertUrlType(req.params[partitionKeyName], partitionKeyType) ];
    } catch(err) {
      res.statusCode = 500;
      res.json({error: 'Wrong column type ' + err});
    }
  }

  let queryParams = {
    TableName: tableName,
    KeyConditions: condition
  }

  dynamodb.query(queryParams, (err, data) => {
    if (err) {
      res.statusCode = 500;
      res.json({error: 'Could not load items: ' + err});
    } else {
      res.json(data.Items);
    }
  });
});

If I substitute dynamodb.query for dynamodb.scan I can scan the DynamoDB from the React App using:

API.get('apiname','/pets/ID') // /pets is the API path and 'ID' is the Partition Key

Does anyone know which is the proper way to call API.get() if I want to make a query?

0 Answers
Related