DEV Community

Abhishek Gupta for AWS

Posted on • Originally published at community.aws

3 1 1 1

[20 Days of DynamoDB] Day 4 - Conditional UpdateItem

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

Recommended reading - ConditionExpressions

Billboard image

Monitoring as code

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

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

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

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

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay