You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

  1. package initialize
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/config"
  4. "github.com/flipped-aurora/gin-vue-admin/server/global"
  5. "github.com/flipped-aurora/gin-vue-admin/server/initialize/internal"
  6. "gorm.io/driver/postgres"
  7. "gorm.io/gorm"
  8. )
  9. // GormPgSql 初始化 Postgresql 数据库
  10. // Author [piexlmax](https://github.com/piexlmax)
  11. // Author [SliverHorn](https://github.com/SliverHorn)
  12. func GormPgSql() *gorm.DB {
  13. p := global.GVA_CONFIG.Pgsql
  14. if p.Dbname == "" {
  15. return nil
  16. }
  17. pgsqlConfig := postgres.Config{
  18. DSN: p.Dsn(), // DSN data source name
  19. PreferSimpleProtocol: false,
  20. }
  21. if db, err := gorm.Open(postgres.New(pgsqlConfig), internal.Gorm.Config()); err != nil {
  22. return nil
  23. } else {
  24. sqlDB, _ := db.DB()
  25. sqlDB.SetMaxIdleConns(p.MaxIdleConns)
  26. sqlDB.SetMaxOpenConns(p.MaxOpenConns)
  27. return db
  28. }
  29. }
  30. // GormPgSqlByConfig 初始化 Postgresql 数据库 通过参数
  31. func GormPgSqlByConfig(p config.DB) *gorm.DB {
  32. if p.Dbname == "" {
  33. return nil
  34. }
  35. pgsqlConfig := postgres.Config{
  36. DSN: p.Dsn(), // DSN data source name
  37. PreferSimpleProtocol: false,
  38. }
  39. if db, err := gorm.Open(postgres.New(pgsqlConfig), internal.Gorm.Config()); err != nil {
  40. panic(err)
  41. } else {
  42. sqlDB, _ := db.DB()
  43. sqlDB.SetMaxIdleConns(p.MaxIdleConns)
  44. sqlDB.SetMaxOpenConns(p.MaxOpenConns)
  45. return db
  46. }
  47. }