CRUD is a collection of database operations. To understand CRUD, let us look into a simple database like postgres. Now before we proceed further, what are the different operation we can perform on a database/table.
A. we can create one or more entries in the database.
B. we can read one or more entries in the database.
C. we can update one or more entries in the database.
D. we can delete one or more entries in the database.
there are a few other database operations like creating/dropping new tables too but, they do not directly work on the data and instead work on the containers that hold the data (tables). So, they are rather moot for this discussion.
On the other hand, APIs are a collection of user/automated actions. For APIs to work seamlessly, they need to store data and preserve context. Let us understand this in detail.
Now let us say there is an /order
API which gives result of all the purchase orders you have made in an e-commerce website. For this to give us the
list of orders, it has to be stored in some place ie, it needs context. This is where database come into picture. We can store all the information relevant for
APIs to work seamlessly in databases.
CRUD operations and HTTP methods are tightly related in terms of the responsibility that they handle and is very intuitive to understand. The table below illustrates how different API methods interact with the database.
Operation | HTTP Method | Description |
---|---|---|
CREATE | POST | making new entry in one or more tables/databases |
READ | GET | reading entries from one or more tables/databases |
UPDATE | PUT/PATCH | update existing entires from one or more tables/databases |
DELETE | DELETE | delete existing entries from one or more tables/databases |
So, by the table we see that POST calls usually involve one or more CREATE operations. GET method involve one or more READ operations. UPDATE method involve one or more PUT/PATCH operations. DELETE method involve one or more DELETE operations.
Lets understand the relation between HTTP methods and CRUD with following examples.
BASE SCENARIO:
Consider the implementation of an online e-commerce cart. For simplicity, let us assume all cart related information like user id, items added, quantity and price are being stored in cart table. with this as base, let us analyse diiferent operations involved with cart information.
* Adding new product to the cart would require a POST call because we have to CREATE a new entry in cart table for the user.
* Deleting an item from the cart would require a DELETE call because we have to remove the entry from the cart table for the user.
* Increasing the quantity of an item already in the cart would require a PUT/PATCH request because, this would require an UPDATE to an existing entry in the cart table.
* When you have added multiple items to your cart, Now you go to carts page this would require a GET call to fetch all the cart items from cart table and will result in multiple READ operations.
Quick Links
Legal Information
Social Media