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
556 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:"-"`
  11. UpdatedAt time.Time `json:"-"`
  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. }