You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
574 B

  1. package Models
  2. import (
  3. "time"
  4. "github.com/gofrs/uuid"
  5. "gorm.io/gorm"
  6. )
  7. // Base contains common columns for all tables.
  8. type Base struct {
  9. ID uuid.UUID `gorm:"type:uuid;primary_key;" json:"id"`
  10. CreatedAt time.Time `json:"created_at"`
  11. UpdatedAt time.Time `json:"updated_at"`
  12. DeletedAt *time.Time `sql:"index" json:"-"`
  13. }
  14. // BeforeCreate will set a UUID rather than numeric ID.
  15. func (base *Base) BeforeCreate(tx *gorm.DB) error {
  16. var (
  17. id uuid.UUID
  18. err error
  19. )
  20. id, err = uuid.NewV4()
  21. if err != nil {
  22. return err
  23. }
  24. base.ID = id
  25. return nil
  26. }