How to prevent creating a new item in UpdateItem if the item does not exist

前端 未结 3 1593
鱼传尺愫
鱼传尺愫 2020-12-29 17:53

I am running an AWS Lambda service written in Node.js that interacts with a DynamoDB database. One of my methods performs an update (AWS.DynamoDB.DocumentClient().update) on

3条回答
  •  礼貌的吻别
    2020-12-29 18:49

    ConditionExpression is the way to go, but you can also use the attribute_exists function :

    ConditionExpression: 'attribute_exists(userId)'
    

    It will only update the line with the correct key and throw a ConditionalCheckFailedException if requested key doesn't exist.

    The goal is more obvious and you can skip the extra userId binding.

    ref : https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html

提交回复
热议问题