Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
555096102a
|
|||
d76d7b5cb9
|
@@ -92,6 +92,17 @@ func (c *Coll[TData]) FindOneOptByID(ctx context.Context, id EntityID) (*TData,
|
|||||||
return &res, nil
|
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) {
|
func (c *Coll[TData]) Find(ctx context.Context, filter bson.M, opts ...*options.FindOptions) ([]TData, error) {
|
||||||
cursor, err := c.coll.Find(ctx, filter, opts...)
|
cursor, err := c.coll.Find(ctx, filter, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -149,22 +160,6 @@ func (c *Coll[TData]) UpdateOneByID(ctx context.Context, id EntityID, updateQuer
|
|||||||
return nil
|
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 {
|
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 {
|
||||||
@@ -225,6 +220,32 @@ func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int,
|
|||||||
return entities, nextToken, nil
|
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) {
|
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)
|
valuePrimary, err := c.getFieldValueAsTokenString(lastEntity, fieldPrimary)
|
||||||
|
Reference in New Issue
Block a user