-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tx support and rename package to mou
- Loading branch information
Showing
14 changed files
with
1,327 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* @Descripttion: | ||
* @version: | ||
* @Author: pengfei | ||
* @Date: 2021-07-01 08:31:06 | ||
*/ | ||
|
||
package mou | ||
|
||
import ( | ||
"context" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
type AggregateObject struct { | ||
ctx context.Context | ||
coll *mongo.Collection | ||
opts *options.AggregateOptions | ||
pipelines mongo.Pipeline | ||
} | ||
|
||
func (o *AggregateObject) DoCount() (int64, error) { | ||
o.pipelines = append(o.pipelines, bson.D{{Key: "$count", Value: "count"}}) | ||
var result []struct { | ||
Count int64 `bson:"count"` | ||
} | ||
err := o.Do(&result) | ||
var count int64 | ||
if len(result) > 0 { | ||
count = result[0].Count | ||
} | ||
return count, err | ||
} | ||
|
||
// func (o *AggregateObject) Group(stage interface{}) *AggregateObject { | ||
// o.pipelines = append(o.pipelines, bson.D{{Key: "$group", Value: stage}}) | ||
// return o | ||
// } | ||
|
||
// func (o *AggregateObject) GraphLookup(stage bson.D) *AggregateObject { | ||
// o.pipelines = append(o.pipelines, bson.D{{Key: "$graphLookup", Value: stage}}) | ||
// return o | ||
// } | ||
|
||
// func (o *AggregateObject) Skip(i int64) *AggregateObject { | ||
// o.pipelines = append(o.pipelines, bson.D{{Key: "$skip", Value: i}}) | ||
// return o | ||
// } | ||
// func (o *AggregateObject) Limit(i int64) *AggregateObject { | ||
// o.pipelines = append(o.pipelines, bson.D{{Key: "$limit", Value: i}}) | ||
// return o | ||
// } | ||
|
||
// func (o *AggregateObject) Sort(stage interface{}) *AggregateObject { | ||
// o.pipelines = append(o.pipelines, bson.D{{Key: "$sort", Value: stage}}) | ||
// return o | ||
// } | ||
|
||
// func (o *AggregateObject) AddFields(stage interface{}) *AggregateObject { | ||
// o.pipelines = append(o.pipelines, bson.D{{Key: "$addFields", Value: stage}}) | ||
// return o | ||
// } | ||
|
||
// func (o *AggregateObject) UnionWith(coll string, pipeline []interface{}) *AggregateObject { | ||
// o.pipelines = append(o.pipelines, bson.D{{Key: "$unionWith", Value: bson.D{{Key: "coll", Value: coll}, {Key: "pipeline", Value: pipeline}}}}) | ||
// return o | ||
// } | ||
|
||
func (o *AggregateObject) Pipelines(p mongo.Pipeline) *AggregateObject { | ||
o.pipelines = p | ||
return o | ||
} | ||
|
||
// func (o *AggregateObject) Clone() *AggregateObject { | ||
// pipelines := append([]bson.D{}, o.pipelines...) | ||
// return &AggregateObject{ctx: o.ctx, coll: o.coll, opts: o.opts, pipelines: pipelines} | ||
// } | ||
|
||
func (o *AggregateObject) Do(results interface{}) error { | ||
if o.pipelines == nil { | ||
o.pipelines = []bson.D{} | ||
} | ||
cursor, err := o.coll.Aggregate(o.ctx, o.pipelines, o.opts) | ||
if err != nil { | ||
return err | ||
} | ||
defer cursor.Close(o.ctx) | ||
return cursor.All(o.ctx, results) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* @Descripttion: | ||
* @version: | ||
* @Author: pengfei | ||
* @Date: 2021-10-13 08:31:06 | ||
*/ | ||
|
||
package mou | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
type PipelineBuilderType mongo.Pipeline | ||
type builder = PipelineBuilderType | ||
|
||
func PipelineBuilder() builder { | ||
return builder{} | ||
} | ||
|
||
func PipelineJsonBuilder(pipelineJSON []byte) (builder builder, err error) { | ||
err = bson.UnmarshalExtJSON([]byte(pipelineJSON), true, &builder) | ||
return | ||
} | ||
|
||
func (o builder) Match(stage interface{}) builder { | ||
o = append(o, bson.D{{Key: "$match", Value: stage}}) | ||
return o | ||
} | ||
|
||
func (o builder) Lookup(stage interface{}) builder { | ||
o = append(o, bson.D{{Key: "$lookup", Value: stage}}) | ||
return o | ||
} | ||
|
||
func (o builder) Project(stage interface{}) builder { | ||
o = append(o, bson.D{{Key: "$project", Value: stage}}) | ||
return o | ||
} | ||
|
||
func (o builder) Unwind(stage interface{}) builder { | ||
o = append(o, bson.D{{Key: "$unwind", Value: stage}}) | ||
return o | ||
} | ||
|
||
func (o builder) ReplaceRoot(stage interface{}) builder { | ||
o = append(o, bson.D{{Key: "$replaceRoot", Value: stage}}) | ||
return o | ||
} | ||
|
||
func (o builder) Unset(stage interface{}) builder { | ||
o = append(o, bson.D{{Key: "$unset", Value: stage}}) | ||
return o | ||
} | ||
|
||
func (o builder) Group(stage interface{}) builder { | ||
o = append(o, bson.D{{Key: "$group", Value: stage}}) | ||
return o | ||
} | ||
|
||
func (o builder) GraphLookup(stage bson.D) builder { | ||
o = append(o, bson.D{{Key: "$graphLookup", Value: stage}}) | ||
return o | ||
} | ||
|
||
func (o builder) Skip(i int64) builder { | ||
o = append(o, bson.D{{Key: "$skip", Value: i}}) | ||
return o | ||
} | ||
|
||
func (o builder) Limit(i int64) builder { | ||
o = append(o, bson.D{{Key: "$limit", Value: i}}) | ||
return o | ||
} | ||
|
||
func (o builder) Sort(stage interface{}) builder { | ||
o = append(o, bson.D{{Key: "$sort", Value: stage}}) | ||
return o | ||
} | ||
|
||
func (o builder) AddFields(stage interface{}) builder { | ||
o = append(o, bson.D{{Key: "$addFields", Value: stage}}) | ||
return o | ||
} | ||
|
||
func (o builder) UnionWith(coll string, pipeline []interface{}) builder { | ||
o = append(o, bson.D{{Key: "$unionWith", Value: bson.D{{Key: "coll", Value: coll}, {Key: "pipeline", Value: pipeline}}}}) | ||
return o | ||
} | ||
|
||
func (o builder) copy() builder { | ||
other := make(builder, len(o)) | ||
copy(other, o) | ||
return other | ||
} | ||
|
||
func (o builder) Append(builderOrPipeline interface{}) builder { | ||
switch value := builderOrPipeline.(type) { | ||
case PipelineBuilderType: | ||
o = append(o, value...) | ||
case mongo.Pipeline: | ||
o = append(o, value...) | ||
case []bson.D: | ||
o = append(o, value...) | ||
default: | ||
err := fmt.Errorf("unsupported type:%v", reflect.TypeOf(builderOrPipeline)) | ||
panic(err) | ||
} | ||
return o | ||
} | ||
|
||
func (o builder) ToPipeline() mongo.Pipeline { | ||
return mongo.Pipeline(o.copy()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* @Descripttion: | ||
* @version: | ||
* @Author: pengfei | ||
* @Date: 2021-07-01 08:31:06 | ||
*/ | ||
|
||
package mou | ||
|
||
import ( | ||
"context" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
type CountObject struct { | ||
ctx context.Context | ||
coll *mongo.Collection | ||
opts *options.CountOptions | ||
filter interface{} | ||
} | ||
|
||
func (o *CountObject) Where(filter interface{}) *CountObject { | ||
o.filter = filter | ||
return o | ||
} | ||
|
||
func (o *CountObject) Limit(i int64) *CountObject { | ||
o.opts.SetLimit(i) | ||
return o | ||
} | ||
|
||
func (o *CountObject) Skip(i int64) *CountObject { | ||
o.opts.SetSkip(i) | ||
return o | ||
} | ||
|
||
func (o *CountObject) Do() (int64, error) { | ||
if o.filter == nil { | ||
o.filter = bson.D{} | ||
} | ||
return o.coll.CountDocuments(o.ctx, o.filter, o.opts) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* @Descripttion: | ||
* @version: | ||
* @Author: pengfei | ||
* @Date: 2021-07-01 08:31:06 | ||
*/ | ||
package mou | ||
|
||
import ( | ||
"context" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
type DeleteOneObject struct { | ||
ctx context.Context | ||
coll *mongo.Collection | ||
filter interface{} | ||
} | ||
|
||
func (o *DeleteOneObject) Where(filter interface{}) *DeleteOneObject { | ||
o.filter = filter | ||
return o | ||
} | ||
|
||
func (o *DeleteOneObject) Do() (deleteCount int, err error) { | ||
rst, er := o.coll.DeleteOne(o.ctx, o.filter) | ||
err = er | ||
if rst != nil { | ||
deleteCount = int(rst.DeletedCount) | ||
} | ||
return | ||
} | ||
|
||
type DeleteManyObject struct { | ||
ctx context.Context | ||
coll *mongo.Collection | ||
filter interface{} | ||
} | ||
|
||
func (o *DeleteManyObject) Where(filter interface{}) *DeleteManyObject { | ||
o.filter = filter | ||
return o | ||
} | ||
|
||
func (o *DeleteManyObject) Do() (deleteCount int, err error) { | ||
if o.filter == nil { | ||
o.filter = bson.D{} | ||
} | ||
rst, er := o.coll.DeleteMany(o.ctx, o.filter) | ||
err = er | ||
if rst != nil { | ||
deleteCount = int(rst.DeletedCount) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* @Descripttion: | ||
* @version: | ||
* @Author: pengfei | ||
* @Date: 2021-07-01 08:31:06 | ||
*/ | ||
|
||
package mou | ||
|
||
import ( | ||
"context" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
type DistinctObject struct { | ||
ctx context.Context | ||
coll *mongo.Collection | ||
opts *options.DistinctOptions | ||
filter interface{} | ||
field string | ||
} | ||
|
||
func (o *DistinctObject) Where(filter interface{}) *DistinctObject { | ||
o.filter = filter | ||
return o | ||
} | ||
|
||
func (o *DistinctObject) Field(fieldName string) *DistinctObject { | ||
o.field = fieldName | ||
return o | ||
} | ||
|
||
func (o *DistinctObject) Do() ([]interface{}, error) { | ||
if o.filter == nil { | ||
o.filter = bson.D{} | ||
} | ||
return o.coll.Distinct(o.ctx, o.field, o.filter) | ||
} |
Oops, something went wrong.