Now all query filters can validate themselves using Valider interface

This commit is contained in:
2020-11-18 20:09:57 +01:00
parent 15bb53694f
commit 84ae518fe9
10 changed files with 67 additions and 18 deletions
+6 -2
View File
@@ -10,11 +10,15 @@ import (
// target is used only to get collection by tag so it'd be better to use nil ptr here
func (d *Database) Count(target interface{}, filters ...interface{}) (result int64, err error) {
composed, err := query.Compose(filters...)
if err != nil {
return
}
collection := d.GetCollectionOf(target)
opts := options.Count()
composed := query.Compose(filters...)
ctx := query.WithContext(d.Context(), composed)
opts := options.Count()
opts.Limit = composed.Limiter()
opts.Skip = composed.Skipper()
+7 -3
View File
@@ -35,10 +35,14 @@ func (d *Database) DeleteArray(target interface{}, filters ...interface{}) (err
zeroElem := reflect.Zero(targetSliceElemT)
targetLen := targetSliceV.Len()
composed, err := query.Compose(filters...)
if err != nil {
return
}
collection := d.GetCollectionOf(zeroElem.Interface())
opts := options.Delete()
ids := primitive.A{}
composed := query.Compose(filters...)
ctx := query.WithContext(d.Context(), composed)
for i := 0; i < targetLen; i++ {
@@ -61,7 +65,7 @@ func (d *Database) DeleteArray(target interface{}, filters ...interface{}) (err
composed.And(primitive.M{"_id": primitive.M{"$in": ids}})
result, err := collection.DeleteMany(ctx, composed.M(), opts)
result, err := collection.DeleteMany(ctx, composed.M(), options.Delete())
if err != nil {
return
}
+6 -2
View File
@@ -15,12 +15,16 @@ import (
// DeleteOne removes a document from a database and then returns it into target
func (d *Database) DeleteOne(target interface{}, filters ...interface{}) (err error) {
composed, err := query.Compose(filters...)
if err != nil {
return
}
collection := d.GetCollectionOf(target)
opts := &options.FindOneAndDeleteOptions{}
composed := query.Compose(filters...)
protected := base.GetProtection(target)
ctx := query.WithContext(d.Context(), composed)
opts := options.FindOneAndDelete()
opts.Sort = composed.Sorter()
if !reflect2.IsNil(target) {
+5 -1
View File
@@ -31,7 +31,11 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) (err er
panic(fmt.Errorf("target slice should contain ptrs"))
}
composed := query.Compose(filters...)
composed, err := query.Compose(filters...)
if err != nil {
return
}
zeroElem := reflect.Zero(targetSliceElemT)
hasPreloader, _ := composed.Preloader()
ctx := query.WithContext(d.Context(), composed)
+5 -1
View File
@@ -11,7 +11,11 @@ import (
// LoadOne function loads a first single target document by a query
func (d *Database) LoadOne(target interface{}, filters ...interface{}) (err error) {
composed := query.Compose(append(filters, query.Limit(1))...)
composed, err := query.Compose(append(filters, query.Limit(1))...)
if err != nil {
return
}
hasPreloader, _ := composed.Preloader()
ctx := query.WithContext(d.Context(), composed)
+6 -2
View File
@@ -10,12 +10,16 @@ import (
// LoadStream function loads documents one by one into a target channel
func (d *Database) LoadStream(target interface{}, filters ...interface{}) (loader mongox.StreamLoader, err error) {
var cursor *mongox.Cursor
composed, err := query.Compose(filters...)
if err != nil {
return
}
composed := query.Compose(filters...)
hasPreloader, _ := composed.Preloader()
ctx := query.WithContext(d.Context(), composed)
var cursor *mongox.Cursor
if hasPreloader {
cursor, err = d.createAggregateLoad(target, composed)
} else {
+6 -2
View File
@@ -13,15 +13,19 @@ import (
// SaveOne saves a single source document to the database
func (d *Database) SaveOne(source interface{}, filters ...interface{}) (err error) {
composed, err := query.Compose(filters...)
if err != nil {
return
}
collection := d.GetCollectionOf(source)
opts := options.FindOneAndReplace()
id := base.GetID(source)
protected := base.GetProtection(source)
composed := query.Compose(filters...)
ctx := query.WithContext(d.Context(), composed)
composed.And(primitive.M{"_id": id})
opts := options.FindOneAndReplace()
opts.SetUpsert(true)
opts.SetReturnDocument(options.After)
+6 -2
View File
@@ -13,13 +13,17 @@ import (
// UpdateOne updates a single document in the database and loads it into target
func (d *Database) UpdateOne(target interface{}, filters ...interface{}) (err error) {
composed, err := query.Compose(filters...)
if err != nil {
return
}
collection := d.GetCollectionOf(target)
opts := options.FindOneAndUpdate()
protected := base.GetProtection(target)
composed := query.Compose(filters...)
ctx := query.WithContext(d.Context(), composed)
updater := composed.Updater()
opts := options.FindOneAndUpdate()
opts.SetReturnDocument(options.After)
if protected != nil {