|
| 1 | +# Database Client Mutations |
| 2 | + |
| 3 | +::: info |
| 4 | + |
| 5 | +For the following page, we will take as an example the following postgres schema. The usage should be similar regardless of the driver or settings. |
| 6 | + |
| 7 | +::: code-group |
| 8 | + |
| 9 | +```sql [Schema] |
| 10 | +CREATE TABLE users ( |
| 11 | + id UUID PRIMARY KEY, |
| 12 | + name VARCHAR(64) NOT NULL, |
| 13 | + email VARCHAR(64) NOT NULL, |
| 14 | + created_at TIMESTAMP NOT NULL DEFAULT NOW(), |
| 15 | + updated_at TIMESTAMP NOT NULL DEFAULT NOW(), |
| 16 | + deleted_at TIMESTAMP |
| 17 | +); |
| 18 | +``` |
| 19 | + |
| 20 | +```go [Generated Structs] |
| 21 | +// Base Model Returned by most API |
| 22 | +type UserModel struct { |
| 23 | + Id uuid.UUID `json:"id" db:"id"` |
| 24 | + Email string `json:"email" db:"email"` |
| 25 | + Name string `json:"name" db:"name"` |
| 26 | + CreatedAt time.Time `json:"created_at" db:"created_at"` |
| 27 | + UpdatedAt time.Time `json:"updated_at" db:"updated_at"` |
| 28 | + DeletedAt *time.Time `json:"deleted_at" db:"deleted_at"` |
| 29 | +} |
| 30 | + |
| 31 | +// Input struct used only for creation |
| 32 | +type UserCreate struct { |
| 33 | + Email string `json:"email" db:"email"` |
| 34 | + Name string `json:"name" db:"name"` |
| 35 | +} |
| 36 | + |
| 37 | +// Input struct used only for update |
| 38 | +type UserUpdate struct { |
| 39 | + Id uuid.UUID `json:"id" db:"id"` |
| 40 | + Email string `json:"email" db:"email"` |
| 41 | + Name string `json:"name" db:"name"` |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +```go [Generated Mutations] |
| 46 | +// Insert |
| 47 | +db.User.Insert(input UserCreate) (*UserModel, error) |
| 48 | +db.User.InsertMany(inputs []UserCreate) ([]UserPrimaryKeySerialized, error) |
| 49 | + |
| 50 | +// Update |
| 51 | +db.User.Update(input UserUpdate) (*UserModel, error) |
| 52 | +db.User.UpdateMany(inputs []UserUpdate) ([]UserPrimaryKeySerialized, error) |
| 53 | + |
| 54 | +// Upsert |
| 55 | +db.User.Upsert(input UserUpdate) (*UserModel, error) |
| 56 | +db.User.UpsertMany(inputs []UserUpdate) ([]UserPrimaryKeySerialized, error) |
| 57 | + |
| 58 | +// Delete |
| 59 | +db.User.DeleteSoft(id UserPrimaryKey) error |
| 60 | +db.User.DeleteHard(id UserPrimaryKey) |
| 61 | +``` |
| 62 | + |
| 63 | +::: |
| 64 | + |
| 65 | +## Insert |
| 66 | + |
| 67 | +```go |
| 68 | +user, err := db.User.Insert(database.UserCreate{ |
| 69 | + Name: "John Doe", |
| 70 | + Email: "john@email.com", |
| 71 | +}) |
| 72 | +``` |
| 73 | + |
| 74 | +::: tip |
| 75 | + |
| 76 | +If you have more than one entry to add to database, you also have a bulk insert alternative which takes a slices of input |
| 77 | + |
| 78 | +```go |
| 79 | +userIds, err := db.User.InsertMany([]database.UserCreate{ |
| 80 | + // ... many users |
| 81 | +}) |
| 82 | +``` |
| 83 | + |
| 84 | +::: |
| 85 | + |
| 86 | +## Update |
| 87 | + |
| 88 | +```go |
| 89 | +user, err := db.User.Update(database.UserUpdate{ |
| 90 | + Id: "00000000-0000-0000-0000-000000000000", |
| 91 | + Name: "John Doe 2", |
| 92 | + Email: "john@email.com", |
| 93 | +}) |
| 94 | +``` |
| 95 | + |
| 96 | +::: tip |
| 97 | + |
| 98 | +If you have more than one entry to add to database, you also have a bulk update alternative which takes a slices of input |
| 99 | + |
| 100 | +```go |
| 101 | +userIds, err := db.User.UpdateMany([]database.UserUpdate{ |
| 102 | + // ... many users |
| 103 | +}) |
| 104 | +``` |
| 105 | + |
| 106 | +::: |
| 107 | + |
| 108 | +## Upsert |
| 109 | + |
| 110 | +Upsert stands for Insert in database if the entry doesn't exist yet, or update the existing entry. In both cases, the entry is returned. |
| 111 | + |
| 112 | +This is really convenient for operations like user creation or session, when the api doesn't know if the user already exists in the database or not. |
| 113 | + |
| 114 | +```go |
| 115 | +user, err := db.User.Upsert(database.UserUpdate{ |
| 116 | + Id: "00000000-0000-0000-0000-000000000000", |
| 117 | + Name: "John Doe 2", |
| 118 | + Email: "john@email.com", |
| 119 | +}) |
| 120 | +``` |
| 121 | + |
| 122 | +::: tip |
| 123 | + |
| 124 | +If you have more than one entry to add to database, you also have a bulk upsert alternative which takes a slices of input |
| 125 | + |
| 126 | +```go |
| 127 | +userIds, err := db.User.UpsertMany([]database.UserUpdate{ |
| 128 | + // ... many users |
| 129 | +}) |
| 130 | +``` |
| 131 | + |
| 132 | +::: |
| 133 | + |
| 134 | +## Delete |
| 135 | + |
| 136 | +```go |
| 137 | +// Default SQL Delete |
| 138 | +err = db.User.DeleteHard(3) |
| 139 | +``` |
| 140 | + |
| 141 | +And if you are using Soft Delete, a second method will we automatically generated |
| 142 | + |
| 143 | +```go |
| 144 | +// Soft Delete backed by a Timestamp field |
| 145 | +err = db.User.DeleteSoft(3) |
| 146 | +``` |
0 commit comments