This commit is contained in:
Mike Schwörer 2023-06-06 21:26:12 +02:00
parent 6622c9003d
commit d76d7b5cb9
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF

View File

@ -92,6 +92,17 @@ func (c *Coll[TData]) FindOneOptByID(ctx context.Context, id EntityID) (*TData,
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 {
@ -149,22 +160,6 @@ func (c *Coll[TData]) UpdateOneByID(ctx context.Context, id EntityID, updateQuer
return nil
}
func (c *Coll[TData]) UpdateOneAndReturn(ctx context.Context, id EntityID, updateQuery bson.M) (TData, error) {
_, err := c.coll.UpdateOne(ctx, bson.M{"_id": id}, updateQuery)
if err != nil {
return *new(TData), err
}
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]) DeleteOne(ctx context.Context, id EntityID) error {
_, err := c.coll.DeleteOne(ctx, bson.M{"_id": id})
if err != nil {