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

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry πŸ•’

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more β†’

Top comments (0)

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay