Skip to content

Commit

Permalink
small correction
Browse files Browse the repository at this point in the history
  • Loading branch information
magellan committed Dec 9, 2022
1 parent 1a5ca58 commit 9f0ad0d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type UserModel struct {
Username string `gorm:"uniqueIndex" filter:"filterable"`
FullName string
Role string `filter:"filterable"`
CreatedAt time.Time `filter:"filterable"`
UpdatedAt time.Time `filter:"filterable"`
}
```
`param` tag in that case defines custom column name for the query param
Expand All @@ -47,4 +49,4 @@ Any filter combination can be used here `filter.PAGINATION|filter.ORDER_BY` e.g.

## TODO list
- [ ] Write tests for the lib with CI integration
- [ ] Add other filters, like > or !=
- [X] Add other filters, like > or !=
25 changes: 15 additions & 10 deletions gin-gorm-filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

package daos
package filter

import (
"net/url"
Expand Down Expand Up @@ -188,16 +188,21 @@ func getSeparator(key, value string) (string, string, string) {
// 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)
//
// 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`fitler` 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"`
// }
//
// 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 FilterByQuery(c *gin.Context, config int) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
var params queryParams
Expand Down

0 comments on commit 9f0ad0d

Please sign in to comment.