gorm
gorm copied to clipboard
gorm Scopes实现根据传入结构体的tag去实现拼接where条件sql
Describe the feature
比如业务写dao层的时候,经常函数传进来的是ctx和结构体参数
type A struct {
A string filter:"col:a;opr:like"
B string filter:"col:b;opr:eq"
C int
}
是不是可以根据结构体的tag标签定义数据库字段值和判断条件去实现 我看使用Scopes这个api可以实现
实现以后大概长这样
db.Model(&A{}).Scopes(func (db *gorm.DB) *gorm.DB{
scope := xxx() # 这里做一些逻辑
return db.Where(scope)
})
Motivation
可以作为一个特性,方便curd happy