Lesson 1: The CRUD Lexicon
Create, Read, Update, Delete
Posted on December 22, 2021
Create, Read, Update, Delete
In nearly any application you have some 'domain' - it could be anything, a user, a post, a payment. It represents some real life object and has some purpose. This domain is usually represented by one or more objects in actual code. In almost any application, you need the following functionalities for those objects, and typically in this order:
- Create that object
- Read that object
- Update that object
- Delete that object
Thus the CRUD acryonym.
If we're talking to our domain via HTTP protocol, these CRUD operatios map 1:1 with the following HTTP methods:
- POST
- GET
- PATCH
- DELETE
Therefore, we only need a single accessible endpoint, where the actual logic behind the domain is determined at the HTTP method level.
Weapon of Choice: Go
I used to write backends in Node. Then I wrote them in .NET. Now I write them in Go. Why Go? With just one file, and a single command to start it up, we can create a HTTP CRUD service for our domains.
Let's imagine our domain is a user - this one is common enough. We'll first define our user type:
// User represents data about a user.
type User struct {
ID int64 `json:"id"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Email string `json:"email"`
}
package main
import (
"github.com/gin-gonic/gin"
"example/api/services/albumservice"
)
func main() {
router := gin.Default()
// User CRUD operations
router.POST("/users", userService.CreateUser)
router.GET("/users/:id", userService.GetUser)
router.PATCH("/users/:id", userService.UpdateUser)
router.DELETE("/users/:id", userService.DeleteUser)
// Listening Host
router.Run("localhost:8080")
}