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.
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"gofaster/internal/shared/config"
|
|
|
|
|
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewDB(cfg *config.DBConfig) (*gorm.DB, error) {
|
|
|
|
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable client_encoding=UTF8",
|
|
|
|
cfg.Host, cfg.User, cfg.Password, cfg.Name, cfg.Port)
|
|
|
|
|
|
|
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 自动迁移将在业务模块中单独处理
|
|
|
|
return db, nil
|
|
|
|
}
|