In this video, we are going to update product method for product lambda microservices with using SDK
libraries.
For that purpose we expect as a HTTP put request from client and Amazon API gateway to invoke our product
Microsoft Lambda function.
When we hit the HTTP port operation, we perform update operation from the product table of Dynamo DB
table into our lambda function.
So to do so, please open the studio code and go on top of the index JS file and we will add an additional
switch statement, case statement for the HTTP port operation.
But before that, let's check the incoming event.
If you go to events and open the put by ID, the JSON, this is the structure of how lambda function
triggered when performing the HTTP input operation aces method is a put.
This is the resource and PID parameters.
If you scroll on, you can see pet parameters in here and also you can see that this could be get the
body information that you can update an existing attribute.
So let's implement this function before we start.
We should accommodate the incoming HTTP post operation.
So that's why after the case delete operation, I would like to add a naive case which will be the.
Days.
Operation.
Here.
You can see that we have added the HTTP put operation with the body of the update product method and
we are placing the whole event object into this method in order to perform update operation.
So please add this case statement into your switch below of the handle method of lambda function.
And this is the indexes we are in the main file.
Save this file and now it is time to implement these update product function.
Before we implement, we should understand which command that we will use during the update operation
for that purpose.
Open the dynamic DB Client SDK documentation and open the update item command update item command that
we will use when editing and updating an item on Dynamo DB table.
And here you can see the example usage and here is the input parameters that we define these properties
when updating an item OC Let's implement this code to do so, please open the index JS file and go go
at the end of this page.
So now I'm going to copy and paste and explain after that.
So basically update product is a bit complicated as you can see that it is a bit complicated in function
that we implement this function.
So basically if you come to documentation, you can see that as you remember that when we are performing
update item with using the click commands, we are placing the table name, key update, expression
and explicit attribute values and the return values.
So these are the properties that we should define when we are using the update item command.
So that takes some complicated code that we are going to provide into the our update product function.
But the idea is the same.
Before explaining this code below, let me add the required import statement.
If you scroll down, we basically using to Marshall and we use update item command.
Please import this update item command on top of the page after the delete command of the dynamic DB
client import statement.
And if you go back to our code, make sure that you have update item command imported successfully.
So let me start explaining this method.
Basically, we will start with the method body and this method is get a parameter which is the event
JSON object and we are covering the case below.
And in the first line we basically get event information, pass this information as a request request
body to get the updated attributes.
After that, we get the keys into these request body to get which which columns, which attribute values
should be update and after that perform log operation.
And after that, basically we provide the parameters for the update item command and the paste the parameters
to the Dynamo DB client send operation.
So a parameter so we can define these parameters.
Basically, we will follow the same discipline into the documentation and the commands we provide the
table name, key update, expression and expression, attribute values.
So if you go back to our code, we can see the table name comes from the Dynamo DB table environment
variables and see the key is an ID and we get the ID from event parameters ID and marshall this operation
to simplify to dynamo DB key definition.
And after that see that we have an update expression.
We generate the automatic set statement with looking the incoming object keys of the JSON object.
So that means in that stage I would like to show you expected HTTP put request but the payload so think
about that we are using the HTTP operation and sent this URL.
These are the parameters that we define in the key.
And after that we send the payload, right.
So these object keys is collected name and price operation and create a set keyword.
And these values are setting in the explicit attribute values to get value information from this JSON
payload object.
So if you scroll down, the whole operation is the same.
So the idea is that we will use the update item command and follow the same discipline and send this
payload and to give these parameter variables to following the same element of the Dynamo DB API.
So as you can see that we have successfully finished the update product operation and make sure that
your code is identical with this code and we will see other methods in the upcoming video.