-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (69 loc) · 1.95 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package mongoutils
import (
"context"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
)
// NewPipe new mongo pipe builder
func NewPipe() MongoPipeline {
return new(mPipe)
}
// NewDoc new mongo doc builder
func NewDoc() MongoDoc {
return new(mDoc)
}
// NewMetaCounter new mongo meta counter
func NewMetaCounter() MetaCounter {
res := new(metaCounter)
res.Data = make(map[string][]meta)
return res
}
// NewMetaSetter new mongo meta setter
func NewMetaSetter() MetaSetter {
res := new(metaSetter)
res.Data = make(map[string][]metaV)
return res
}
// MongoOperationCtx create context for mongo db operations for 10 sec
func MongoOperationCtx() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.TODO(), 10*time.Second)
}
// ParseObjectID parse object id from string
func ParseObjectID(id string) *primitive.ObjectID {
if oId, err := primitive.ObjectIDFromHex(id); err == nil && !oId.IsZero() {
return &oId
}
return nil
}
// IsValidObjectId check if object id is valid and not zero
func IsValidObjectId(id *primitive.ObjectID) bool {
return id != nil && !id.IsZero()
}
// FindOption generate find option with sorts params
func FindOption(sort any, skip int64, limit int64) *options.FindOptions {
opt := new(options.FindOptions)
opt.SetAllowDiskUse(true)
opt.SetSkip(skip)
if limit > 0 {
opt.SetLimit(limit)
}
if sort != nil {
opt.SetSort(sort)
}
return opt
}
// AggregateOption generate aggregation options
func AggregateOption() *options.AggregateOptions {
return new(options.AggregateOptions).
SetAllowDiskUse(true)
}
// TxOption generate transaction option with majority write and snapshot read
func TxOption() *options.TransactionOptions {
return options.
Transaction().
SetWriteConcern(writeconcern.New(writeconcern.WMajority())).
SetReadConcern(readconcern.Snapshot())
}