File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ func (m Model[T]) QS(query string) Model[T] {
1818// Usage:
1919//
2020// UserModel.QSR(fq.Parse("filter[name]=John&sort[name]=asc&include=field1&select=field1")).ExecTT()
21- func (m Model [T ]) QSR (result fq.FilterQueryResult ) Model [T ] {
21+ func (m Model [T ]) QSR (result fq.Result ) Model [T ] {
2222 if len (result .Filters ) > 0 {
2323 m = m .Find (result .Filters )
2424 }
Original file line number Diff line number Diff line change 66 "strings"
77)
88
9- type FilterQueryResult struct {
9+ type Result struct {
1010 Filters bson.M // Primary filters which are usually evaluated as the first stage of an aggregation query
1111 SecondaryFilters bson.M // Secondary filters which are usually evaluated after any lookups
1212 Sorts bson.M // Fields to sort by, with 1 or 'asc' for ascending and -1 or 'desc' for descending
@@ -17,14 +17,19 @@ type FilterQueryResult struct {
1717 Limit int64 // The page size for pagination
1818}
1919
20+ // Type alias for the Result struct
21+ //
22+ // Deprecated: Use Result instead.
23+ type FilterQueryResult = Result
24+
2025// Parses the given query string into a Elemental FilterQueryResult.
21- func Parse (queryString string ) FilterQueryResult {
22- result := FilterQueryResult {}
26+ func Parse (qs string ) Result {
27+ result := Result {}
2328 result .Filters = bson.M {}
2429 result .SecondaryFilters = bson.M {}
2530 result .Sorts = bson.M {}
2631 result .Select = bson.M {}
27- queries := strings .Split (queryString , "&" )
32+ queries := strings .Split (qs , "&" )
2833 for _ , query := range queries {
2934 if query == "" {
3035 continue
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package fqm
33import (
44 "github.com/elcengine/elemental/plugins/filterquery"
55 "github.com/gofiber/fiber/v2"
6+ "github.com/samber/lo"
67)
78
89// NewGoFiber is a middleware for Fiber that parses the query string and stores the result in the context.
@@ -13,13 +14,19 @@ import (
1314// app := fiber.New()
1415// app.Use(fqm.NewGoFiber())
1516// app.Get("/users", func(ctx *fiber.Ctx) error {
16- // q := ctx.Locals(fqm.CtxKey).(fq.FilterQueryResult )
17+ // q := ctx.Locals(fqm.CtxKey).(fq.Result )
1718// users := UserModel.Find(q.Filters).Sort(q.Sorts).Select(q.Select).Populate(q.Include...).ExecTT()
1819// return ctx.JSON(users)
1920// })
20- func NewGoFiber () func (* fiber.Ctx ) error {
21+ func NewGoFiber (opts ... Options ) func (* fiber.Ctx ) error {
2122 return func (ctx * fiber.Ctx ) error {
22- ctx .Locals (CtxKey , fq .Parse (string (ctx .Request ().URI ().QueryString ())))
23+ result := fq .Parse (string (ctx .Request ().URI ().QueryString ()))
24+ if len (opts ) > 0 {
25+ if opts [0 ].DefaultLimit > 0 {
26+ result .Limit = lo .CoalesceOrEmpty (result .Limit , opts [0 ].DefaultLimit )
27+ }
28+ }
29+ ctx .Locals (CtxKey , result )
2330 return ctx .Next ()
2431 }
2532}
Original file line number Diff line number Diff line change 11package fqm
22
33const CtxKey = "elementalFilterQuery"
4+
5+ type Options struct {
6+ DefaultLimit int64 // Sets a default page size for the parsed query.
7+ }
You can’t perform that action at this time.
0 commit comments