DEV Community

Abhishek Gupta for AWS

Posted on • Originally published at community.aws

2

[20 Days of DynamoDB] Day 13 - Using the DynamoDB expression package to build Condition expressions

Posted: 25/Jan/2024

Thanks to the expression package in the AWS Go SDK for DynamoDB, you can programmatically build Condition expressions and use them with write operations. Here is an example with the DeleteItem API:

    conditionExpressionBuilder := expression.Name("inactive_days").GreaterThanEqual(expression.Value(20))
    conditionExpression, _ := expression.NewBuilder().WithCondition(conditionExpressionBuilder).Build()

    _, err := client.DeleteItem(context.Background(), &dynamodb.DeleteItemInput{
        TableName: aws.String(tableName),
        Key: map[string]types.AttributeValue{
            "email": &types.AttributeValueMemberS{Value: email},
        },
        ConditionExpression:       conditionExpression.Condition(),
        ExpressionAttributeNames:  conditionExpression.Names(),
        ExpressionAttributeValues: conditionExpression.Values(),
    })
Enter fullscreen mode Exit fullscreen mode

Recommended reading - WithCondition method in the package API docs

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

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

Okay