Skip to main content

CRUD Operations

To perform operations on your database first make sure database is enabled in the config/gorm.go

Create

Here is an example of how you can create a database record

package controllers

import (
"git.smarteching.com/goffee/core/v2"
"git.smarteching.com/goffee/[myapp]/models"
)

func Signup(c *core.Context) *core.Response {
db := c.GetGorm()
hashedPassword, _ := c.GetHashing().HashPassword("my-password")
user := models.User{
Name: "Jack",
Email: "mail@example.com",
Password: hashedPassword,
}
result := db.Create(&user) // pass pointer
// user.ID: the inserted record primary key
// result.Error: returned error if any

return c.Response.Json(`{"message": "user created"}`)
}

More information is available at gorm record creating docs

Read

Here is how you can read records from the database

package controllers

import (
"errors"

"git.smarteching.com/goffee/core/v2"
"git.smarteching.com/goffee/[myapp]/models"
"gorm.io/gorm"
)

func SomeHandler(c *core.Context) *core.Response {
db := c.GetGorm()

var user models.User
result := db.First(&user, 1) // find user id 1
// result.RowsAffected: found records count
// result.Error: if there is any error
errors.Is(result.Error, gorm.ErrRecordNotFound) // if record not found, the error ErrRecordNotFound is returned

db.Where("email = ?", "mail@mail.com").First(&user) // find the first user with email mail@mail.com

// Get the first record you find (ordered by primary key)
db.First(&user) // SELECT * FROM users ORDER BY id LIMIT 1;

// Get one record, no specified order
db.Take(&user) // SELECT * FROM users LIMIT 1;

// Get the last record ordered by primary key desc
db.Last(&user) // SELECT * FROM users ORDER BY id DESC LIMIT 1;

// Get all records
var users []models.User
result = db.Find(&users) // SELECT * FROM users;

return c.Response.Json(`{"message": "ok"}`)
}

More information is available at gorm record querying docs

For more advanced queries check GORM advanced queries docs

Update

Here is how you can update a record

package controllers

import (
"git.smarteching.com/goffee/core/v2"
"git.smarteching.com/goffee/[myapp]/models"
)

func SomeHandler(c *core.Context) *core.Response {
db := c.GetGorm()

var user models.User
db.First(&user)

user.Name = "Joe"
user.Age = 40
db.Save(&user) // Note: save creates the record if missing

return c.Response.Json(`{"message": "ok"}`)
}

More information is available at gorm records updating docs

Deleting

Here is how you can delete records:

package controllers

import (
"git.smarteching.com/goffee/core/v2"
"git.smarteching.com/goffee/[myapp]/models"
)

func SomeHandler(c *core.Context) *core.Response {
db := c.GetGorm()

db.Delete(&models.User{}, 10) // DELETE FROM users WHERE id = 10;

db.Delete(&models.User{}, []int{1, 2, 3}) // DELETE FROM users WHERE id IN (1,2,3)

return c.Response.Json(`{"message": "ok"}`)
}

More information is available at gorm records deleting docs

Raw SQL

The docs are available at gorm raw sql docs

Transactions

The docs are available at gorm transactions docs