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.
23 lines
492 B
23 lines
492 B
1 month ago
|
package database
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"gofaster/internal/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 TimeZone=Asia/Shanghai",
|
||
|
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
|
||
|
}
|