Comments (3)
db := gormDb.Scopes(func(db *gorm.DB) *gorm.DB {
// 自定义表
return db.Table("users")
})
query, _ := gplus.NewQuery[User]()
// 传入Db
list, db := gplus.SelectList[User](query, gplus.Db(db))
if db.Error != nil {
return
}
for _, item := range list {
fmt.Println(item)
}
这样是否能满足你的要求
场景如下:通过租户或者ID首位分表(如user_1、user_2、user_3等),这种场景下只有前端的请求到来的时候,才知道应该具体去查询哪一张表,我在代码里没有看到关于表名的设置,目前我用的是GORM原生的Scopes方法去动态加载表名,可以为这个需求添加支持吗?
from gorm-plus.
db := gormDb.Scopes(func(db *gorm.DB) *gorm.DB { // 自定义表 return db.Table("users") }) query, _ := gplus.NewQuery[User]() // 传入Db list, db := gplus.SelectList[User](query, gplus.Db(db)) if db.Error != nil { return } for _, item := range list { fmt.Println(item) }
这样是否能满足你的要求
场景如下:通过租户或者ID首位分表(如user_1、user_2、user_3等),这种场景下只有前端的请求到来的时候,才知道应该具体去查询哪一张表,我在代码里没有看到关于表名的设置,目前我用的是GORM原生的Scopes方法去动态加载表名,可以为这个需求添加支持吗?
我目前就是这样子做的,但是这一部分的逻辑代码太多重复了。另外还有个,能否再添加一组QueryCond,类似mybatis plus的 boolean condition
那样,根据bool条件去决定是否要拼接sql。
from gorm-plus.
可以的,你感兴趣的话,也可以提交pr
from gorm-plus.
Related Issues (20)
- 事务使用报错 HOT 2
- 建议结构体改为嵌套 HOT 8
- 没有找到能执行DDL语句的接口 HOT 2
- 请教一个问题,这个gorm-plus事务是不是还没实现? HOT 1
- 分页JSON序列化字段可以改成小驼峰吗? HOT 1
- 分页查询只有单个列执行查询 在查询SelectCount总数会报错,因为gorm里count处理在mysql会有问题 HOT 3
- 能不能通过接口来做的更通用一些?做成Repository层 HOT 1
- 提供一种基于流式分页的分页函数 HOT 1
- 按条件判断拼接查询bug
- 当子条件的查询表达式为空时,构建sql拼接了多余的LeftBracket和RightBracket HOT 6
- 增加apply方法,用于拼接自定义sql字符串 HOT 4
- support multiple gorm HOT 4
- 为什么自动给我加了s HOT 2
- 关于连接池的问题 HOT 1
- 有异常panic HOT 3
- 关于join的查询,目前我没有找到该功能 HOT 2
- 构造查询条件的时候使用u.的时候报错,说没有这个字段 HOT 4
- 主键自定义字段名称和类型,根据主键查询数据报错。提示表没有id这个字段, HOT 2
- 不能进行多库绑定 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gorm-plus.