使用 appsync api 和数据源仅更新给定的列值作为 postgresql
Update only the given column value with appsync api and datasource as posgresql
我已经使用 postgresql 作为数据源创建了 appsync api。现在使用 graphql 可以非常直接地创建、更新和删除任何记录。但我想要类似的东西,每当发生任何更新时,我只能更新该特定列的值。例如
让我们有 3 个字段的用户 table:
user_id
name
city
现在,当我们为用户更新城市时,应该只更新城市:
mutation update{
updateUser(input:{user_id:"xxxxx",city:"xyz"}){
user_id
name
city
}}
用户更新输入:
input updateUserInput{
user_id!
name
city
}
有没有办法在 vtx 模板中执行此操作,或者我们必须使用 lambda 作为数据源然后执行此操作。
我想出了如何实现它:
你可以这样做:
#set ($updates = "")
#foreach( $item in $ctx.args.input.keySet())
#if($item)
#set ($updates = "$updates, $item = '$ctx.args.input.get($item)'")
#end
#end
--------used cognito as authentication , so user id will come in indentity
#set($user_id = ($ctx.identity.username))
#set($updates = "$updates, updated_at = '$util.time.nowFormatted('yyyy-MM-dd HH:mm:ssZ')'")
{
"version": "2018-05-29",
"statements": [
$util.toJson("update public.tableName SET $updates.substring(1) WHERE user_id = '$user_id'"),
$util.toJson("select * from public.tableName WHERE user_id = '$user_id'")
]
}
希望对大家有所帮助。
我已经使用 postgresql 作为数据源创建了 appsync api。现在使用 graphql 可以非常直接地创建、更新和删除任何记录。但我想要类似的东西,每当发生任何更新时,我只能更新该特定列的值。例如 让我们有 3 个字段的用户 table:
user_id
name
city
现在,当我们为用户更新城市时,应该只更新城市:
mutation update{
updateUser(input:{user_id:"xxxxx",city:"xyz"}){
user_id
name
city
}}
用户更新输入:
input updateUserInput{
user_id!
name
city
}
有没有办法在 vtx 模板中执行此操作,或者我们必须使用 lambda 作为数据源然后执行此操作。
我想出了如何实现它:
你可以这样做:
#set ($updates = "")
#foreach( $item in $ctx.args.input.keySet())
#if($item)
#set ($updates = "$updates, $item = '$ctx.args.input.get($item)'")
#end
#end
--------used cognito as authentication , so user id will come in indentity
#set($user_id = ($ctx.identity.username))
#set($updates = "$updates, updated_at = '$util.time.nowFormatted('yyyy-MM-dd HH:mm:ssZ')'")
{
"version": "2018-05-29",
"statements": [
$util.toJson("update public.tableName SET $updates.substring(1) WHERE user_id = '$user_id'"),
$util.toJson("select * from public.tableName WHERE user_id = '$user_id'")
]
}
希望对大家有所帮助。