-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
35 lines (30 loc) · 945 Bytes
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package db
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
// BaseModel defines the common columns that all db structs should hold, usually
// db structs based on this have no soft delete
type BaseModel struct {
// ID should use uuid_generate_v4() for the pk's
ID uuid.UUID `gorm:"primary_key;type:uuid"`
CreatedAt time.Time `gorm:"index;not null"` // (My|Postgre)SQL
UpdatedAt *time.Time `gorm:"index"`
}
// BaseModelSoftDelete defines the common columns that all db structs should
// hold, usually. This struct also defines the fields for GORM triggers to
// detect the entity should soft delete
type BaseModelSoftDelete struct {
BaseModel
DeletedAt *time.Time `sql:"index"`
}
// BeforeCreate will set a UUID rather than numeric ID.
func (base *BaseModel) BeforeCreate(scope *gorm.DB) error {
uuid, err := uuid.NewRandom()
if err != nil {
return err
}
scope.Statement.SetColumn("ID", uuid)
return nil
}