serverless-apigateway-service-proxy icon indicating copy to clipboard operation
serverless-apigateway-service-proxy copied to clipboard

DynamoDB: Allow generation of custom values for hash and range key

Open robhughadams opened this issue 2 years ago • 0 comments

I'd like to be able to use the plugin to support more complex request mapping scenarios for DynamoDB.

The shorthand mapping from querystring or path works really well for simple examples, but doesn't for more complex situations. E.g. where the hash and range key in the dynamo table are concatenated values and are exposed as separate parameters on the external API.

Ideally I'd be able to generate this request template:

"RequestTemplates": {
  "application/json": {
    "Fn::Sub": [
      "{\"TableName\": \"${TableName}\",\"Key\":{\"${HashKey}\": {\"${HashAttributeType}\": \"${HashAttributeValue}\"},\"${RangeKey}\": {\"${RangeAttributeType}\": \"${RangeAttributeValue}\"}}}",
      {
        "TableName": "some-table",
        "HashKey": "Foo_Bar",
        "HashAttributeType": "S",
        "HashAttributeValue": "${input.params().path.foo}_${input.params().querystring.bar}",
        "RangeKey": "Baz_Quux",
        "RangeAttributeType": "S",
        "RangeAttributeValue": "${input.params().path.baz}_${input.params().header.quux}"
      }
    ]
  }

using this serverless yaml:

hashKey:
  keyName: id
  attributeValue: \${input.params().path.foo}_\${input.params().querystring.bar}
  attributeType: S
rangeKey:
  keyName: range
  attributeValue: \${input.params().path.baz}_\${input.params().header.quux} # Custom mapping for the attribute. Use '\${}' to escape the '$' character.
  attributeType: S

robhughadams avatar Jun 22 '23 16:06 robhughadams