29 lines
424 B
Go
29 lines
424 B
Go
package pgModels
|
|
|
|
import (
|
|
gonanoid "github.com/matoous/go-nanoid/v2"
|
|
"gorm.io/gorm"
|
|
"time"
|
|
)
|
|
|
|
type BaseModel struct {
|
|
ID string `gorm:"primaryKey"`
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
DeletedAt gorm.DeletedAt `gorm:"index"`
|
|
}
|
|
|
|
func (base *BaseModel) BeforeCreate(db *gorm.DB) error {
|
|
|
|
if base.ID == "" {
|
|
id, err := gonanoid.New(25)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
base.ID = id
|
|
}
|
|
|
|
return nil
|
|
}
|