aws-appsync-community
aws-appsync-community copied to clipboard
How to create a map if not exists or update it if it does in appsync
I have come across a scenario that incase a map is not created beforehand in dynamodb the update operation fails.
I am trying to create a map(userStatus in my case) incase it doesn't exist and also update it's key value in one pass.
here is the code from my appsync request template
{ "version": "2018-05-29", "operation": "UpdateItem", "key": { "topicId": $util.dynamodb.toDynamoDBJson($ctx.stash.topicId) }, "update" : { "expression" : "SET userStatus = if_not_exists(userStatus, :emptymap), userStatus.#idx = :userContent", "expressionNames": { "#idx": "$ctx.args.input.event.payload.userId" }, "expressionValues": { ":emptymap" : $util.dynamodb.toDynamoDBJson({}), ":userContent" : $util.dynamodb.toDynamoDBJson($userStatusContent) } } }
i'm getting this error "Two document paths overlap with each other; must remove or rewrite one of these paths" Here is a code that I tried but it fails
How can I solve this?
you need to provide a path. try doing this instead:
{
"version": "2018-05-29",
"operation": "UpdateItem",
"key": {
"topicId": $util.dynamodb.toDynamoDBJson($ctx.stash.topicId)
},
"update" : {
"expression" : "SET userStatus = if_not_exists(userStatus.#idx, :mymap)",
"expressionNames": {
"#idx": "$ctx.args.input.event.payload.userId"
},
"expressionValues": {
":mymap" : $util.dynamodb.toDynamoDBJson({"$ctx.args.input.event.payload.userId": "$userStatusContent"}),
}
}
}
That would only create the map if userStatus.#idx
doesn't exist, but it doesn't update the attribute if the map does exist