DEV Community

Abhishek Gupta for AWS

Posted on • Originally published at community.aws

2 1

[20 Days of DynamoDB] Day 15 - Using the DynamoDB expression package to build Update expressions

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,
    })
Enter fullscreen mode Exit fullscreen mode

Recommended reading - WithUpdate method in the package API docs

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

Imagine monitoring that's actually built for developers

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay