34 lines
722 B
Go
34 lines
722 B
Go
package postgres
|
|
|
|
import (
|
|
"datart/config"
|
|
"fmt"
|
|
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var client *gorm.DB
|
|
|
|
func init() {
|
|
postgresConfig := config.Conf().PostgresConf("default")
|
|
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s",
|
|
postgresConfig.GetHost(), postgresConfig.GetUser(), postgresConfig.GetPassword(),
|
|
postgresConfig.GetDBName(), postgresConfig.GetPort(), postgresConfig.GetSSLMode(),
|
|
postgresConfig.GetTimeZone())
|
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
client = db
|
|
}
|
|
|
|
// close postgres default client
|
|
func Close() error {
|
|
db, err := client.DB()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return db.Close()
|
|
}
|