-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to configure when dealing with multiple DB instances? Are there any practices?[Question] #3152
Comments
If you want to operate multiple DBs, how can you add them more conveniently, through different configurations, or by implementing a configuration list? If it is a configuration list, how to use it, how about DB.Use("xxx") compatible? |
🤖 你好,@JackChanw!我是Dosu,一个能帮你解决问题,回答问题,甚至成为贡献者的机器人。虽然我是一个机器人,但我还是会尽我所能来帮助你,同时等待人类维护者的回答。我正在查看你的问题,并将在不久的将来给你一个完整的答案。🐸 |
🤖 Hello @JackChanw! I'm Dosu, a bot that can help you solve problems, answer questions, and even become a contributor. Even though I'm a robot, I'll try my best to help you while waiting for answers from the human maintainers. I'm looking at your question and will give you a complete answer in the near future. 🐸 |
给你个参考,我是这么做的:
|
To give you a reference, this is what I did:
"time" "gorm.io/gorm" "example/internal/conf"
*gorm.DB One *gorm.DB
def, err := gorm.New(....) one, err := gorm.New(...) return &DB{
|
https://github.com/nextmicro/next-component/blob/main/gorm/option.go //{
// "mysql": {
// "default": {
// "master": 'user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local',
// "slaves": [
// 'user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local',
// 'user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local'
// ]
// }
// }
//}
func (c *Component) Instance(name ...string) *gorm.DB {
group := defaultName
if len(name) > 0 && name[0] != "" {
group = name[0]
}
value, ok := c.clients.Load(group)
if !ok {
panic("not found instance" + group)
}
return value.(*gorm.DB)
} |
Based on the information provided, your approach to handling multiple database instances seems to be on the right track. You've encapsulated the database instances within a struct ( However, there are a few things you might want to consider:
Here's how you might modify your code to incorporate these suggestions: package data
import (
"errors"
"time"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"example/internal/conf"
)
type DB struct {
*gorm.DB
One *gorm.DB
Two *gorm.DB
}
type DBHandler interface {
Where(...interface{}) *gorm.DB
}
func NewDB(def, one, two *gorm.DB) (*DB, error) {
if def == nil || one == nil || two == nil {
return nil, errors.New("nil database instance")
}
return &DB{
DB: def,
One: one,
Two: two,
}, nil
} In your code where you use the var dbHandler data.DBHandler = db
dbHandler.Where(...)
dbHandler.One.Where(...)
dbHandler.Two.Where(...) This way, you're programming against an interface ( Remember, these are just suggestions and the actual implementation might vary based on your specific use case and requirements. I hope this helps! If you have any more questions, feel free to ask. Sources
|
如果要对多个DB进行操作的话,如何比较方便的添加进去,是通过不同的配置,还是实现一个配置列表,如果是配置列表,在使用上怎么用呢,DB.Use("xxx") 怎么兼容?
The text was updated successfully, but these errors were encountered: