AppSync查询解析器:是否需要expressionNames和expressionValues? [英] AppSync query resolver: are expressionNames and expressionValues necessary?
问题描述
AppSync文档说 expressionNames
和 expressionValues
是可选字段,但始终由代码生成填充。第一个问题,在使用DynamoDB时是否应将它们作为最佳实践?如果是,为什么?
AppSync doc says that expressionNames
and expressionValues
are optional fields, but they are always populated by code generation. First question, should they be included as a best practice when working with DynamoDB? If so, why?
AppSync解析程序对分区键的查询:
{
"version": "2017-02-28",
"operation": "Query",
"query": {
"expression": "#partitionKey = :partitionKey",
"expressionNames": {
"#partitionKey": "partitionKey"
},
"expressionValues": {
":partitionKey": {
"S": "${ctx.args.partitionKey}"
}
}
}
}
第二个问题,表达式$ c的外行翻译到底是什么? $ c>字段在上面的代码中?该语句告诉DynamoDB到底要做什么?
表达式中
,它们是表达式名称 #
的用途是什么: #partitionKey =:partitionKey
Second question, what exactly is the layman translation of the expression
field here in the code above? What exactly is that statement telling DynamoDB to do? What is the use of the #
in "expression": "#partitionKey = :partitionKey"
and are the expression names and values just formatting safeguards?
推荐答案
让我先回答第二个问题:
Let me answer your second question first:
expressionNames
用于插值。这意味着在插值之后,此过滤器表达式对象:
expressionNames
are used for interpolation. What this means is after interpolation, this filter expression object:
"expression": "#partitionKey = :value",
"expressionNames": {
"#partitionKey": "id"
}
将转换为:
"expression": "id = :value",
#partitionKey
充当列名<$ c $的占位符c> id 。 '#'
恰好是分隔符。
the #partitionKey
acts as a placeholder for your column name id
. '#'
happens to be the delimiter.
expressionNames
是必需的,因为某些关键字由DynamoDB保留,这意味着您不能在DynamoDB表达式中使用这些单词。
expressionNames
are necessary because certain keywords are reserved by DynamoDB, meaning you can't use these words inside a DynamoDB expression.
当您需要比较DynamoDB表达式中的任何内容时,还需要使用替代项使用占位符表示实际值,因为DynamoDB输入的值是一个复杂对象。
When you need to compare anything in a DynamoDB expression, you will need also to use a substitute for the actual value using a placeholder, because the DynamoDB typed value is a complex object.
在以下示例中:
"expression": "myKey = :partitionKey",
"expressionValues": {
":partitionKey": {
"S": "123"
}
}
:partitionKey
是复杂值的占位符
{
"S": "123"
}
':'
是不同的分隔符,告诉DynamoDB替换时使用 expressionValues
映射。
':'
is the different delimiter that tells DynamoDB to use the expressionValues
map when replacing.
始终使用 expressionNames的代码生成逻辑
和 expressionValues
,因为对于保留/非保留DynamoDB单词不需要两个代码路径。使用 expressionNames
总是可以防止冲突!
It is just simpler for the code generation logic to always use expressionNames
and expressionValues
because there is no need to have two code paths for reserved/non-reserved DynamoDB words. Using expressionNames
will always prevent collisions!
这篇关于AppSync查询解析器:是否需要expressionNames和expressionValues?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!