vaulterm/server/models/base_model.go

32 lines
542 B
Go
Raw Normal View History

2024-11-07 19:07:41 +00:00
package models
import (
"strings"
"time"
"github.com/oklog/ulid/v2"
"gorm.io/gorm"
)
2024-11-09 10:33:07 +00:00
type Model struct {
2024-11-07 19:07:41 +00:00
ID string `gorm:"primarykey;type:varchar(26)" json:"id"`
}
2024-11-09 10:33:07 +00:00
func (m *Model) BeforeCreate(tx *gorm.DB) error {
2024-11-07 19:07:41 +00:00
m.ID = m.GenerateID()
return nil
}
2024-11-09 10:33:07 +00:00
func (m *Model) GenerateID() string {
2024-11-07 19:07:41 +00:00
return strings.ToLower(ulid.Make().String())
}
type Timestamps struct {
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type SoftDeletes struct {
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
}