DEV Community

Abhishek Gupta for AWS

Posted on • Originally published at community.aws

2 1

[20 Days of DynamoDB] Day 18 - Using a SQL-compatible query language

Posted: 6/Feb/2024

DynamoDB supports PartiQL to execute SQL-like select, insert, update, and delete operations.

Here is an example of how you would use PartiQL based queries for a simple URL shortener application. Notice how it uses a (generic) ExecuteStatement API to execute INSERT, SELECT, UPDATE and DELETE:

_, err := client.ExecuteStatement(context.Background(), &dynamodb.ExecuteStatementInput{
        Statement: aws.String("INSERT INTO url_metadata value {'longurl':?,'shortcode':?, 'active': true}"),
        Parameters: []types.AttributeValue{
            &types.AttributeValueMemberS{Value: "https://github.com/abhirockzz"},
            &types.AttributeValueMemberS{Value: uuid.New().String()[:8]},
        },
    })

    _, err := client.ExecuteStatement(context.Background(), &dynamodb.ExecuteStatementInput{
        Statement: aws.String("SELECT * FROM url_metadata where shortcode=? AND active=true"),
        Parameters: []types.AttributeValue{
            &types.AttributeValueMemberS{Value: "abcd1234"},
        },
    })

    _, err := client.ExecuteStatement(context.Background(), &dynamodb.ExecuteStatementInput{
        Statement: aws.String("UPDATE url_metadata SET active=? where shortcode=?"),
        Parameters: []types.AttributeValue{
            &types.AttributeValueMemberBOOL{Value: false},
            &types.AttributeValueMemberS{Value: "abcd1234"},
        },
    })

    _, err := client.ExecuteStatement(context.Background(), &dynamodb.ExecuteStatementInput{
        Statement: aws.String("DELETE FROM url_metadata where shortcode=?"),
        Parameters: []types.AttributeValue{
            &types.AttributeValueMemberS{Value: "abcd1234"},
        },
    })
Enter fullscreen mode Exit fullscreen mode

Recommended reading:

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay