Okay, in the last lecture, we created a lambda function. And we were able to save data into dynamodb. In this lecture, we are going to update. Now, the documentation I was talking about. And we had an initial view about this function, which creates a new item or replaces an old item. So if the ID is same in this case, then it's going to replace your row with this new set of records.
Now what we are going to do get us going to copy this lambda function and we are going to create a funk function which will update data into Dynamo dB. Let's name it update inventory. Node JS runtime environment is 8.2. We are going to choose this time existing role and this is add inventory. What we created a couple of lectures back. Now what we are going to do once we have created this initial update inventor will function we are going to replace with what we had created.
Of course, we don't want to create and write this all code every time that we need to do one thing we need to define this lambda environment variable. And as you remember to access this dynamodb table name inventory. Now once everything has been set up, of course the syntax or to update would be a little bit different here. So what we are going to do here so we need a key. So key is a kind of primary key in other RDBMS languages. In Dynamo DB context, it is hash key.
So it is a what so here's the primary ID in this case is ID primary key. So what we are Going to add is the hash key. And hash key here is ID. And of course, later we'll also get that Id dynamically from our request body. We keep all this dynamic variable, variable, same, let's let's replace it with event ID, and everything is going to be same. Now, what we have to do is what we want to update with, so we have to replace all other variables, update variables, what we wanted to update with, and we just have to replace with update expression a couple of ways to do it.
You can refer a dynamodb documentation with AWS SDK at any point of time what we are going to discuss and you should be knowing this method when you want to update your information and dynamodb table So, what we are going to define is update expression like name is in stock.an in stock. So, hash actually defines what variable name we want to replace with later on. And colon signifies or tells us that what should be the value here. So, what what what we are going to do here is we are going to define all those variables what we want to update with all hashes. And colon as I said, is used to replace with the actual values. Now, a couple of things we have to do, once we have updated with update expressions, we have to replace with expression attribute names and expression attribute values.
Expression attribute names are has names what we have defined there, and values with the actual values come in time. From the request body so what we are going to do quickly is we are going to replace and define those all houses with the actual variable names in dynamodb table. So name the actual value of the hash name is name name, which is in the dynamodb table itself, and for multiple values, and if you are aware how JSON script or JSON data works just separate with comma and define all those dynamodb attributes with actual actual attribute names in dynamodb table. So what we are going to do we are going to replace with all these variable names with actual dynamodb attributes here. And here is the thing we are going to replace rather writing everything and especially attribute values as I said earlier, This will be replaced with the actual Dynamo DB not not not Dynamo DB but request body variable.
So what we are getting from a request body. So, we are going to replace even talk name as we have done it earlier in his talk with even talk is in stock and total in stock as we are going to define here even talk total in stock once we have done everything we have all our parents ready and we are just ready to execute but let's just before this we are going to update with the update method here. So, we can get to know we can apply this update in the dynamodb table here. And once we have saved all this informations here and let's see what has been what we should be defining here in it. We can just copy those all you know the values what we had defined in our earlier lecture. And we can just copy paste everything.
But let's, let's just configure here everything. So what we are saying here, update event, it has to be unique. So you can give any event name you are happy with. And just define all those request bodies like ID and everything ID. Name is in stock, and availability, status. In this in this request body, we can we can just copy this JSON object.
Oops, let's go back. And let's just copy this all JSON object objects here. So we can define our request body We can copy this from a dynamo DB in a text view from we can we can we can get it from let's let's define everything here. So we can we can configure our request body. Yeah. So we can just define everything here.
And we can define all the values what we are after to update with. Of course ID it needs to be there in the database. When by writing API we can first of all validate whether that ID is in dynamodb or not, but for the sake of this lecture, what we are we are not going to worry too much about that Id exists or not. So once we have defined everything in our request model let's let's say updated so we can be sure that what values we are going to update Submit. Now once we have to find everything here, let's save this lambda function. And once we have saved this lambda function, let's execute it.
And once it has executed successfully, we shouldn't have any error. And now if we see this well, magic happened. The records have been updated now. And that's how you actually update any data in dynamodb. So that's for all this lecture, and I'll see you in the next lecture.