适用于 DynamoDB 的 AWS AppSync 解析程序映射模板动态密钥



我正在尝试为 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" : {
...
}
}

我在文档中看不到太多此类内容。 任何指示将不胜感激。

谢谢

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

"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

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

最新更新