如何使用嵌套数据配置UpdateExpression

时间:2016-09-15 21:18:19

标签: amazon-web-services aws-api-gateway

我有一个AWS API网关,可以在DynamoDB实例中存储数据。我的表结构如下所示:

{
  "TableName": "stuff",
  "Item": {
    "stuffId": {
      "S": "02b4e004-1132-4b87-a855-20e7d1bd1840"
    },
    "clients": {
      "M": {
        "company_inc": {
          "M": {
            "prod": {
              "S": "null"
            },
            "qa": {
              "S": "null"
            },
            "stage": {
              "S": "null"
            }
          }
        }
      }
    }
  }
}

我试图弄清楚如何配置我的身体映射模板,以便在给定HTTP PATCH请求时我可以更新company_inc.prod。例如;给定此查询字符串:

?stuffId=02b4e004-1132-4b87-a855-20e7d1bd1840&client=company_inc&location=prod&locationIsSet=true

我会将记录更新为:

{
  "TableName": "stuff",
  "Item": {
    "stuffId": {
      "S": "02b4e004-1132-4b87-a855-20e7d1bd1840"
    },
    "clients": {
      "M": {
        "company_inc": {
          "M": {
            "prod": {
              "S": "true"
            },
            "qa": {
              "S": "null"
            },
            "stage": {
              "S": "null"
            }
          }
        }
      }
    }
  }
}

" UpdateExpression"看起来想实现那个目标?

1 个答案:

答案 0 :(得分:1)

我想我找到了答案。您必须使用#client#location等属性名称作为company_inc.prod路径中的占位符。

{
    "TableName": "stuff",
    "Key": {
        "alterId": {
            "S": "$input.params('stuffId')"
        }
    },
    "UpdateExpression": "set clients.#attrClientName.#attrLocation = :locationIsSet",
    "ExpressionAttributeNames" : {
        "#attrClientName" : "$input.params('client')",
        "#attrLocation" : "$input.params('location')"
    },
    "ExpressionAttributeValues": {
        ":locationIsSet": {"S": "$input.params('location')"}
    }
}