aws-appsync-community icon indicating copy to clipboard operation
aws-appsync-community copied to clipboard

How to create a map if not exists or update it if it does in appsync

Open deepanwita22 opened this issue 3 years ago • 1 comments

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?

deepanwita22 avatar Sep 25 '21 17:09 deepanwita22

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"}),
            }
        }
}

onlybakam avatar Feb 08 '22 00:02 onlybakam

That would only create the map if userStatus.#idx doesn't exist, but it doesn't update the attribute if the map does exist

CharlesPerrotMinotHCHB avatar Dec 09 '22 02:12 CharlesPerrotMinotHCHB