The best kind of todo is the one we already did and we can delete it from the list 🎉
In this quick lesson we're going to learn how to delete an item from our DynamoDB table with a AWS.DynamoDB.DocumentClient
delete
operation
Instructor: [0:00] Since now we can add a to-the-item, it is time to implement the delete to-the-item method. In the updated code, for this lesson, we can see that we've updated it to the handle.ts. Again, right now, whenever there's a delete request sent to this Lambda function, we are going to call the delete to-the-item method.
[0:16] Let's take a look at the cheat sheet. In the database operations section, we can see that we have a method that is called delete. It allows us to delete a single item in a table by a primary key. In our case, this is the id.
[0:27] We can see that in order to call documentClient delete, we have to pass in the params. Params is an object with a table name and also the key of the item that we want to delete.
[0:37] In the delete to-the-item, let's do const id, we're going to get that from data. Next up, whenever there is an id, and id is not an empty string, we're going to do await dynamo.delete. This is going to take an object and similar to the add to-the-item, the first field is the table name. Let me just copy and paste that over here.
[0:53] The second field is the key. We can see that, in our case, we only need to provide a value for the partition key, which in our case, is the id.
[1:01] At the end, let me do .promise, because we are using await over here. Let me return the id from this function. After this is done, open up the terminal and run cdk deploy in order to deploy the updated version of this Lambda function to AWS.
[1:14] After the deploy, let's go ahead and test our API. I'm back in insomnia. Let me try to delete this to-the-item with a totally random id. First, let's switch to delete, switch the body type to JSON. Let's update the request body to id = totally-random-id. Let me send the request. We can see the confirmation that to-the-item with an id of totally-random-id has been deleted from the database.
[1:37] Once we get all the items, we can see that to-the-item with a totally-random-id is no longer present in our table. As such, our service backend is finished because we are able to get all the items. We can also add a to-the-item, and also delete a to-the-item.
[1:52] All of that was done with CDK, DynamoDB, and Lambda functions, and we didn't have to leave our editor in order to deploy all of them.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!