db.go 867 B

1234567891011121314151617181920212223242526272829303132333435
  1. package global
  2. import (
  3. "Panel_development/configs"
  4. "fmt"
  5. "gorm.io/driver/mysql"
  6. "gorm.io/gorm"
  7. "gorm.io/gorm/logger"
  8. )
  9. var (
  10. DBLink *gorm.DB
  11. )
  12. func SetupDBLink() error {
  13. var err error
  14. dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?%s",
  15. configs.Config.GetString("database.userName"),
  16. configs.Config.GetString("database.password"),
  17. configs.Config.GetString("database.host"),
  18. configs.Config.GetString("database.db"),
  19. configs.Config.GetString("database.otherParams"),
  20. )
  21. DBLink, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
  22. DisableForeignKeyConstraintWhenMigrating: true,
  23. Logger: logger.Default.LogMode(logger.Info),
  24. })
  25. if err != nil {
  26. return err
  27. }
  28. //DBLink.SingularTable(true)
  29. //DBLink.DB().SetMaxIdleConns(DatabaseSetting.MaxIdleConn)
  30. //DBLink.DB().SetMaxOpenConns(DatabaseSetting.MaxOpenConn)
  31. return nil
  32. }