1
0

model.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package model
  2. import (
  3. "time"
  4. "github.com/google/uuid"
  5. "gorm.io/gen"
  6. "gorm.io/gorm"
  7. )
  8. var db *gorm.DB
  9. type Model struct {
  10. ID uint64 `gorm:"primary_key" json:"id"`
  11. CreatedAt time.Time `json:"created_at"`
  12. UpdatedAt time.Time `json:"updated_at"`
  13. DeletedAt *gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
  14. }
  15. // BaseModelUUID defines a base model with UUID as the primary key.
  16. type BaseModelUUID struct {
  17. ID uuid.UUID `gorm:"type:uuid;primary_key;" json:"id"`
  18. CreatedAt time.Time `json:"created_at"`
  19. UpdatedAt time.Time `json:"updated_at"`
  20. }
  21. // BeforeCreate will set a UUID rather than numeric ID.
  22. func (base *BaseModelUUID) BeforeCreate(tx *gorm.DB) (err error) {
  23. if base.ID == uuid.Nil {
  24. base.ID = uuid.New()
  25. }
  26. return
  27. }
  28. func GenerateAllModel() []any {
  29. return []any{
  30. ConfigBackup{},
  31. User{},
  32. AuthToken{},
  33. Cert{},
  34. LLMSession{},
  35. Site{},
  36. Stream{},
  37. DnsCredential{},
  38. Node{},
  39. Notification{},
  40. AcmeUser{},
  41. BanIP{},
  42. Config{},
  43. Passkey{},
  44. Namespace{},
  45. ExternalNotify{},
  46. AutoBackup{},
  47. SiteConfig{},
  48. NginxLogIndex{},
  49. }
  50. }
  51. func Use(tx *gorm.DB) {
  52. db = tx
  53. }
  54. func UseDB() *gorm.DB {
  55. return db
  56. }
  57. type Pagination struct {
  58. Total int64 `json:"total"`
  59. PerPage int `json:"per_page"`
  60. CurrentPage int `json:"current_page"`
  61. TotalPages int64 `json:"total_pages"`
  62. }
  63. type DataList struct {
  64. Data interface{} `json:"data"`
  65. Pagination Pagination `json:"pagination,omitempty"`
  66. }
  67. type Method interface {
  68. // FirstByID Where("id=@id")
  69. FirstByID(id uint64) (*gen.T, error)
  70. // DeleteByID update @@table set deleted_at=strftime('%Y-%m-%d %H:%M:%S','now') where id=@id
  71. DeleteByID(id uint64) error
  72. }