database/entities.go

28 lines
604 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 {
2025-04-18 17:53:53 +02:00
ID uuid.UUID `gorm:"type:uuid;primary_key;" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
2025-03-27 14:21:57 +01:00
//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
}