go-admin
go-admin copied to clipboard
[Proposal] support custom db connection interface inheritance
Description [describe your advice]
- 目前有快速实现后台数据的简单管理的需求
- go-admin 项目各方面都符合需求,但我这面要使用的 mysql 数据库连接基于内部的 ”服务发现“ 进行初始化,现使用数据库有问题
- 希望可以支持自定义实现 db Connection 接口,然后可以以类似插件的方式插入项目,进行使用(更为灵活)
Solution [if any solutions, describe here]
- db/connection/GetConnectionByDriver 逻辑重构,定义全局私有变量或自定义结构,支持 connection 种类的扩容(可以有一个 public method 追加)
- 这是现在项目中的
// GetConnectionByDriver return the Connection by given driver name.
func GetConnectionByDriver(driver string) Connection {
switch driver {
case "mysql":
return GetMysqlDB()
case "mssql":
return GetMssqlDB()
case "sqlite":
return GetSqliteDB()
case "postgresql":
return GetPostgresqlDB()
default:
panic("driver not found!")
}
}
- 建议可以类似这样去增加可以扩容的机制(这里简单写个例子)
var driverFuncMap map[string]func() Connection = map[string]func() Connection{
"mysql": GetMysqlDB,
"mssql": GetMssqlDB,
"sqlite": GetSqliteDB,
"postgresql": GetPostgresqlDB
}
func DriverFuncMapExtend(driver string, f func() Connection) {
driverFuncMap[driver] = f
}
func GetConnectionByDriver(driver string) Connection {
if f, ok := driverFuncMap[driver]; ok {
return f()
}
panic("driver not found!")
}