From 8682fd98803e15dd287917d62cc2e4440e2605e7 Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Wed, 26 Dec 2018 23:18:06 +0100 Subject: [PATCH] Compose query inside common functions --- mongox/common/loadarray.go | 3 ++- mongox/common/loadmany.go | 3 ++- mongox/common/loadone.go | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/mongox/common/loadarray.go b/mongox/common/loadarray.go index ae424f1..f6c0751 100644 --- a/mongox/common/loadarray.go +++ b/mongox/common/loadarray.go @@ -10,7 +10,7 @@ import ( ) // LoadArray loads an array of documents from the database by query -func LoadArray(db *mongox.Database, target interface{}, composed *query.Query) error { +func LoadArray(db *mongox.Database, target interface{}, filters ...interface{}) error { targetV := reflect.ValueOf(target) targetT := targetV.Type() @@ -34,6 +34,7 @@ func LoadArray(db *mongox.Database, target interface{}, composed *query.Query) e dummy := reflect.Zero(targetSliceElemT) collection := db.GetCollectionOf(dummy.Interface()) opts := options.Find() + composed := query.Compose(filters...) opts.Sort = composed.Sorter() opts.Limit = composed.Limiter() diff --git a/mongox/common/loadmany.go b/mongox/common/loadmany.go index d9c9f1d..0803424 100644 --- a/mongox/common/loadmany.go +++ b/mongox/common/loadmany.go @@ -41,10 +41,11 @@ func (l *ManyLoader) Close() error { } // LoadMany function loads documents one by one into a target channel -func LoadMany(db *mongox.Database, target interface{}, composed *query.Query) (*ManyLoader, error) { +func LoadMany(db *mongox.Database, target interface{}, filters ...interface{}) (*ManyLoader, error) { collection := db.GetCollectionOf(target) opts := options.Find() + composed := query.Compose(filters...) opts.Sort = composed.Sorter() opts.Limit = composed.Limiter() diff --git a/mongox/common/loadone.go b/mongox/common/loadone.go index 8a3d0f7..d2eedb4 100644 --- a/mongox/common/loadone.go +++ b/mongox/common/loadone.go @@ -9,10 +9,11 @@ import ( ) // LoadOne function loads a first single target document by a query -func LoadOne(db *mongox.Database, target interface{}, composed *query.Query) error { +func LoadOne(db *mongox.Database, target interface{}, filters ...interface{}) error { collection := db.GetCollectionOf(target) opts := options.FindOne() + composed := query.Compose(filters...) opts.Sort = composed.Sorter()