model.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. Node{},
  25. Notification{},
  26. AcmeUser{},
  27. BanIP{},
  28. Config{},
  29. Passkey{},
  30. Namespace{},
  31. ExternalNotify{},
  32. AutoBackup{},
  33. SiteConfig{},
  34. }
  35. }
  36. func Use(tx *gorm.DB) {
  37. db = tx
  38. }
  39. func UseDB() *gorm.DB {
  40. return db
  41. }
  42. type Pagination struct {
  43. Total int64 `json:"total"`
  44. PerPage int `json:"per_page"`
  45. CurrentPage int `json:"current_page"`
  46. TotalPages int64 `json:"total_pages"`
  47. }
  48. type DataList struct {
  49. Data interface{} `json:"data"`
  50. Pagination Pagination `json:"pagination,omitempty"`
  51. }
  52. type Method interface {
  53. // FirstByID Where("id=@id")
  54. FirstByID(id uint64) (*gen.T, error)
  55. // DeleteByID update @@table set deleted_at=strftime('%Y-%m-%d %H:%M:%S','now') where id=@id
  56. DeleteByID(id uint64) error
  57. }