gorm.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package initialize
  2. import (
  3. "fmt"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  5. "go.uber.org/zap"
  6. "gorm.io/driver/mysql"
  7. "gorm.io/gorm"
  8. "gorm.io/gorm/logger"
  9. "gorm.io/gorm/schema"
  10. "lc-fangdaosha/global"
  11. "lc-fangdaosha/model/app"
  12. "log"
  13. "os"
  14. "time"
  15. )
  16. func Gorm() *gorm.DB {
  17. m := global.Config.Mysql
  18. fmt.Println("mysql配置:", m)
  19. if m.Dbname == "" {
  20. return nil
  21. }
  22. mysqlConfig := mysql.Config{
  23. DSN: m.Dsn(), // DSN data source name
  24. DefaultStringSize: 191, // string 类型字段的默认长度
  25. SkipInitializeWithVersion: false, // 根据版本自动配置
  26. }
  27. if db, err := gorm.Open(mysql.New(mysqlConfig), config(m.Prefix, m.Singular)); err != nil {
  28. log.Fatalln("29:", err)
  29. return nil
  30. } else {
  31. db.InstanceSet("gorm:table_options", "ENGINE="+m.Engine)
  32. sqlDB, _ := db.DB()
  33. sqlDB.SetMaxIdleConns(m.MaxIdleConns)
  34. sqlDB.SetMaxOpenConns(m.MaxOpenConns)
  35. return db
  36. }
  37. }
  38. func RegisterTables() {
  39. db := global.Db
  40. err := db.AutoMigrate(
  41. system.SysApi{},
  42. system.SysUser{},
  43. system.SysBaseMenu{},
  44. system.JwtBlacklist{},
  45. system.SysAuthority{},
  46. //system.SysDictionary{},
  47. system.SysOperationRecord{},
  48. //system.SysAutoCodeHistory{},
  49. system.SysDictionaryDetail{},
  50. //system.SysBaseMenuParameter{},
  51. system.SysBaseMenuBtn{},
  52. system.SysAuthorityBtn{},
  53. //system.SysAutoCode{},
  54. //system.SysChatGptOption{},
  55. //example.ExaFile{},
  56. //example.ExaCustomer{},
  57. //example.ExaFileChunk{},
  58. //example.ExaFileUploadAndDownload{},
  59. app.Camera{}, app.Picture{}, app.PictureData{}, app.Gateway{}, app.Event{},
  60. )
  61. if err != nil {
  62. global.GVA_LOG.Error("register table failed", zap.Error(err))
  63. os.Exit(0)
  64. }
  65. global.GVA_LOG.Info("register table success")
  66. }
  67. type DBBASE interface {
  68. GetLogMode() string
  69. }
  70. func config(prefix string, singular bool) *gorm.Config {
  71. config := &gorm.Config{
  72. NamingStrategy: schema.NamingStrategy{
  73. TablePrefix: prefix,
  74. SingularTable: singular,
  75. },
  76. DisableForeignKeyConstraintWhenMigrating: true,
  77. }
  78. _default := logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{
  79. SlowThreshold: 200 * time.Millisecond,
  80. LogLevel: logger.Warn,
  81. Colorful: true,
  82. })
  83. var logMode DBBASE
  84. logMode = &global.Config.Mysql
  85. switch logMode.GetLogMode() {
  86. case "silent", "Silent":
  87. config.Logger = _default.LogMode(logger.Silent)
  88. case "error", "Error":
  89. config.Logger = _default.LogMode(logger.Error)
  90. case "warn", "Warn":
  91. config.Logger = _default.LogMode(logger.Warn)
  92. case "info", "Info":
  93. config.Logger = _default.LogMode(logger.Info)
  94. default:
  95. config.Logger = _default.LogMode(logger.Info)
  96. }
  97. return config
  98. }