Posted: 11/Jan/2024
Conditional operations are helpful in cases when you want a DynamoDB write operation (PutItem
, UpdateItem
or DeleteItem
) to be executed based on a certain criteria. To do so, use a condition expression - it must evaluate to true in order for the operation to succeed.
Here is an example that demonstrates a conditional UpdateItem
operation. It uses the attribute_not_exists
function:
resp, err := client.UpdateItem(context.Background(), &dynamodb.UpdateItemInput{
TableName: aws.String(tableName),
Key: map[string]types.AttributeValue{
"email": &types.AttributeValueMemberS{Value: email},
},
UpdateExpression: aws.String("SET first_name = :fn"),
ExpressionAttributeValues: map[string]types.AttributeValue{
":fn": &types.AttributeValueMemberS{
Value: firstName,
},
},
ConditionExpression: aws.String("attribute_not_exists(account_locked)"),
ReturnConsumedCapacity: types.ReturnConsumedCapacityTotal,
})
Recommended reading - ConditionExpressions
Top comments (0)