What is CRUD?
Within computer programming, the acronym CRUD stands for Create, Read, Update, Delete. These are the ways one can operate on stored data. These are the four essential functions of persistent storage. Let's dive deeper into it and explore each operation in a painless way.
When building APIs, we want our models to provide four basic types of functionality. The model must be able to Create, Read, Update, and Delete resources. The CRUD paradigm is standard in constructing web applications because it provides a memorable framework for reminding developers of how to construct full, usable models. For example, we will use my latest project - social media(Twitter clone) for reference. All the GIFs provided below will be from my project.
In this social media database, we can imagine that there would be a post resource, which would store post objects. Let’s say that the post object looks like this:
post: {
id: <UniqueId>,
username: <String>,
content: <String>,
image: <File>
}
Each letter in the CRUD acronym has a corresponding HTTP request method.
CRUD OPERATION | HTTP REQUEST METHOD | |
Create | - | POST |
Read | - | GET |
Update | - | PUT or PATCH |
Delete | - | DELETE |
Note: You can make a CRUD app with any of the programming languages out there. And the app doesn’t have to be full stack – you can make a CRUD app with client-side JavaScript.
Create(POST)
The CREATE operation adds a new record to a database. As we are using the social media post example, here we will use the POST method to save a post object in a database.
In a SQL database, to create is to INSERT. In a NoSQL database like MongoDB, you create with the insert() method.
The GIF below shows how the CREATE operation works
Read(GET)
Read returns records from a database table based on some search criteria. The read operation can return all records or some. In simple terms, make it visible in the UI. In our example showing the user's post in the UI.
In a SQL database, to read is to SELECT an entry. In a NoSQL database like MongoDB, you read with the find() or findById() method.
The image below for the READ operation
Update(PUT or PATCH)
The update is used to modify existing records in the database. For example, this can be used to change the content in the user's post or the user's profile. Similar to READ, UPDATEs can be applied across all records or only a few, based on criteria.
PUT should be used when you want the entire entry updated, and PATCH if you don’t want the entire entry to be modified.
In a SQL database, you use UPDATE to update an entry. In a NoSQL database like MongoDB, you can implement an update feature with the findByIdAndUpdate() method.
The GIF below shows how the UPDATE operation works
Delete(DELETE)
DELETE operations allow the user to remove records from the database. A hard delete removes the record altogether, while a soft delete flags the record but leaves it in place. For example, if the user has deactivated his/her social media account it remains in a database for future retrieval.
In a SQL database, DELETE is used to delete an entry. In a NoSQL database like MongoDB, you can implement delete with the findByIdAndDelete() method.
The GIF below shows how the DELETE operation works
We can sum up CRUD in this way:
- You upload a social media post - CREATE
- You get access to your uploaded post and people can search for you – READ
- You made a mistake in your post and edit it – UPDATE
- You become aware of your privacy and delete some of your posts - DELETE
Links to my project: Live Github
That's it and by now you must have understood what CRUD means. If you learnt something new please react or leave comments. Connect with me on Twitter and LinkedIn.
Happy Coding👋