26 lines
627 B
Go
26 lines
627 B
Go
package database
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// BaseModel contains common fields for all models
|
|
// This replaces gorm.Model but uses UUID instead of uint for ID
|
|
type BaseModel struct {
|
|
ID string `gorm:"type:uuid;primary_key" json:"id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
|
}
|
|
|
|
// BeforeCreate hook to set UUID before creating a record
|
|
func (b *BaseModel) BeforeCreate(tx *gorm.DB) error {
|
|
if b.ID == "" {
|
|
b.ID = uuid.New().String()
|
|
}
|
|
return nil
|
|
}
|