|
|
- package Models
-
- import (
- "time"
-
- "github.com/gofrs/uuid"
- "gorm.io/gorm"
- )
-
- // Base contains common columns for all tables.
- type Base struct {
- ID uuid.UUID `gorm:"type:uuid;primary_key;" json:"id"`
- CreatedAt time.Time `json:"created_at"`
- UpdatedAt time.Time `json:"updated_at"`
- DeletedAt *time.Time `sql:"index" json:"-"`
- }
-
- // BeforeCreate will set a UUID rather than numeric ID.
- func (base *Base) BeforeCreate(tx *gorm.DB) error {
- var (
- id uuid.UUID
- err error
- )
-
- id, err = uuid.NewV4()
- if err != nil {
- return err
- }
-
- base.ID = id
- return nil
- }
|