Limiter and Sorter should return ready-to-use values

v2
Nikita Tokarchuk 6 years ago
parent bc595dd024
commit a1f52c8a08
  1. 11
      mongox/common/loadarray.go
  2. 11
      mongox/common/loadmany.go
  3. 6
      mongox/common/loadone.go
  4. 4
      mongox/common/saveone.go
  5. 13
      mongox/query/limit.go
  6. 19
      mongox/query/query.go

@ -33,15 +33,10 @@ func LoadArray(db *mongox.Database, target interface{}, composed *query.Query) e
dummy := reflect.Zero(targetSliceElemT)
collection := db.GetCollectionOf(dummy.Interface())
opts := &options.FindOptions{}
opts := options.Find()
if composed.Sorter() != nil {
opts.Sort = composed.Sorter().Sort()
}
if composed.Limiter() != nil {
limit := int64(composed.Limiter().Limit())
opts.Limit = &limit
}
opts.Sort = composed.Sorter()
opts.Limit = composed.Limiter()
result, err := collection.Find(db.Context(), composed.M(), opts)
if err != nil {

@ -44,15 +44,10 @@ func (l *ManyLoader) Close() error {
func LoadMany(db *mongox.Database, target interface{}, composed *query.Query) (*ManyLoader, error) {
collection := db.GetCollectionOf(target)
opts := &options.FindOptions{}
opts := options.Find()
if composed.Sorter() != nil {
opts.Sort = composed.Sorter().Sort()
}
if composed.Limiter() != nil {
limit := int64(composed.Limiter().Limit())
opts.Limit = &limit
}
opts.Sort = composed.Sorter()
opts.Limit = composed.Limiter()
cursor, err := collection.Find(db.Context(), composed.M(), opts)
if err != nil {

@ -12,11 +12,9 @@ import (
func LoadOne(db *mongox.Database, target interface{}, composed *query.Query) error {
collection := db.GetCollectionOf(target)
opts := &options.FindOneOptions{}
opts := options.FindOne()
if composed.Sorter() != nil {
opts.Sort = composed.Sorter().Sort()
}
opts.Sort = composed.Sorter()
result := collection.FindOne(db.Context(), composed.M(), opts)
if result.Err() != nil {

@ -2,9 +2,9 @@ package common
import (
"github.com/mainnika/mongox-go-driver/mongox"
"github.com/mainnika/mongox-go-driver/mongox/base"
"github.com/mainnika/mongox-go-driver/mongox/errors"
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/bson/primitive"
"github.com/mongodb/mongo-go-driver/mongo/options"
)
@ -12,7 +12,7 @@ import (
func SaveOne(db *mongox.Database, source interface{}) error {
collection := db.GetCollectionOf(source)
opts := &options.FindOneAndReplaceOptions{}
opts := options.FindOneAndReplace()
id := base.GetID(source)
opts.SetUpsert(true)

@ -2,16 +2,21 @@ package query
// Limiter is a filter to limit the result
type Limiter interface {
Limit() int
Limit() *int64
}
// Limit is a simple implementation of the Limiter filter
type Limit int
type Limit int64
var _ Limiter = Limit(0)
// Limit returns a limit
func (l Limit) Limit() int {
func (l Limit) Limit() *int64 {
return int(l)
lim := int64(l)
if lim <= 0 {
return nil
}
return &lim
}

@ -32,16 +32,24 @@ func (q *Query) And(elem bson.M) *Query {
return q
}
// Limiter is a limit function for a query
func (q *Query) Limiter() Limiter {
// Limiter returns limiter value or nil
func (q *Query) Limiter() *int64 {
return q.limiter
if q.limiter == nil {
return nil
}
return q.limiter.Limit()
}
// Sorter is a sort rule for a query
func (q *Query) Sorter() Sorter {
func (q *Query) Sorter() interface{} {
return q.sorter
if q.sorter == nil {
return nil
}
return q.sorter.Sort()
}
// Empty checks the query for any content
@ -55,6 +63,5 @@ func (q *Query) Empty() bool {
// M returns underlying query map
func (q *Query) M() bson.M {
return q.m
}

Loading…
Cancel
Save