Encrypted messaging app
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.

31 lines
468 B

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