This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
otter-space-sdk/pkg/models/orm.go
SoXX 251611c5a0 feat: added json notation
Signed-off-by: SoXX <soxx@fenpa.ws>
2024-07-15 15:10:44 +02:00

35 lines
595 B
Go

package models
import (
"time"
gonanoid "github.com/matoous/go-nanoid/v2"
"gorm.io/gorm"
)
type ID interface {
AnthroveUserID | AnthroveSourceID | AnthrovePostID
}
type BaseModel[T ID] struct {
ID T `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `json:"-" 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
}