Write from a request
A model decoded straight from a request body takes every field the body names, including its key, its timestamps and its relations, and ant writes them as given. Decode into an input struct that holds what the client may set, and copy those fields into the model.
Decode into an input struct
import "encoding/json"
type NewPost struct {
Title string
}
func createPost(w http.ResponseWriter, r *http.Request) {
var in NewPost
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
http.Error(w, "bad body", http.StatusBadRequest)
return
}
post := &Post{
UserID: signedIn(r), // from the session, not the body
Title: in.Title,
}
if err := db.Posts.Insert(r.Context(), post); err != nil {
http.Error(w, "could not save", http.StatusInternalServerError)
}
}
INSERT INTO "posts" ("created_at", "updated_at", "user_id", "title", "published")
VALUES ($1, $2, $3, $4, $5) RETURNING "id"
A body such as {"Title": "Mine", "id": 77, "User": {"Name": "Evil"}} sets the title and nothing else, since NewPost has no other field.
What a body reaches in a model
Decoded into Post itself, the same body chooses the key and inserts a user:
import "encoding/json"
var decoded Post
err := json.Unmarshal(body, &decoded) // {"Title": "Mine", "id": 77, "User": {"Name": "Evil", ...}}
err = db.Posts.Insert(ctx, &decoded)
// INSERT INTO "users" ("created_at", "updated_at", "name", "email", "age", "bio") VALUES (...) RETURNING "id"
// INSERT INTO "posts" ("id", "created_at", "updated_at", "user_id", "title", "published") VALUES ($1, ...) -- $1 = 77
| In the body | What the insert does with it |
|---|---|
A key, "id": 77 | Writes it: a non-zero key is inserted as given |
"createdAt" | Keeps it, as any CreatedAt you set |
"Version": 9 | Starts the row at that version |
"DeletedAt" with a time | Inserts the row already soft-deleted |
A parent, "User": {...} | Inserts it, and links the post to it in place of UserID |
Children, "Posts": [...] on a user | Inserts them |
encoding/json matches keys without regard to case, so "ID" and "CreatedAt" reach those fields too.
InsertOnly leaves the relations out, and writes the rest as given.
Update from a request
Decoding into a tracked entity writes over what was loaded. "Posts": [] empties a loaded relation, and SaveChanges deletes those posts. Decode into an input struct here too, and copy what it holds:
import "encoding/json"
type Rename struct {
Name string
}
session := ant.Session(db)
user, err := session.Users.Include(&Users.Posts).Find(ctx, id)
var in Rename
err = json.Unmarshal(body, &in) // {"Name": "Alice B.", "Posts": []}
user.Name = in.Name
err = session.SaveChanges(ctx)
// UPDATE "users" SET "name" = $1, "updated_at" = $2 WHERE "users"."id" = $3
For a field the client may leave out, give the input struct a pointer, such as Name *string, and copy it only when it isn't nil.
See it tested: tests/features/writing/from_request_test.go