DEV Community

Abhishek Gupta for AWS

Posted on • Originally published at community.aws

2 1

[20 Days of DynamoDB] Day 12 - Using the DynamoDB expression package to build Projection expressions

Posted: 23/Jan/2024

The expression package in the AWS Go SDK for DynamoDB provides a fluent builder API with types and functions to create expression strings programmatically along with corresponding expression attribute names and values.

Here is an example of how you would build a Projection Expression and use it with the GetItem API:

    projectionBuilder := expression.NamesList(expression.Name("first_name"), expression.Name("last_name"))
    projectionExpression, _ := expression.NewBuilder().WithProjection(projectionBuilder).Build()

    _, err := client.GetItem(context.Background(), &dynamodb.GetItemInput{
        TableName: aws.String("customer"),
        Key: map[string]types.AttributeValue{
            "email": &types.AttributeValueMemberS{Value: "c1@foo.com"},
        },
        ProjectionExpression:     projectionExpression.Projection(),
        ExpressionAttributeNames: projectionExpression.Names(),
    })
Enter fullscreen mode Exit fullscreen mode

Recommended reading - expression package API docs

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

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