```
feat(database): 新增数据库初始化函数 新增 NewDatabase 函数,支持根据驱动类型初始化 MySQL 或 Postgres 数据库连接。 该函数根据传入的驱动名称自动路由到对应的数据库连接创建逻辑,并提供错误处理。 ```
This commit is contained in:
27
with/databases.go
Normal file
27
with/databases.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package with
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/conf"
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/print"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Databases(cfg *conf.DBConf, db *gorm.DB, opts *types.SqlOptions) {
|
||||
if cfg == nil || len(cfg.Source) == 0 {
|
||||
panic("No Database Source Found !")
|
||||
}
|
||||
|
||||
// print inform.
|
||||
print.Info("[BSM - %s] Databases: %v", vars.ServiceKey, cfg)
|
||||
|
||||
var err error
|
||||
db, err = database.NewDatabase(cfg.Driver, cfg.Source, opts)
|
||||
if err != nil {
|
||||
print.Error("Database Init Failed !")
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user