Create, Read, Update, Delete - the four basic operations for managing data in any application.
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.
Add new records to the database.
SQL:
INSERT INTO users (name, email)
VALUES ('Alice', 'alice@email.com');
Code:
// Create new user
const user = await User.create({
name: "Alice",
email: "alice@email.com"
})
HTTP: POST request to /users
Retrieve data from the database.
No related topics found.
SQL:
-- Get all users
SELECT * FROM users;
-- Get specific user
SELECT * FROM users WHERE id = 1;
Code:
// 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
Change existing records.
SQL:
UPDATE users
SET email = 'newemail@email.com'
WHERE id = 1;
Code:
const user = await User.findById(1)
user.email = "newemail@email.com"
await user.save()
HTTP: PUT or PATCH request to /users/1
Remove records from the database.
SQL:
DELETE FROM users WHERE id = 1;
Code:
const user = await User.findById(1)
await user.destroy()
HTTP: DELETE request to /users/1
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 |
// 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!
// 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" })
}
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)
Soft Delete: Mark as deleted instead of removing
user.deletedAt = new Date()
await user.save()
Bulk Operations: Create/update/delete multiple records
await User.bulkCreate([user1, user2, user3])
Upsert: Create if not exists, update if exists
await User.upsert({ id: 1, name: "Alice" })
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.
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.