CRUD Operations with FastAPI
Key Concepts
CRUD stands for Create, Read, Update, and Delete. These are the four basic operations that can be performed on data. In FastAPI, these operations are implemented using HTTP methods and route handlers. Here are the key concepts:
- Create: Adding new data to the database.
- Read: Retrieving existing data from the database.
- Update: Modifying existing data in the database.
- Delete: Removing existing data from the database.
Explaining Each Concept
1. Create
The Create operation is used to add new data to the database. In FastAPI, this is typically implemented using the HTTP POST method.
Example:
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str description: str @app.post("/items/") async def create_item(item: Item): # Simulate adding the item to the database return {"item": item}
2. Read
The Read operation is used to retrieve data from the database. In FastAPI, this is typically implemented using the HTTP GET method.
Example:
from fastapi import FastAPI app = FastAPI() items_db = [ {"id": 1, "name": "Item 1", "description": "Description 1"}, {"id": 2, "name": "Item 2", "description": "Description 2"}, ] @app.get("/items/{item_id}") async def read_item(item_id: int): for item in items_db: if item["id"] == item_id: return item return {"message": "Item not found"}
3. Update
The Update operation is used to modify existing data in the database. In FastAPI, this is typically implemented using the HTTP PUT or PATCH method.
Example:
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str description: str items_db = [ {"id": 1, "name": "Item 1", "description": "Description 1"}, {"id": 2, "name": "Item 2", "description": "Description 2"}, ] @app.put("/items/{item_id}") async def update_item(item_id: int, item: Item): for i in range(len(items_db)): if items_db[i]["id"] == item_id: items_db[i] = {"id": item_id, **item.dict()} return {"message": "Item updated"} return {"message": "Item not found"}
4. Delete
The Delete operation is used to remove existing data from the database. In FastAPI, this is typically implemented using the HTTP DELETE method.
Example:
from fastapi import FastAPI app = FastAPI() items_db = [ {"id": 1, "name": "Item 1", "description": "Description 1"}, {"id": 2, "name": "Item 2", "description": "Description 2"}, ] @app.delete("/items/{item_id}") async def delete_item(item_id: int): for i in range(len(items_db)): if items_db[i]["id"] == item_id: del items_db[i] return {"message": "Item deleted"} return {"message": "Item not found"}
Analogies
Think of CRUD operations as the basic actions you can perform on a library's catalog. Creating is like adding a new book to the catalog, reading is like looking up a book's details, updating is like correcting a book's information, and deleting is like removing a book from the catalog.
Another analogy is a to-do list app. Creating is adding a new task, reading is viewing the list of tasks, updating is editing a task, and deleting is removing a completed task.
By mastering CRUD operations in FastAPI, you can build powerful and flexible APIs that interact seamlessly with databases.