【问题标题】:AWS AppSync Resolver Mapping Template Dynamic Key for DynamoDB适用于 DynamoDB 的 AWS AppSync 解析器映射模板动态键
【发布时间】:2020-10-19 02:15:36
【问题描述】:

我正在尝试为 DynamoDB 'putItem' 调用创建一个动态密钥。我拥有的当前(非动态)解析器映射模板是

{
  "version" : "2017-02-28",
  "operation" : "PutItem",
  "key" : {
    "userId1" : { "S" : "${context.identity.sub}" },
    "userId2" : { "S" : "${context.stash.userId2}" },
  },
  "attributeValues" : {
   ...
  }
}

我正在尝试使关键部分动态化,我进行过的尝试之一是:

{
  "version" : "2017-02-28",
  "operation" : "PutItem",
  #set($key={})
  #set($keyValue1={})
  #set($keyValue2={})
  $keyValue1.put("S", ${context.identity.sub})
  $keyValue2.put("S", ${context.stash.userId2})
  $util.qr($key.put("userId1", $keyValue1))
  $util.qr($key.put("userId2", $keyValue2))

  "key" : $util.toJson($key),
  "attributeValues" : {
    ...
  }
}

我在这类事情的文档中看不到太多内容。任何指针将不胜感激。

谢谢

【问题讨论】:

    标签: amazon-dynamodb velocity aws-appsync


    【解决方案1】:

    我找到了一个我想分享的解决方案。

        "version" : "2017-02-28",
        "operation" : "PutItem",
    
        #set($key={})
    
        #set($key.userId1= $util.dynamodb.toString("${context.identity.sub}"))
    
        #if(!$ctx.prev.result.items.isEmpty()) ## My condition is based on result from previous template
          #set($key.userId2 = $util.dynamodb.toString("${ctx.prev.result.items[0].userId}"))
        #else
          #set($key.userId2 = $util.dynamodb.toString("${context.stash.anotherValue}"))
        #end
    
        "key" : $util.toJson($key), ## convert map/object to JSON and set the 'key' for use in the DynamoDB request
    

    希望这对未来的人有所帮助:)

    【讨论】:

      猜你喜欢
      • 2021-12-12
      • 2020-05-29
      • 2019-10-14
      • 1970-01-01
      • 2021-07-18
      • 2019-12-04
      • 2022-01-23
      • 2019-01-06
      • 2019-09-01
      相关资源
      最近更新 更多