1
0

model.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. UpstreamConfig{},
  50. }
  51. }
  52. func Use(tx *gorm.DB) {
  53. db = tx
  54. }
  55. func UseDB() *gorm.DB {
  56. return db
  57. }
  58. type Pagination struct {
  59. Total int64 `json:"total"`
  60. PerPage int `json:"per_page"`
  61. CurrentPage int `json:"current_page"`
  62. TotalPages int64 `json:"total_pages"`
  63. }
  64. type DataList struct {
  65. Data interface{} `json:"data"`
  66. Pagination Pagination `json:"pagination,omitempty"`
  67. }
  68. type Method interface {
  69. // FirstByID Where("id=@id")
  70. FirstByID(id uint64) (*gen.T, error)
  71. // DeleteByID update @@table set deleted_at=strftime('%Y-%m-%d %H:%M:%S','now') where id=@id
  72. DeleteByID(id uint64) error
  73. }