18

I've a Dynamo Db table with following

{
  "primary_key": {
    "S": "series"
  },
  "sort_key": {
    "S": "type-of-brokers"
  },
  "title": {
    "S": "Types Of Brokers"
}
,
{
  "primary_key": {
    "S": "series"
  },
  "sort_key": {
    "S": "type-of-brokers-2"
  },
  "title": {
    "S": "Types Of Brokers-2"
}

Following this I've implemented application/json body mapping template:

{
    "TableName": "table_name",
    "Key" :{
        "primary_key" :{
            "S": "series"
        },
        "sort_key" : {
            "S": "type-of-brokers"
        }
    }
}

It returns one item that matched primary and sort key!

Now I want to return all the matched primary_key items i.e. without sort_key

How do I achieve that? with API gateway body mapping template?

Also, can api return common json rather than "S" kind, here it specify attributes , can it be done by not hard coding in integration response

Garvit Jain
  • 1,862
  • 2
  • 19
  • 27
  • can api return common json rather than "S" kind ? like the Text view in console for edit Item with `DynamoDB JSON` checkbox unchecked? – Garvit Jain May 30 '18 at 13:53
  • If you're using the nodejs sdk you can use the [DocumentClient](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html) to abstract away the AttributeValues from the dynamodb response. Other sdks might have similar Clients/abstractions. AFAIK the api itself will always include the attributevalues by design. – Jacob Lange May 30 '18 at 19:15
  • I'm avoiding AWS Lambda The values are fetched by API Gateway using Body Mapping Templates, mapping can be done to response too. but I don't want to hard code for each response. – Garvit Jain May 31 '18 at 06:13

1 Answers1

29

The getitem API requires both hash and sort key. However, you can use query api with only hash only.

Query API

Use the KeyConditionExpression parameter to provide a specific value for the partition key. The Query operation will return all of the items from the table or index with that partition key value. You can optionally narrow the scope of the Query operation by specifying a sort key value and a comparison operator in KeyConditionExpression. To further refine the Query results, you can optionally provide a FilterExpression. A FilterExpression determines which items within the results should be returned to you. All of the other results are discarded.

notionquest
  • 37,595
  • 6
  • 111
  • 105
  • 1
    can api return common json rather than "S" kind ? like the Text view in console for edit Item with `DynamoDB JSON` checkbox unchecked? – Garvit Jain May 30 '18 at 11:18
  • You should get without data type if you use Document client api. https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/dynamodb-example-document-client.html – notionquest Jun 01 '18 at 13:37
  • I'm using Api Gateway and dynamodb as appears here : https://sanderknape.com/2017/10/creating-a-serverless-api-using-aws-api-gateway-and-dynamodb/ without lambda functions – Garvit Jain Jun 07 '18 at 07:36