diff --git a/mongox/common/loadarray.go b/mongox/common/loadarray.go index f6c0751..fd5eb44 100644 --- a/mongox/common/loadarray.go +++ b/mongox/common/loadarray.go @@ -38,6 +38,7 @@ func LoadArray(db *mongox.Database, target interface{}, filters ...interface{}) opts.Sort = composed.Sorter() opts.Limit = composed.Limiter() + opts.Skip = composed.Skipper() result, err := collection.Find(db.Context(), composed.M(), opts) if err != nil { diff --git a/mongox/common/loadmany.go b/mongox/common/loadmany.go index 0803424..1434869 100644 --- a/mongox/common/loadmany.go +++ b/mongox/common/loadmany.go @@ -49,6 +49,7 @@ func LoadMany(db *mongox.Database, target interface{}, filters ...interface{}) ( opts.Sort = composed.Sorter() opts.Limit = composed.Limiter() + opts.Skip = composed.Skipper() cursor, err := collection.Find(db.Context(), composed.M(), opts) if err != nil { diff --git a/mongox/query/compose.go b/mongox/query/compose.go index 870e9cf..2639437 100644 --- a/mongox/query/compose.go +++ b/mongox/query/compose.go @@ -45,6 +45,8 @@ func applyLimits(q *Query, f interface{}) bool { q.limiter = f case Sorter: q.sorter = f + case Skipper: + q.skipper = f default: return false } diff --git a/mongox/query/query.go b/mongox/query/query.go index f27d336..c17c4b9 100644 --- a/mongox/query/query.go +++ b/mongox/query/query.go @@ -11,6 +11,7 @@ type Query struct { m bson.M limiter Limiter sorter Sorter + skipper Skipper } // And function pushes the elem query to the $and array of the query @@ -52,6 +53,16 @@ func (q *Query) Sorter() interface{} { return q.sorter.Sort() } +// Skipper is a skipper for a query +func (q *Query) Skipper() *int64 { + + if q.skipper == nil { + return nil + } + + return q.skipper.Skip() +} + // Empty checks the query for any content func (q *Query) Empty() bool { diff --git a/mongox/query/skip.go b/mongox/query/skip.go new file mode 100644 index 0000000..12ed094 --- /dev/null +++ b/mongox/query/skip.go @@ -0,0 +1,22 @@ +package query + +// Skipper is a filter to skip the result +type Skipper interface { + Skip() *int64 +} + +// Skip is a simple implementation of the Skipper filter +type Skip int64 + +var _ Skipper = Skip(0) + +// Skip returns a skip number +func (l Skip) Skip() *int64 { + + lim := int64(l) + if lim <= 0 { + return nil + } + + return &lim +}