database/entities.go

28 lines
552 B
Go
Raw Normal View History

2025-03-27 14:21:57 +01:00
package database
import (
"github.com/satori/go.uuid"
"gorm.io/gorm"
"time"
)
// EntityBase contains common columns for all tables.
type EntityBase struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;"`
CreatedAt time.Time
UpdatedAt *time.Time
//DeletedAt *time.Time
}
// BeforeCreate will set a UUID rather than numeric ID.
func (base *EntityBase) BeforeCreate(tx *gorm.DB) error {
base.ID = uuid.NewV4()
return nil
}
func (base *EntityBase) BeforeUpdate(tx *gorm.DB) error {
now := time.Now()
base.UpdatedAt = &now
return nil
}