Is it possible in dynamoDB to conditionally create/update an item and return its current value?
In the below we have three states, one in which the user is not in the table yet, one in which the user is in the table but does not have a CreatedAt field, and one in which the user exists and has a CreatedAt field.
out, err := db.UpdateItem(&dynamodb.UpdateItemInput{
TableName: aws.String("User"),
Key: map[string]*dynamodb.AttributeValue{
"ID": {S: aws.String(u.ID)},
},
ExpressionAttributeNames: map[string]*string{
"#c": aws.String("CreatedAt"),
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":c": {S: aws.String(u.CreatedAt.Format(time.RFC3339))},
}
ConditionExpression: aws.String("attribute_not_exists(#c)"),
UpdateExpression: aws.String("SET #c = :c"),
ReturnValues: aws.String("ALL_NEW"),
})
if isConditionError(err) {
// out.Attributes == nil
// .: out.Attributes["CreatedAt"] == ""
}
When the user is not present, this creates the user as expected.
When the user does not have a CreatedAt field, it adds that field as expected.
When the condition fails (i.e. the user has a value for CreatedAt), I would like to know what the database says in the CreatedAt field for that user. Unfortunately regardless whether I specify ALL_NEW or ALL_OLD for ReturnValues, out.Attributes is always nil.
I would like to avoid calling the database twice to get this information, and since I know dynamo is reading it to evaluate the condition I think this is reasonable. How would I do this?