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

[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(),
    })

Recommended reading – Key and NameBuilder in the package API docs

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
navigating-the-graphql-galaxy:-a-comprehensive-roadmap-for-developers-

🚀Navigating the GraphQL Galaxy🌌: A Comprehensive Roadmap for Developers 🚀

Next Post
angular-17-encrypting-decrypting-data-with-cryptojs-|-angular-17-tutorial-|-react

Angular 17 Encrypting Decrypting Data with CryptoJs | Angular 17 Tutorial | React

Related Posts