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 31c4249129
Some checks failed
Gitea Build Check / Build (push) Failing after 4m57s
refactor(postgres): use generics for IDs
Signed-off-by: soxx <soxx@fenpa.ws>
2024-06-24 22:31:18 +02:00

34 lines
530 B
Go

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
}