refactor(postgres): use generics for IDs
Some checks failed
Gitea Build Check / Build (push) Failing after 4m57s
Some checks failed
Gitea Build Check / Build (push) Failing after 4m57s
Signed-off-by: soxx <soxx@fenpa.ws>
This commit is contained in:
parent
d90736b1b6
commit
31c4249129
@ -7,27 +7,26 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ID interface {
|
type ID interface {
|
||||||
AnthroveUserID
|
AnthroveUserID | AnthroveSourceID | AnthrovePostID
|
||||||
AnthroveSourceID
|
|
||||||
AnthrovePostID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type BaseModel struct {
|
type BaseModel[T ID] struct {
|
||||||
ID string `gorm:"primaryKey"`
|
ID T `gorm:"primaryKey"`
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (base *BaseModel) BeforeCreate(db *gorm.DB) error {
|
func (base *BaseModel[T]) BeforeCreate(db *gorm.DB) error {
|
||||||
|
var defaultVar T
|
||||||
|
|
||||||
if base.ID == "" {
|
if base.ID == defaultVar {
|
||||||
id, err := gonanoid.New(25)
|
id, err := gonanoid.New(25)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
base.ID = id
|
base.ID = T(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Reference in New Issue
Block a user