2023-06-06 21:33:49 +02:00
|
|
|
package wmo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
2023-06-07 10:56:11 +02:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
2023-06-06 21:33:49 +02:00
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
2023-08-21 15:08:35 +02:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
|
2023-06-06 21:33:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Coll[TData]) FindOneAndUpdate(ctx context.Context, filterQuery bson.M, updateQuery bson.M) (TData, error) {
|
2023-06-10 16:22:14 +02:00
|
|
|
mongoRes := c.coll.FindOneAndUpdate(ctx, filterQuery, updateQuery, options.FindOneAndUpdate().SetReturnDocument(options.After))
|
2023-08-21 15:08:35 +02:00
|
|
|
if err := mongoRes.Err(); err != nil {
|
|
|
|
return *new(TData), exerr.Wrap(err, "mongo-query[find-one-and-update] failed").
|
|
|
|
Str("collection", c.Name()).
|
|
|
|
Any("filterQuery", filterQuery).
|
|
|
|
Any("updateQuery", updateQuery).
|
|
|
|
Build()
|
|
|
|
}
|
2023-06-06 21:33:49 +02:00
|
|
|
|
2023-11-09 09:26:46 +01:00
|
|
|
return c.decodeSingleOrRequery(ctx, mongoRes)
|
2023-06-06 21:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-08-21 15:08:35 +02:00
|
|
|
return exerr.Wrap(err, "mongo-query[update-one] failed").
|
|
|
|
Str("collection", c.Name()).
|
|
|
|
Any("filterQuery", filterQuery).
|
|
|
|
Any("updateQuery", updateQuery).
|
|
|
|
Build()
|
2023-06-06 21:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-08-21 15:08:35 +02:00
|
|
|
return exerr.Wrap(err, "mongo-query[update-one-by-id] failed").
|
|
|
|
Str("collection", c.Name()).
|
|
|
|
Id("id", id).
|
|
|
|
Any("updateQuery", updateQuery).
|
|
|
|
Build()
|
2023-06-06 21:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-07 10:56:11 +02:00
|
|
|
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 {
|
2023-08-21 15:08:35 +02:00
|
|
|
return nil, exerr.Wrap(err, "mongo-query[update-many] failed").
|
|
|
|
Str("collection", c.Name()).
|
|
|
|
Any("filterQuery", filterQuery).
|
|
|
|
Any("updateQuery", updateQuery).
|
|
|
|
Build()
|
2023-06-07 10:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-06-08 16:24:15 +02:00
|
|
|
func (c *Coll[TData]) ReplaceOne(ctx context.Context, filterQuery bson.M, value TData) error {
|
2024-05-12 16:45:45 +02:00
|
|
|
for _, hook := range c.marshalHooks {
|
|
|
|
value = hook(value)
|
|
|
|
}
|
|
|
|
|
2023-06-08 16:24:15 +02:00
|
|
|
_, err := c.coll.UpdateOne(ctx, filterQuery, bson.M{"$set": value})
|
2023-06-06 21:33:49 +02:00
|
|
|
if err != nil {
|
2023-08-21 15:08:35 +02:00
|
|
|
return exerr.Wrap(err, "mongo-query[replace-one] failed").
|
|
|
|
Str("collection", c.Name()).
|
|
|
|
Any("filterQuery", filterQuery).
|
|
|
|
Build()
|
2023-06-06 21:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-06 21:40:34 +02:00
|
|
|
|
2023-06-08 16:24:15 +02:00
|
|
|
func (c *Coll[TData]) FindOneAndReplace(ctx context.Context, filterQuery bson.M, value TData) (TData, error) {
|
2024-05-12 16:45:45 +02:00
|
|
|
for _, hook := range c.marshalHooks {
|
|
|
|
value = hook(value)
|
|
|
|
}
|
|
|
|
|
2023-09-26 14:40:56 +02:00
|
|
|
mongoRes := c.coll.FindOneAndReplace(ctx, filterQuery, value, options.FindOneAndReplace().SetReturnDocument(options.After))
|
2023-08-21 15:08:35 +02:00
|
|
|
if err := mongoRes.Err(); err != nil {
|
|
|
|
return *new(TData), exerr.Wrap(err, "mongo-query[find-one-and-update] failed").
|
|
|
|
Str("collection", c.Name()).
|
|
|
|
Any("filterQuery", filterQuery).
|
|
|
|
Build()
|
|
|
|
}
|
2023-06-06 21:40:34 +02:00
|
|
|
|
2023-11-09 09:26:46 +01:00
|
|
|
return c.decodeSingleOrRequery(ctx, mongoRes)
|
2023-06-06 21:40:34 +02:00
|
|
|
}
|