model.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package model
  2. import (
  3. "time"
  4. "gorm.io/gen"
  5. "gorm.io/gorm"
  6. )
  7. var db *gorm.DB
  8. type Model struct {
  9. ID uint64 `gorm:"primary_key" json:"id"`
  10. CreatedAt time.Time `json:"created_at"`
  11. UpdatedAt time.Time `json:"updated_at"`
  12. DeletedAt *gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
  13. }
  14. func GenerateAllModel() []any {
  15. return []any{
  16. ConfigBackup{},
  17. User{},
  18. AuthToken{},
  19. Cert{},
  20. ChatGPTLog{},
  21. Site{},
  22. Stream{},
  23. DnsCredential{},
  24. Environment{},
  25. Notification{},
  26. AcmeUser{},
  27. BanIP{},
  28. Config{},
  29. Passkey{},
  30. EnvGroup{},
  31. ExternalNotify{},
  32. }
  33. }
  34. func Use(tx *gorm.DB) {
  35. db = tx
  36. }
  37. func UseDB() *gorm.DB {
  38. return db
  39. }
  40. type Pagination struct {
  41. Total int64 `json:"total"`
  42. PerPage int `json:"per_page"`
  43. CurrentPage int `json:"current_page"`
  44. TotalPages int64 `json:"total_pages"`
  45. }
  46. type DataList struct {
  47. Data interface{} `json:"data"`
  48. Pagination Pagination `json:"pagination,omitempty"`
  49. }
  50. type Method interface {
  51. // FirstByID Where("id=@id")
  52. FirstByID(id uint64) (*gen.T, error)
  53. // DeleteByID update @@table set deleted_at=strftime('%Y-%m-%d %H:%M:%S','now') where id=@id
  54. DeleteByID(id uint64) error
  55. }