model.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package model
  2. import (
  3. "gorm.io/gen"
  4. "gorm.io/gorm"
  5. "time"
  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. SiteCategory{},
  31. }
  32. }
  33. func Use(tx *gorm.DB) {
  34. db = tx
  35. }
  36. func UseDB() *gorm.DB {
  37. return db
  38. }
  39. type Pagination struct {
  40. Total int64 `json:"total"`
  41. PerPage int `json:"per_page"`
  42. CurrentPage int `json:"current_page"`
  43. TotalPages int64 `json:"total_pages"`
  44. }
  45. type DataList struct {
  46. Data interface{} `json:"data"`
  47. Pagination Pagination `json:"pagination,omitempty"`
  48. }
  49. type Method interface {
  50. // FirstByID Where("id=@id")
  51. FirstByID(id uint64) (*gen.T, error)
  52. // DeleteByID update @@table set deleted_at=strftime('%Y-%m-%d %H:%M:%S','now') where id=@id
  53. DeleteByID(id uint64) error
  54. }