## What are CRUD Operations?
CRUD stands for Create, Read, Update, Delete - the four basic operations you perform on data. Every application that stores data uses CRUD. Whether you are building a blog, e-commerce site, or social network, CRUD is the foundation.
Think of CRUD like basic file operations: create a file, open and read it, edit it, delete it. Same concept, but for database records.
## The Four Operations
### Create (Add New Data)
Add new records to the database.
**SQL**:
```sql
INSERT INTO users (name, email)
VALUES ('Alice', 'alice@email.com');
```
**Code**:
```javascript
// Create new user
const user = await User.create({
name: "Alice",
email: "alice@email.com"
})
```
**HTTP**: POST request to `/users`
### Read (Get Data)
Retrieve data from the database.
**SQL**:
```sql
-- Get all users
SELECT * FROM users;
-- Get specific user
SELECT * FROM users WHERE id = 1;
```
**Code**:
```javascript
// Get all users
const users = await User.findAll()
// Get one user
const user = await User.findById(1)
```
**HTTP**: GET request to `/users` or `/users/1`
### Update (Modify Existing Data)
Change existing records.
**SQL**:
```sql
UPDATE users
SET email = 'newemail@email.com'
WHERE id = 1;
```
**Code**:
```javascript
const user = await User.findById(1)
user.email = "newemail@email.com"
await user.save()
```
**HTTP**: PUT or PATCH request to `/users/1`
### Delete (Remove Data)
Remove records from the database.
**SQL**:
```sql
DELETE FROM users WHERE id = 1;
```
**Code**:
```javascript
const user = await User.findById(1)
await user.destroy()
```
**HTTP**: DELETE request to `/users/1`
## CRUD in REST APIs
REST APIs map CRUD operations to HTTP methods:
| Operation | HTTP Method | URL | Purpose |
|-----------|-------------|---------------|-------------------|
| Create | POST | /users | Create new user |
| Read | GET | /users | Get all users |
| Read | GET | /users/1 | Get specific user |
| Update | PUT/PATCH | /users/1 | Update user |
| Delete | DELETE | /users/1 | Delete user |
## Building a CRUD API
```javascript
// Create
app.post("/users", async (req, res) => {
const user = await User.create(req.body)
res.status(201).json(user)
})
// Read all
app.get("/users", async (req, res) => {
const users = await User.findAll()
res.json(users)
})
// Read one
app.get("/users/:id", async (req, res) => {
const user = await User.findById(req.params.id)
res.json(user)
})
// Update
app.put("/users/:id", async (req, res) => {
const user = await User.findById(req.params.id)
await user.update(req.body)
res.json(user)
})
// Delete
app.delete("/users/:id", async (req, res) => {
await User.destroy({ where: { id: req.params.id } })
res.status(204).send()
})
```
That is a complete CRUD API in 30 lines!
## CRUD in Frontend
```javascript
// Create
const createUser = async (userData) => {
const response = await fetch("/api/users", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(userData)
})
return await response.json()
}
// Read
const getUsers = async () => {
const response = await fetch("/api/users")
return await response.json()
}
// Update
const updateUser = async (id, updates) => {
const response = await fetch(`/api/users/${id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(updates)
})
return await response.json()
}
// Delete
const deleteUser = async (id) => {
await fetch(`/api/users/${id}`, { method: "DELETE" })
}
```
## Beyond Basic CRUD
Real applications add complexity:
**Validation**: Ensure data is correct before saving
**Authentication**: Only allow authorized users
**Pagination**: Do not return all records at once
**Filtering**: Search and filter results
**Sorting**: Order results by field
**Relationships**: Handle related data (user has many posts)
## Common CRUD Patterns
**Soft Delete**: Mark as deleted instead of removing
```javascript
user.deletedAt = new Date()
await user.save()
```
**Bulk Operations**: Create/update/delete multiple records
```javascript
await User.bulkCreate([user1, user2, user3])
```
**Upsert**: Create if not exists, update if exists
```javascript
await User.upsert({ id: 1, name: "Alice" })
```
## CRUD Applications
Almost every app is a CRUD app at its core:
**Blog**: Create posts, read posts, update posts, delete posts
**Todo App**: Create tasks, read tasks, complete tasks, delete tasks
**E-commerce**: Create products, view products, update inventory, remove products
**Social Media**: Create posts, view feed, edit posts, delete posts
Master CRUD and you can build 80% of web applications.
## The Bottom Line
CRUD is the foundation of data-driven applications. Every developer must understand these four operations. They are simple but powerful - the building blocks of everything you will create.