support pgsql model generation
This commit is contained in:
36
providers/database/postgres.go
Normal file
36
providers/database/postgres.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"atom/providers/config"
|
||||
"log"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
func NewPostgres(conf *config.PostgreSQL) (*gorm.DB, error) {
|
||||
dbConfig := postgres.Config{
|
||||
DSN: conf.DSN(), // DSN data source name
|
||||
}
|
||||
log.Println("PostgreSQL DSN: ", dbConfig.DSN)
|
||||
|
||||
gormConfig := gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
TablePrefix: conf.Prefix,
|
||||
SingularTable: conf.Singular,
|
||||
},
|
||||
DisableForeignKeyConstraintWhenMigrating: true,
|
||||
}
|
||||
|
||||
db, err := gorm.Open(postgres.New(dbConfig), &gormConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.SetMaxIdleConns(conf.MaxIdleConns)
|
||||
sqlDB.SetMaxOpenConns(conf.MaxOpenConns)
|
||||
|
||||
return db, err
|
||||
}
|
||||
Reference in New Issue
Block a user