Skip to content
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

gdb.DB: I want dongtai change current db,thanks! #4223

Open
pihc opened this issue Mar 25, 2025 · 13 comments
Open

gdb.DB: I want dongtai change current db,thanks! #4223

pihc opened this issue Mar 25, 2025 · 13 comments
Labels

Comments

@pihc
Copy link

pihc commented Mar 25, 2025

Is your feature request related to a problem?

Option Yes

Describe the solution you'd like

gdb.DB:我想动态的去切换当前的数据库,现在 gf gen dao 生成出来的
func (dao *SysDictDao) DB() gdb.DB { return g.DB(dao.group) } 数据库是固定死的,不支持动态的切换,我有几个同样的数据库,这里我想动态的去切换数据库(或者 sass系统也需要这个方案),希望这里可以加一个可以自定义获取 db的方法,谢谢!

Describe alternatives you've considered

1

Additional

No response

@pihc pihc added the feature label Mar 25, 2025
@pihc pihc changed the title os/gtime: issue title gdb.DB: I want dongtai change current db,thanks! Mar 25, 2025
@wwwfeng
Copy link
Contributor

wwwfeng commented Mar 25, 2025

我猜你想找这个

// DB sets/changes the db object for current operation.
func (m *Model) DB(db DB) *Model {
	model := m.getModel()
	model.db = db
	return model
}

https://github.com/gogf/gf/blob/master/database/gdb/gdb_model.go

@pihc
Copy link
Author

pihc commented Mar 25, 2025

我猜你想找这个

// DB sets/changes the db object for current operation.
func (m *Model) DB(db DB) *Model {
	model := m.getModel()
	model.db = db
	return model
}

https://github.com/gogf/gf/blob/master/database/gdb/gdb_model.go

我想全局改,而不是每次都是手动去调用这个 DB方法,请问除了修改 gf gen dao 的模板,还有其他法子吗?

@806572349
Copy link

你应该是saas的效果,a账号下面的系统,发送请求到a数据库,b账号下面的系统,发送请求时到b数据库吧?

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


You should have the effect of saas, the system under account a send request to database a, the system under account b, the system under account b, the system under account b, when sending request, right?

@pihc
Copy link
Author

pihc commented Mar 26, 2025

你应该是saas的效果,a账号下面的系统,发送请求到a数据库,b账号下面的系统,发送请求时到b数据库吧?

是的,我想要全局动态的去切数据库,你有什么好的解决方案吗?

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


You should have the effect of saas, the system under account a, send the request to database a, the system under account b, when sending the request to database b, right?

Yes, I want to cut the database dynamically globally. Do you have any good solutions?

@hailaz
Copy link
Member

hailaz commented Mar 26, 2025

@pihc
Copy link
Author

pihc commented Mar 26, 2025

看看符合不https://goframe.org/docs/core/gdb-sharding-schema

我想全局动态切换呢,每次手动去调用分库逻辑的话肯定会发生遗漏的情况,您给的这个页面最下面我看到有人也提出了这样的问题,现在生成的 gf gen dao 把数据库链接写死了,能不能开个自定义的口子,让用户可以去设置当前的db

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


See if it matches https://goframe.org/docs/core/gdb-sharding-schema

I want to switch globally dynamically. Every time I manually call the library logic, there will definitely be omissions. At the bottom of the page you gave me, I saw someone raise this question. The generated gf gen dao has written the database link to death. Can you open a custom hole so that users can set the current db

@hailaz
Copy link
Member

hailaz commented Mar 26, 2025

那就按评论,自己写dao就可以了,模板生成只是比较简单的一种方式。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Then just follow the comments and write it yourself. Template generation is just a relatively simple way.

@806572349
Copy link

  1. 可以写个中间件,判断是哪个租户的,并且设置到ctx 对应的db名称
  2. 然后可以修改gf gen dao 的模板

func (dao *GenTableDao) DBTenant(ctx context.Context) gdb.DB {
return g.DB(getDb(ctx))
}

func (dao *GenTableDao) Ctx(ctx context.Context) *gdb.Model {
return dao.DBTenant(ctx).Model(dao.table).Safe().Ctx(ctx).Hook(daomiddleware.DbHook())
}

getDb方法:获取中间件的的租户db名称

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


  1. You can write a middleware to determine which tenant it belongs to, and set the db name corresponding to ctx
  2. You can then modify the template of gf gen dao

func (dao *GenTableDao) DBTenant(ctx context.Context) gdb.DB {
return g.DB(getDb(ctx))
}

func (dao *GenTableDao) Ctx(ctx context.Context) *gdb.Model {
return dao.DBTenant(ctx).Model(dao.table).Safe().Ctx(ctx).Hook(daomiddleware.DbHook())
}

getDb method: get the middleware's tenant db name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants