Posted: 31/Jan/2024
The DynamoDB Go SDK expression package supports programmatic creation of Update expressions. Here is an example of how you can build an expression to include execute a SET
operation of the UpdateItem
API and combine it with a Condition
expression (update criteria):
updateExpressionBuilder := expression.Set(expression.Name("category"), expression.Value("standard"))
conditionExpressionBuilder := expression.AttributeNotExists(expression.Name("account_locked"))
expr, _ := expression.NewBuilder().
WithUpdate(updateExpressionBuilder).
WithCondition(conditionExpressionBuilder).
Build()
resp, err := client.UpdateItem(context.Background(), &dynamodb.UpdateItemInput{
TableName: aws.String(tableName),
Key: map[string]types.AttributeValue{
"email": &types.AttributeValueMemberS{Value: "c1@foo.com"},
},
UpdateExpression: expr.Update(),
ConditionExpression: expr.Condition(),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
ReturnValues: types.ReturnValueAllOld,
})
Recommended reading - WithUpdate method in the package API docs
Top comments (0)