Limiter and Sorter should return ready-to-use values

This commit is contained in:
Nikita Tokarchuk
2018-12-26 23:17:46 +01:00
parent bc595dd024
commit a1f52c8a08
6 changed files with 32 additions and 32 deletions
+3 -8
View File
@@ -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 {
+3 -8
View File
@@ -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 {
+2 -4
View File
@@ -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 -2
View File
@@ -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)