package models import ( gonanoid "github.com/matoous/go-nanoid/v2" "gorm.io/gorm" "time" ) type ID interface { AnthroveUserID | AnthroveSourceID | AnthrovePostID } type BaseModel[T ID] struct { ID T `gorm:"primaryKey"` CreatedAt time.Time UpdatedAt time.Time DeletedAt gorm.DeletedAt `gorm:"index"` } func (base *BaseModel[T]) BeforeCreate(db *gorm.DB) error { var defaultVar T if base.ID == defaultVar { id, err := gonanoid.New(25) if err != nil { return err } base.ID = T(id) } return nil }