‫So I've mentioned this before,
‫but let's do a little zoom in
‫on the conditional rights of DynamoDB.
‫So this is for the write operations.
‫So we have PutItem, UpdateItem,
‫DeleteItem, and BatchWriteItem.
‫And the idea is that you can specify a Condition expression
‫that's going to determine which items should be modified.
‫Now you have several conditions possible.
‫We'll have a look at many of these in examples.
‫So we have attributes_exists or attributes_not_exists,
‫attribute_type, so this is to check whether
‫or not an attribute_exists doesn't exist
‫and is of the correct type,
‫contains and begins_with, this is for string comparison.
‫And then we have the IN keyword to check different values.
‫For example, for product category
‫to be in two different categories.
‫And Price we have between :low and :high.
‫But we can also, for example,
‫do price over or price less than specific values.
‫And finally if we want to check for string length,
‫we have the size attributes.
‫Okay, so you have seen Filter Expressions
‫and you've seen Condition Expressions.
‫So Filter Expressions filter the results of a read query.
‫So Filter Expressions are related to read queries,
‫whereas Condition Expressions are only for rights operations
‫and they tell DynamoDB which rights should succeed or not.
‫So let's do an example, for example, an Update Item.
‫So here we update an item on the table product catalog
‫and we say that we want to set the price to be equal
‫to the current price minus discounts
‫only if the price is over to a specific limit.
‫That's our condition expression.
‫Now to fill in what is the discount and what is the limit?
‫We pass in the file://values.json
‫which has the discount to be a number
‫of 150 and the limit to be a number of 500.
‫So that means that if we have this item
‫in our DynamoDB table with a key 4, 5, 6
‫if we apply this Update Item command is going to
‫be transformed to be now having a price of 500.
‫That's because indeed before the price was strictly
‫over the limit of 500 because 650 is greater than 500.
‫But if we apply yet again the same command now
‫it's not going to succeed.
‫The price will never go below 500 because, well,
‫the condition expression would evaluate to being false.
‫So as you can see, condition expressions vary
‫whether or not the item has the value over 500 or not
‫and they will determine whether or not
‫the item will be updated.
‫So if we have Delete Item
‫if we can check for example, the attribute_not_exists.
‫So this only succeeds
‫if the attribute doesn't exist yet when there's no value.
‫For example, we're saying hey, delete an item
‫or delete many items if you have a batch right item
‫we can do delete one item for our product catalog
‫if the attribute price doesn't exist.
‫That makes sense.
‫If we have an item in our catalog but we don't have a price
‫we may not be able to sell it.
‫So we may want to clean up our table and delete it.
‫Alternatively, you have attribute_exists.
‫So it is the opposite of attributes not exist.
‫And here we can check if something exists.
‫For example,
‫if we have ProductReviews.Onestar that exists
‫that means that our product review
‫our product actually has one star and
‫so we can delete it altogether from our DynamoDB table
‫but we don't have it then don't do anything.
‫So you can start seeing the usefulness of it.
‫So there's a good use case
‫for attribute_not_exists and attribute_exists.
‫So if we have attribute_not_exists
‫and then we specify the partition key
‫what's going to happen is that if the item is already
‫in the database, then this condition will be evaluated false
‫and then we don't overwrite the item.
‫But if the item doesn't exist yet
‫then we can write the item.
‫So it's one way to make sure
‫that we never overwrite existing data
‫for this we do attribute_not_exists
‫and then we specify the partition key.
‫And in case we have the partition key and the sort key
‫and we want to make sure to again never override something
‫then you specify attributes_not_exists
‫partition key and attributes_not_exists sort key
‫and you're good to go. For other things,
‫for example, to check on values, you can have a look
‫at whether a product category is in different categories
‫or if the price is between a low and a high price.
‫And again, we can pass
‫in this values.json file we're specifying category 1
‫and category 2 as well
‫as the low of 500 and the high of 600.
‫And so when we apply Delete Item
‫in case the item does belong to this range
‫then we're good to go.
‫But as we can see, for example,
‫even though the products category Sporting Good is compliant
‫with our condition,
‫which is the first part of our condition,
‫the price is 650, which is not between 500 and 600.
‫And so the whole condition is false
‫and the item will not be deleted in this case.
‫You can also have a look at String Comparisons.
‫So you have begins_with and contains.
‫So here for example, we're saying, hey
‫I want to make sure to delete items
‫if it begins with http.://
‫So we won't, don't want to keep any insecure images
‫in our product catalog.
‫So that's just some examples of course.
‫But what I want to remember out of it is that
‫Condition Expressions help you
‫create conditions that will tell that DynamoDB
‫whether or not to apply your right operations.
‫Alright, let's see for this lecture
‫I hope you liked it, and I will see you in the next lecture.