Understanding And Using Condition Expressions In DynamoDB
Article cover image

Understanding And Using Condition Expressions In DynamoDB

Welcome to the 24th edition of Excelling With DynamoDB!

In this week’s issue, we'll discuss what condition expressions are, what they're useful for, and how to use them to solve different problems.


Condition Expressions solve a host of problems when writing or modifying data in DynamoDB.

They are powerful tools that help enforce logic and data integrity by allowing us to specify conditions to modify data in our database.

Problems Solved by Condition Expressions

The following are all problems that are easily solved with condition expressions:

  • avoid overwriting an existing item
  • prevent number counters from going below 0
  • verifying a user has permission to edit or delete data
  • update a value on if it has a certain value
  • Allow writes if a user passes certain criteria
  • Ensure an attribute is not null

...and many more use cases.

API calls that support Condition Expressions

Condition Expressions are supported on the following DynamoDB API calls:

  1. PutItem
  2. UpdateItem
  3. DeleteItem
  4. BatchWriteItem
  5. TransactionWriteItem

Without Condition Expressions, you would need to add additional, more complex code to do the same thing, incurring extra costs.

Additionally, you would also need to consider race conditions if another request tried to modify the item at the same time. This can be complex to implement, but DynamoDB provides a dead-simple way to achieve this.

Condition Expressions operators

So how do condition expressions actually work?

You can use any of the following operators from the list below in a condition expression.

Each of these help with a different type of condition check:

  • attribute_exists() - checks if an attribute exists.
  • attribute_not_exists() - checks if an attribute does not exist.
  • begins_with() - checks if an attribute's value begins with a given substring.
  • contains() - checks if a string contains a given substring
  • size() - checks the length of a string or number, for lists, sets, and maps it checks for the number of elements in the data structure
  • attribute_type() - checks if an attribute is of a particular data type.

This is in addition to plain comparison operators like greater than (and greater or equal to), less than (and less than or equal to), equal to and between (between 2 numbers).

In the first example below, we'll learn how to use a condition expression method to check for the existence of an item in our database.

In the second example below, we'll see how we can use a simple greater than operator in our condition expressions to keep a number counter above 0.

Demo Example

Let's look at an example of using condition expressions methods.

Here, we'll look at two of the most common use cases for condition expressions (which I use regularly):

  1. Preventing data overwrites
  2. Preventing a number count from going below 0.

Preventing item overwrites

When using the PutItem API call, the item is inserted into your table, overwriting any other existing item with the same primary key.

Most of the time, you do not want to do this. Potentially, you want to check if an item with a matching primary key exists before writing it to your database.

We can achieve this by using the attribute_not_exists() method as seen above.

const params = {
    TableName: "users",
    Item: {
      username: { S: "urielbitton" }, 
      email: { S: "urielas1@gmail.com" },
      name: { S: "Uriel Bitton" }
    },
    ConditionExpression: "attribute_not_exists(username),
  };        

In the example above we want to write a user with their username to our database.

In the ConditionExpression, we check if the username already exists.

Only if it doesn't exist do we write it to our users table.

Preventing negative counts

In this example, we can use the condition expressions to prevent a "likes" counter for a blog post from falling below 0.

const params = {
    TableName: "blog",
    Key: {
      postID: { S: "post#101" }, 
      title: { S: "How To Use Condition Expressions In DynamoDB" },
    },
    UpdateExpression: "SET likesCount = likesCount - 1",
    ConditionExpression: "likesCount > 0",
  };        

In the code above we decrement the likes counter by 1 when someone unlikes a post.

But we do so only in the case the likesCount value is greater than 0 to avoid it going into a negative count.

Conclusion

There are many more use cases we haven't explored that are possible to solve with condition expressions, as we have seen above.

Using condition expressions not only reinforces data integrity, it also acts as an efficient tool for verifying data without adding expensive queries and latency.

I encourage you to use condition expressions wherever necessary in order to enhance your database and the consistency of its data.

References

  1. The DynamoDB Book. Alex DeBrie. https://www.dynamodbbook.com/


?? My name is Uriel Bitton and I hope you learned something in this edition of Excelling With DynamoDB.

?? You can share the article with your network to help others learn as well.

?? If you enjoyed this you can subscribe to my newsletter on Medium to get my latest articles on DynamoDB by email.

?? my blog website is coming soon, so stay tuned for that.

?? I hope to see you in the next week's edition!

要查看或添加评论,请登录

Uriel Bitton的更多文章

社区洞察

其他会员也浏览了