Compare commits

...

4 Commits

Author SHA1 Message Date
a4b5f33d15 v0.0.142 2023-06-07 11:28:07 +02:00
e89e2c18f2 v0.0.141 2023-06-07 10:56:11 +02:00
b16d5152c7 v0.0.140 2023-06-07 10:42:56 +02:00
5fb2f8a312 v0.0.139 2023-06-06 21:40:34 +02:00
5 changed files with 49 additions and 8 deletions

6
.idea/goext.iml generated
View File

@@ -1,6 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4"> <module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" /> <component name="Go" enabled="true">
<buildTags>
<option name="goVersion" value="1.19" />
</buildTags>
</component>
<component name="NewModuleRootManager"> <component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" /> <content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />

View File

@@ -6,5 +6,5 @@ import (
type Filter interface { type Filter interface {
FilterQuery() mongo.Pipeline FilterQuery() mongo.Pipeline
Pagination() (string, SortDirection, *string, *SortDirection) Pagination() (string, SortDirection, string, SortDirection)
} }

View File

@@ -1,5 +1,11 @@
package wmo package wmo
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
func (c *Coll[TData]) DeleteOne(ctx context.Context, id EntityID) error { func (c *Coll[TData]) DeleteOne(ctx context.Context, id EntityID) error {
_, err := c.coll.DeleteOne(ctx, bson.M{"_id": id}) _, err := c.coll.DeleteOne(ctx, bson.M{"_id": id})
if err != nil { if err != nil {

View File

@@ -13,7 +13,17 @@ func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int,
pipeline := filter.FilterQuery() pipeline := filter.FilterQuery()
sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary := filter.Pagination() pf1, pd1, pf2, pd2 := filter.Pagination()
sortPrimary := pf1
sortDirPrimary := pd1
sortSecondary := &pf2
sortDirSecondary := &pd2
if pf1 == pf2 {
sortSecondary = nil
sortDirSecondary = nil
}
paginationPipeline, err := CreatePagination(c, inTok, sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary, pageSize) paginationPipeline, err := CreatePagination(c, inTok, sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary, pageSize)
if err != nil { if err != nil {
@@ -48,6 +58,10 @@ func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int,
return entities, nextToken, nil return entities, nextToken, nil
} }
type countRes struct {
Count int64 `bson:"c"`
}
func (c *Coll[TData]) Count(ctx context.Context, filter ct.Filter) (int64, error) { func (c *Coll[TData]) Count(ctx context.Context, filter ct.Filter) (int64, error) {
pipeline := filter.FilterQuery() pipeline := filter.FilterQuery()
@@ -58,12 +72,8 @@ func (c *Coll[TData]) Count(ctx context.Context, filter ct.Filter) (int64, error
return 0, err return 0, err
} }
type res struct {
Count int64 `bson:"c"`
}
if cursor.Next(ctx) { if cursor.Next(ctx) {
v := res{} v := countRes{}
err = cursor.Decode(&v) err = cursor.Decode(&v)
if err != nil { if err != nil {
return 0, err return 0, err

View File

@@ -3,6 +3,7 @@ package wmo
import ( 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/options" "go.mongodb.org/mongo-driver/mongo/options"
) )
@@ -35,6 +36,15 @@ func (c *Coll[TData]) UpdateOneByID(ctx context.Context, id EntityID, updateQuer
return nil return nil
} }
func (c *Coll[TData]) UpdateMany(ctx context.Context, filterQuery bson.M, updateQuery bson.M) (*mongo.UpdateResult, error) {
res, err := c.coll.UpdateMany(ctx, filterQuery, updateQuery)
if err != nil {
return nil, err
}
return res, nil
}
func (c *Coll[TData]) ReplaceOne(ctx context.Context, id EntityID, value TData) error { func (c *Coll[TData]) ReplaceOne(ctx context.Context, id EntityID, value TData) error {
_, err := c.coll.UpdateOne(ctx, bson.M{"_id": id}, value) _, err := c.coll.UpdateOne(ctx, bson.M{"_id": id}, value)
if err != nil { if err != nil {
@@ -43,3 +53,14 @@ func (c *Coll[TData]) ReplaceOne(ctx context.Context, id EntityID, value TData)
return nil return nil
} }
func (c *Coll[TData]) FindOneAndReplace(ctx context.Context, id EntityID, value TData) (TData, error) {
var res TData
err := c.coll.FindOneAndUpdate(ctx, bson.M{"_id": id}, value, options.FindOneAndUpdate().SetReturnDocument(options.After)).Decode(&res)
if err != nil {
return *new(TData), err
}
return res, nil
}