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) dummy := reflect.Zero(targetSliceElemT)
collection := db.GetCollectionOf(dummy.Interface()) collection := db.GetCollectionOf(dummy.Interface())
opts := &options.FindOptions{} opts := options.Find()
if composed.Sorter() != nil { opts.Sort = composed.Sorter()
opts.Sort = composed.Sorter().Sort() opts.Limit = composed.Limiter()
}
if composed.Limiter() != nil {
limit := int64(composed.Limiter().Limit())
opts.Limit = &limit
}
result, err := collection.Find(db.Context(), composed.M(), opts) result, err := collection.Find(db.Context(), composed.M(), opts)
if err != nil { if err != nil {

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

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

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

@ -2,16 +2,21 @@ package query
// Limiter is a filter to limit the result // Limiter is a filter to limit the result
type Limiter interface { type Limiter interface {
Limit() int Limit() *int64
} }
// Limit is a simple implementation of the Limiter filter // Limit is a simple implementation of the Limiter filter
type Limit int type Limit int64
var _ Limiter = Limit(0) var _ Limiter = Limit(0)
// Limit returns a limit // 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 return q
} }
// Limiter is a limit function for a query // Limiter returns limiter value or nil
func (q *Query) Limiter() Limiter { 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 // 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 // Empty checks the query for any content
@ -55,6 +63,5 @@ func (q *Query) Empty() bool {
// M returns underlying query map // M returns underlying query map
func (q *Query) M() bson.M { func (q *Query) M() bson.M {
return q.m return q.m
} }

Loading…
Cancel
Save