Compare commits

...

3 Commits

Author SHA1 Message Date
770f5c5c64 v0.0.309
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 1m17s
2023-11-09 10:17:29 +01:00
623c021689 v0.0.308
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 1m20s
2023-11-09 10:02:31 +01:00
afcc89bf9e v0.0.307
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 1m20s
2023-11-09 10:00:01 +01:00
5 changed files with 114 additions and 21 deletions

View File

@@ -46,6 +46,7 @@ var (
TypeCursorTokenDecode = NewType("CURSOR_TOKEN_DECODE", langext.Ptr(500)) TypeCursorTokenDecode = NewType("CURSOR_TOKEN_DECODE", langext.Ptr(500))
TypeMongoFilter = NewType("MONGO_FILTER", langext.Ptr(500)) TypeMongoFilter = NewType("MONGO_FILTER", langext.Ptr(500))
TypeMongoReflection = NewType("MONGO_REFLECTION", langext.Ptr(500)) TypeMongoReflection = NewType("MONGO_REFLECTION", langext.Ptr(500))
TypeMongoInvalidOpt = NewType("MONGO_INVALIDOPT", langext.Ptr(500))
TypeWrap = NewType("Wrap", nil) TypeWrap = NewType("Wrap", nil)

View File

@@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.306" const GoextVersion = "0.0.309"
const GoextVersionTimestamp = "2023-11-09T09:36:41+0100" const GoextVersionTimestamp = "2023-11-09T10:17:29+0100"

View File

@@ -1,29 +1,28 @@
package pagination package pagination
import ( import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
ct "gogs.mikescher.com/BlackForestBytes/goext/cursortoken"
) )
type Filter interface { type Filter interface {
FilterQuery() mongo.Pipeline FilterQuery() mongo.Pipeline
Pagination() (string, ct.SortDirection) Sort() bson.D
} }
type dynamicFilter struct { type dynamicFilter struct {
pipeline mongo.Pipeline pipeline mongo.Pipeline
sortField string sort bson.D
sortDir ct.SortDirection
} }
func (d dynamicFilter) FilterQuery() mongo.Pipeline { func (d dynamicFilter) FilterQuery() mongo.Pipeline {
return d.pipeline return d.pipeline
} }
func (d dynamicFilter) Pagination() (string, ct.SortDirection) { func (d dynamicFilter) Sort() bson.D {
return d.sortField, d.sortDir return d.sort
} }
func CreateFilter(pipeline mongo.Pipeline, sortField string, sortdir ct.SortDirection) Filter { func CreateFilter(pipeline mongo.Pipeline, sort bson.D) Filter {
return dynamicFilter{pipeline: pipeline, sortField: sortField, sortDir: sortdir} return dynamicFilter{pipeline: pipeline, sort: sort}
} }

View File

@@ -4,18 +4,52 @@ import (
"context" "context"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gogs.mikescher.com/BlackForestBytes/goext/exerr" "gogs.mikescher.com/BlackForestBytes/goext/exerr"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
) )
func (c *Coll[TData]) Find(ctx context.Context, filter bson.M) ([]TData, error) { func (c *Coll[TData]) Find(ctx context.Context, filter bson.M, opts ...*options.FindOptions) ([]TData, error) {
pipeline := mongo.Pipeline{} pipeline := mongo.Pipeline{}
pipeline = append(pipeline, bson.D{{Key: "$match", Value: filter}}) pipeline = append(pipeline, bson.D{{Key: "$match", Value: filter}})
for _, opt := range opts {
if opt != nil && opt.Sort != nil {
pipeline = append(pipeline, bson.D{{Key: "$sort", Value: opt.Sort}})
}
}
for _, opt := range opts {
if opt != nil && opt.Skip != nil {
pipeline = append(pipeline, bson.D{{Key: "$skip", Value: *opt.Skip}})
}
}
for _, opt := range opts {
if opt != nil && opt.Limit != nil {
pipeline = append(pipeline, bson.D{{Key: "$limit", Value: *opt.Limit}})
}
}
pipeline = langext.ArrConcat(pipeline, c.extraModPipeline) pipeline = langext.ArrConcat(pipeline, c.extraModPipeline)
cursor, err := c.coll.Aggregate(ctx, pipeline) for _, opt := range opts {
if opt != nil && opt.Projection != nil {
pipeline = append(pipeline, bson.D{{Key: "$project", Value: opt.Projection}})
}
}
convOpts := make([]*options.AggregateOptions, 0, len(opts))
for _, v := range opts {
vConv, err := convertFindOpt(v)
if err != nil {
return nil, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipeline).Str("collection", c.Name()).Build()
}
convOpts = append(convOpts, vConv)
}
cursor, err := c.coll.Aggregate(ctx, pipeline, convOpts...)
if err != nil { if err != nil {
return nil, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipeline).Str("collection", c.Name()).Build() return nil, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipeline).Str("collection", c.Name()).Build()
} }
@@ -27,3 +61,66 @@ func (c *Coll[TData]) Find(ctx context.Context, filter bson.M) ([]TData, error)
return res, nil return res, nil
} }
// converts FindOptions to AggregateOptions
func convertFindOpt(v *options.FindOptions) (*options.AggregateOptions, error) {
if v == nil {
return nil, nil
}
r := options.Aggregate()
if v.AllowDiskUse != nil {
r.SetAllowDiskUse(*v.AllowDiskUse)
}
if v.AllowPartialResults != nil {
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'AllowPartialResults' (cannot convert to AggregateOptions)").Build()
}
if v.BatchSize != nil {
r.SetBatchSize(*v.BatchSize)
}
if v.Collation != nil {
r.SetCollation(v.Collation)
}
if v.Comment != nil {
r.SetComment(*v.Comment)
}
if v.CursorType != nil {
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'CursorType' (cannot convert to AggregateOptions)").Build()
}
if v.Hint != nil {
r.SetHint(v.Hint)
}
if v.Max != nil {
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'Max' (cannot convert to AggregateOptions)").Build()
}
if v.MaxAwaitTime != nil {
r.SetMaxAwaitTime(*v.MaxAwaitTime)
}
if v.MaxTime != nil {
r.SetMaxTime(*v.MaxTime)
}
if v.Min != nil {
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'Min' (cannot convert to AggregateOptions)").Build()
}
if v.NoCursorTimeout != nil {
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'NoCursorTimeout' (cannot convert to AggregateOptions)").Build()
}
if v.OplogReplay != nil {
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'OplogReplay' (cannot convert to AggregateOptions)").Build()
}
if v.ReturnKey != nil {
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'ReturnKey' (cannot convert to AggregateOptions)").Build()
}
if v.ShowRecordID != nil {
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'ShowRecordID' (cannot convert to AggregateOptions)").Build()
}
if v.Snapshot != nil {
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'Snapshot' (cannot convert to AggregateOptions)").Build()
}
if v.Let != nil {
r.SetLet(v.Let)
}
return r, nil
}

View File

@@ -4,7 +4,6 @@ import (
"context" "context"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
ct "gogs.mikescher.com/BlackForestBytes/goext/cursortoken"
"gogs.mikescher.com/BlackForestBytes/goext/exerr" "gogs.mikescher.com/BlackForestBytes/goext/exerr"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
pag "gogs.mikescher.com/BlackForestBytes/goext/pagination" pag "gogs.mikescher.com/BlackForestBytes/goext/pagination"
@@ -21,18 +20,15 @@ func (c *Coll[TData]) Paginate(ctx context.Context, filter pag.Filter, page int,
pipelineSort := mongo.Pipeline{} pipelineSort := mongo.Pipeline{}
pipelineFilter := mongo.Pipeline{} pipelineFilter := mongo.Pipeline{}
pf1 := "_id" sort := bson.D{}
pd1 := ct.SortASC
if filter != nil { if filter != nil {
pipelineFilter = filter.FilterQuery() pipelineFilter = filter.FilterQuery()
pf1, pd1 = filter.Pagination() sort = filter.Sort()
} }
if pd1 == ct.SortASC { if len(sort) != 0 {
pipelineSort = append(pipelineSort, bson.D{{Key: "$sort", Value: bson.D{{Key: pf1, Value: +1}}}}) pipelineSort = append(pipelineSort, bson.D{{Key: "$sort", Value: sort}})
} else if pd1 == ct.SortDESC {
pipelineSort = append(pipelineSort, bson.D{{Key: "$sort", Value: bson.D{{Key: pf1, Value: -1}}}})
} }
pipelinePaginate := mongo.Pipeline{} pipelinePaginate := mongo.Pipeline{}