diff --git a/wmo/collection.go b/wmo/collection.go index 6167edd..1b28a74 100644 --- a/wmo/collection.go +++ b/wmo/collection.go @@ -2,9 +2,7 @@ package wmo import ( "context" - "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" ct "gogs.mikescher.com/BlackForestBytes/goext/cursortoken" "gogs.mikescher.com/BlackForestBytes/goext/langext" "reflect" @@ -42,210 +40,6 @@ func (c *Coll[TData]) Drop(ctx context.Context) error { return c.coll.Drop(ctx) } -func (c *Coll[TData]) FindOne(ctx context.Context, filter bson.M) (TData, error) { - var res TData - - err := c.coll.FindOne(ctx, filter).Decode(&res) - if err != nil { - return *new(TData), err - } - - return res, nil -} - -func (c *Coll[TData]) FindOneOpt(ctx context.Context, filter bson.M) (*TData, error) { - var res TData - - err := c.coll.FindOne(ctx, filter).Decode(&res) - if err == mongo.ErrNoDocuments { - return nil, nil - } - if err != nil { - return nil, err - } - - return &res, nil -} - -func (c *Coll[TData]) FindOneByID(ctx context.Context, id EntityID) (TData, error) { - var res TData - - err := c.coll.FindOne(ctx, bson.M{"_id": id}).Decode(&res) - if err != nil { - return *new(TData), err - } - - return res, nil -} - -func (c *Coll[TData]) FindOneOptByID(ctx context.Context, id EntityID) (*TData, error) { - var res TData - - err := c.coll.FindOne(ctx, bson.M{"_id": id}).Decode(&res) - if err == mongo.ErrNoDocuments { - return nil, nil - } - if err != nil { - return nil, err - } - - return &res, nil -} - -func (c *Coll[TData]) FindOneAndUpdate(ctx context.Context, filterQuery bson.M, updateQuery bson.M) (TData, error) { - var res TData - - err := c.coll.FindOneAndUpdate(ctx, filterQuery, updateQuery, options.FindOneAndUpdate().SetReturnDocument(options.After)).Decode(&res) - if err != nil { - return *new(TData), err - } - - return res, nil -} - -func (c *Coll[TData]) Find(ctx context.Context, filter bson.M, opts ...*options.FindOptions) ([]TData, error) { - cursor, err := c.coll.Find(ctx, filter, opts...) - if err != nil { - return nil, err - } - - res := make([]TData, 0, cursor.RemainingBatchLength()) - err = cursor.All(ctx, &res) - if err != nil { - return nil, err - } - - return res, nil -} - -func (c *Coll[TData]) Aggregate(ctx context.Context, pipeline mongo.Pipeline, opts ...*options.AggregateOptions) ([]TData, error) { - cursor, err := c.coll.Aggregate(ctx, pipeline, opts...) - if err != nil { - return nil, err - } - - res := make([]TData, 0, cursor.RemainingBatchLength()) - err = cursor.All(ctx, &res) - if err != nil { - return nil, err - } - - return res, nil -} - -func (c *Coll[TData]) ReplaceOne(ctx context.Context, id EntityID, value TData) error { - _, err := c.coll.UpdateOne(ctx, bson.M{"_id": id}, value) - if err != nil { - return err - } - - return nil -} - -func (c *Coll[TData]) UpdateOne(ctx context.Context, filterQuery bson.M, updateQuery bson.M) error { - _, err := c.coll.UpdateOne(ctx, filterQuery, updateQuery) - if err != nil { - return err - } - - return nil -} - -func (c *Coll[TData]) UpdateOneByID(ctx context.Context, id EntityID, updateQuery bson.M) error { - _, err := c.coll.UpdateOne(ctx, bson.M{"_id": id}, updateQuery) - if err != nil { - return err - } - - return nil -} - -func (c *Coll[TData]) DeleteOne(ctx context.Context, id EntityID) error { - _, err := c.coll.DeleteOne(ctx, bson.M{"_id": id}) - if err != nil { - return err - } - - return nil -} - -func (c *Coll[TData]) DeleteMany(ctx context.Context, filterQuery bson.M) (*mongo.DeleteResult, error) { - res, err := c.coll.DeleteMany(ctx, filterQuery) - if err != nil { - return nil, err - } - - return res, nil -} - -func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int, inTok ct.CursorToken) ([]TData, ct.CursorToken, error) { - if inTok.Mode == ct.CTMEnd { - return make([]TData, 0), ct.End(), nil - } - - pipeline := filter.FilterQuery() - - sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary := filter.Pagination() - - paginationPipeline, err := CreatePagination(c, inTok, sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary, pageSize) - if err != nil { - return nil, ct.CursorToken{}, err - } - - pipeline = append(pipeline, paginationPipeline...) - - cursor, err := c.coll.Aggregate(ctx, pipeline) - if err != nil { - return nil, ct.CursorToken{}, err - } - - entities := make([]TData, 0, cursor.RemainingBatchLength()+1) - for (pageSize == nil || len(entities) != *pageSize) && cursor.Next(ctx) { - var entry TData - err = cursor.Decode(&entry) - if err != nil { - return nil, ct.CursorToken{}, err - } - entities = append(entities, entry) - } - - if pageSize == nil || len(entities) <= *pageSize || !cursor.TryNext(ctx) { - return entities, ct.End(), nil - } - - last := entities[len(entities)-1] - - nextToken, _ := c.createToken(sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary, last, pageSize) - - return entities, nextToken, nil -} - -func (c *Coll[TData]) Count(ctx context.Context, filter ct.Filter) (int64, error) { - pipeline := filter.FilterQuery() - - pipeline = append(pipeline, bson.D{{Key: "$count", Value: "c"}}) - - cursor, err := c.coll.Aggregate(ctx, pipeline) - if err != nil { - return 0, err - } - - type res struct { - Count int64 `bson:"c"` - } - - if cursor.Next(ctx) { - v := res{} - err = cursor.Decode(&v) - if err != nil { - return 0, err - } - return v.Count, nil - } - - return 0, nil -} - func (c *Coll[TData]) createToken(fieldPrimary string, dirPrimary ct.SortDirection, fieldSecondary *string, dirSecondary *ct.SortDirection, lastEntity TData, pageSize *int) (ct.CursorToken, error) { valuePrimary, err := c.getFieldValueAsTokenString(lastEntity, fieldPrimary) diff --git a/wmo/queryAggregate.go b/wmo/queryAggregate.go new file mode 100644 index 0000000..9572c99 --- /dev/null +++ b/wmo/queryAggregate.go @@ -0,0 +1,22 @@ +package wmo + +import ( + "context" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +func (c *Coll[TData]) Aggregate(ctx context.Context, pipeline mongo.Pipeline, opts ...*options.AggregateOptions) ([]TData, error) { + cursor, err := c.coll.Aggregate(ctx, pipeline, opts...) + if err != nil { + return nil, err + } + + res := make([]TData, 0, cursor.RemainingBatchLength()) + err = cursor.All(ctx, &res) + if err != nil { + return nil, err + } + + return res, nil +} diff --git a/wmo/queryDelete.go b/wmo/queryDelete.go new file mode 100644 index 0000000..3714835 --- /dev/null +++ b/wmo/queryDelete.go @@ -0,0 +1,19 @@ +package wmo + +func (c *Coll[TData]) DeleteOne(ctx context.Context, id EntityID) error { + _, err := c.coll.DeleteOne(ctx, bson.M{"_id": id}) + if err != nil { + return err + } + + return nil +} + +func (c *Coll[TData]) DeleteMany(ctx context.Context, filterQuery bson.M) (*mongo.DeleteResult, error) { + res, err := c.coll.DeleteMany(ctx, filterQuery) + if err != nil { + return nil, err + } + + return res, nil +} diff --git a/wmo/queryFind.go b/wmo/queryFind.go new file mode 100644 index 0000000..0b92306 --- /dev/null +++ b/wmo/queryFind.go @@ -0,0 +1,73 @@ +package wmo + +import ( + "context" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +func (c *Coll[TData]) FindOne(ctx context.Context, filter bson.M) (TData, error) { + var res TData + + err := c.coll.FindOne(ctx, filter).Decode(&res) + if err != nil { + return *new(TData), err + } + + return res, nil +} + +func (c *Coll[TData]) FindOneOpt(ctx context.Context, filter bson.M) (*TData, error) { + var res TData + + err := c.coll.FindOne(ctx, filter).Decode(&res) + if err == mongo.ErrNoDocuments { + return nil, nil + } + if err != nil { + return nil, err + } + + return &res, nil +} + +func (c *Coll[TData]) FindOneByID(ctx context.Context, id EntityID) (TData, error) { + var res TData + + err := c.coll.FindOne(ctx, bson.M{"_id": id}).Decode(&res) + if err != nil { + return *new(TData), err + } + + return res, nil +} + +func (c *Coll[TData]) FindOneOptByID(ctx context.Context, id EntityID) (*TData, error) { + var res TData + + err := c.coll.FindOne(ctx, bson.M{"_id": id}).Decode(&res) + if err == mongo.ErrNoDocuments { + return nil, nil + } + if err != nil { + return nil, err + } + + return &res, nil +} + +func (c *Coll[TData]) Find(ctx context.Context, filter bson.M, opts ...*options.FindOptions) ([]TData, error) { + cursor, err := c.coll.Find(ctx, filter, opts...) + if err != nil { + return nil, err + } + + res := make([]TData, 0, cursor.RemainingBatchLength()) + err = cursor.All(ctx, &res) + if err != nil { + return nil, err + } + + return res, nil +} diff --git a/wmo/queryInsert.go b/wmo/queryInsert.go new file mode 100644 index 0000000..ab246c1 --- /dev/null +++ b/wmo/queryInsert.go @@ -0,0 +1,22 @@ +package wmo + +import ( + "context" + "go.mongodb.org/mongo-driver/bson" +) + +func (c *Coll[TData]) InsertOne(ctx context.Context, valueIn TData) (TData, error) { + insRes, err := c.coll.InsertOne(ctx, valueIn) + if err != nil { + return *new(TData), err + } + + var res TData + + err = c.coll.FindOne(ctx, bson.M{"_id": insRes.InsertedID}).Decode(&res) + if err != nil { + return *new(TData), err + } + + return res, nil +} diff --git a/wmo/queryList.go b/wmo/queryList.go new file mode 100644 index 0000000..56c4d33 --- /dev/null +++ b/wmo/queryList.go @@ -0,0 +1,75 @@ +package wmo + +import ( + "context" + "go.mongodb.org/mongo-driver/bson" + ct "gogs.mikescher.com/BlackForestBytes/goext/cursortoken" +) + +func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int, inTok ct.CursorToken) ([]TData, ct.CursorToken, error) { + if inTok.Mode == ct.CTMEnd { + return make([]TData, 0), ct.End(), nil + } + + pipeline := filter.FilterQuery() + + sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary := filter.Pagination() + + paginationPipeline, err := CreatePagination(c, inTok, sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary, pageSize) + if err != nil { + return nil, ct.CursorToken{}, err + } + + pipeline = append(pipeline, paginationPipeline...) + + cursor, err := c.coll.Aggregate(ctx, pipeline) + if err != nil { + return nil, ct.CursorToken{}, err + } + + entities := make([]TData, 0, cursor.RemainingBatchLength()+1) + for (pageSize == nil || len(entities) != *pageSize) && cursor.Next(ctx) { + var entry TData + err = cursor.Decode(&entry) + if err != nil { + return nil, ct.CursorToken{}, err + } + entities = append(entities, entry) + } + + if pageSize == nil || len(entities) <= *pageSize || !cursor.TryNext(ctx) { + return entities, ct.End(), nil + } + + last := entities[len(entities)-1] + + nextToken, _ := c.createToken(sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary, last, pageSize) + + return entities, nextToken, nil +} + +func (c *Coll[TData]) Count(ctx context.Context, filter ct.Filter) (int64, error) { + pipeline := filter.FilterQuery() + + pipeline = append(pipeline, bson.D{{Key: "$count", Value: "c"}}) + + cursor, err := c.coll.Aggregate(ctx, pipeline) + if err != nil { + return 0, err + } + + type res struct { + Count int64 `bson:"c"` + } + + if cursor.Next(ctx) { + v := res{} + err = cursor.Decode(&v) + if err != nil { + return 0, err + } + return v.Count, nil + } + + return 0, nil +} diff --git a/wmo/queryUpdate.go b/wmo/queryUpdate.go new file mode 100644 index 0000000..19dc8b6 --- /dev/null +++ b/wmo/queryUpdate.go @@ -0,0 +1,45 @@ +package wmo + +import ( + "context" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo/options" +) + +func (c *Coll[TData]) FindOneAndUpdate(ctx context.Context, filterQuery bson.M, updateQuery bson.M) (TData, error) { + var res TData + + err := c.coll.FindOneAndUpdate(ctx, filterQuery, updateQuery, options.FindOneAndUpdate().SetReturnDocument(options.After)).Decode(&res) + if err != nil { + return *new(TData), err + } + + return res, nil +} + +func (c *Coll[TData]) UpdateOne(ctx context.Context, filterQuery bson.M, updateQuery bson.M) error { + _, err := c.coll.UpdateOne(ctx, filterQuery, updateQuery) + if err != nil { + return err + } + + return nil +} + +func (c *Coll[TData]) UpdateOneByID(ctx context.Context, id EntityID, updateQuery bson.M) error { + _, err := c.coll.UpdateOne(ctx, bson.M{"_id": id}, updateQuery) + if err != nil { + return err + } + + return nil +} + +func (c *Coll[TData]) ReplaceOne(ctx context.Context, id EntityID, value TData) error { + _, err := c.coll.UpdateOne(ctx, bson.M{"_id": id}, value) + if err != nil { + return err + } + + return nil +}