Documentation
¶
Index ¶
- Constants
- Variables
- func FilterByQuery(c *gin.Context, config int) func(db *gorm.DB) *gorm.DB
- func FilterByQueryWithCustomDefault(c *gin.Context, config int, params QueryParams) func(db *gorm.DB) *gorm.DB
- func Paginate(c *gin.Context, db *gorm.DB, params QueryParams) *gorm.DB
- func ToSnakeCase(str string) string
- type QueryParams
Constants ¶
View Source
const ( //SEARCH = 1 // NOT IMPLEMENTED // Filter response with LIKE query "search={search_phrase}" FILTER = 2 // Filter response by column name values "{column_name}={value}" PAGINATE = 4 // Paginate response with page and page_size ORDER_BY = 8 // Order response by column name ALL = 15 // Equivalent to SEARCH|FILTER|PAGINATE|ORDER_BY )
Variables ¶
View Source
var Separators = []string{
gte,
gt,
lte,
lt,
neq,
eq,
}
Functions ¶
func FilterByQuery ¶
Filter DB request with query parameters. Note: Don't forget to initialize DB Model first, otherwise filter and search won't work Example:
db.Model(&UserModel).Scope(filter.FilterByQuery(ctx, filter.ALL)).Find(&users)
Or if only pagination and order is needed:
db.Model(&UserModel).Scope(filter.FilterByQuery(ctx, filter.PAGINATION|filter.ORDER_BY)).Find(&users)
And models should have appropriate`filter` tags:
type User struct { gorm.Model Username string `gorm:"uniqueIndex" filter:"param:login;searchable;filterable"` // `param` defines custom column name for the query param FullName string `filter:"searchable"` }
func FilterByQueryWithCustomDefault ¶ added in v1.1.0
func Paginate ¶ added in v1.1.0
Use this function to paginate custom query. Example :
db := vd.db.Model(&Customer{}).
Select("id, COUNT(orders.id)"). Joins("JOIN orders ON orders.customer_id = customers.id"). Where("orders.type = ?", orderSold)
err = Paginate(c, db, params). Scan(&customType).Error
if err != nil { return ret, err }
func ToSnakeCase ¶ added in v1.0.0
Types ¶
type QueryParams ¶ added in v1.1.0
Click to show internal directories.
Click to hide internal directories.