DEV Community

Abhishek Gupta for AWS

Posted on • Originally published at community.aws

[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

Top comments (0)