Describe the feature
I want to add conditional query and It would be nice if we could have a .When() function which get a bool as first argument and the rest can be same as .Where() which the first argument indicates if the ORM should add that specific query or not.
Motivation
I want to add conditional query, Imagine I want to add a .where() clause which will take place only if there is a specific parameter like Active present in the request query params.
active, exists := QueryParams["active"]
db.Where("name = ?", name).When(exists, "active = ?", active).Where("something_else = ?", "random").First(&User)
Related Issues
--
Comment From: jinzhu
I am not particularly keen on adding a new API; rather, I would like to implement 'When' support based on the current Scopes format.
Example:
Let's say you want to dynamically apply a filter based on a condition using GORM's Scopes. Here's a simple example to illustrate how this can be achieved:
// UserActiveScope applies a filter dynamically based on the 'active' condition
func UserActiveScope(active bool) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Scopes(When(active, ActiveUserFilter))
}
}
// ActiveUserFilter defines the filter to be applied when the user is active
func ActiveUserFilter(db *gorm.DB) *gorm.DB {
return db.Where("active = ?", true)
}
// When determines whether to apply the given scope based on the condition
func When(condition bool, applyScope func(*gorm.DB) *gorm.DB) func(*gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
if condition {
return applyScope(db)
}
return db
}
}
In this example, UserActiveScope is a Scope that dynamically applies the ActiveUserFilter based on the passed active boolean value. The function When is used to decide whether to apply this filter. If active is true, then the ActiveUserFilter is applied to the query; otherwise, the query remains unchanged. This way, you can flexibly control the query conditions without the need to introduce a new API.