This commit is contained in:
Mike Schwörer 2023-06-06 21:33:49 +02:00
parent 555096102a
commit 2ad820be8d
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
7 changed files with 256 additions and 206 deletions

View File

@ -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)

22
wmo/queryAggregate.go Normal file
View File

@ -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
}

19
wmo/queryDelete.go Normal file
View File

@ -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
}

73
wmo/queryFind.go Normal file
View File

@ -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
}

22
wmo/queryInsert.go Normal file
View File

@ -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
}

75
wmo/queryList.go Normal file
View File

@ -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
}

45
wmo/queryUpdate.go Normal file
View File

@ -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
}