DEV Community

Abhishek Gupta for AWS

Posted on • Originally published at community.aws

2 1

[20 Days of DynamoDB] Day 14 - Using the DynamoDB expression package to build Key Condition and Filter expressions

Posted: 30/Jan/2024

You can use expression package in the AWS Go SDK for DynamoDB to programmatically build key condition and filter expressions and use them with Query API.

Here is an example that queries for a specific thread based on the forum name (partition key) and subject (sort key):

    keyConditionBuilder := expression.Key("ForumName").Equal(expression.Value("Amazon DynamoDB"))
    filterExpressionBuilder := expression.Name("Views").GreaterThanEqual(expression.Value(3))

    expr, _ := expression.NewBuilder().
        WithKeyCondition(keyConditionBuilder).
        WithFilter(filterExpressionBuilder).
        Build()

    _, err := client.Query(context.Background(), &dynamodb.QueryInput{
        TableName:                 aws.String("Thread"),
        KeyConditionExpression:    expr.KeyCondition(),
        FilterExpression:          expr.Filter(),
        ExpressionAttributeNames:  expr.Names(),
        ExpressionAttributeValues: expr.Values(),
    })
Enter fullscreen mode Exit fullscreen mode

Recommended reading - Key and NameBuilder in the package API docs

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

👋 Kindness is contagious

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

Okay