-
Notifications
You must be signed in to change notification settings - Fork 674
Expand file tree
/
Copy pathmongo.go
More file actions
176 lines (139 loc) · 4.98 KB
/
Copy pathmongo.go
File metadata and controls
176 lines (139 loc) · 4.98 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package mongo
import (
"context"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
type Database interface {
Collection(string) Collection
Client() Client
}
type Collection interface {
FindOne(context.Context, interface{}) SingleResult
InsertOne(context.Context, interface{}) (interface{}, error)
InsertMany(context.Context, []interface{}) ([]interface{}, error)
DeleteOne(context.Context, interface{}) (int64, error)
Find(context.Context, interface{}, ...*options.FindOptions) (Cursor, error)
CountDocuments(context.Context, interface{}, ...*options.CountOptions) (int64, error)
Aggregate(context.Context, interface{}) (Cursor, error)
UpdateOne(context.Context, interface{}, interface{}, ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateMany(context.Context, interface{}, interface{}, ...*options.UpdateOptions) (*mongo.UpdateResult, error)
}
type SingleResult interface {
Decode(interface{}) error
}
type Cursor interface {
Close(context.Context) error
Next(context.Context) bool
Decode(interface{}) error
All(context.Context, interface{}) error
}
type Client interface {
Database(string) Database
Connect(context.Context) error
Disconnect(context.Context) error
StartSession() (mongo.Session, error)
UseSession(ctx context.Context, fn func(mongo.SessionContext) error) error
Ping(context.Context) error
}
type mongoClient struct {
cl *mongo.Client
}
type mongoDatabase struct {
db *mongo.Database
}
type mongoCollection struct {
coll *mongo.Collection
}
type mongoSingleResult struct {
sr *mongo.SingleResult
}
type mongoCursor struct {
mc *mongo.Cursor
}
type mongoSession struct {
mongo.Session
}
func NewClient(connection string) (Client, error) {
time.Local = time.UTC
c, err := mongo.NewClient(options.Client().ApplyURI(connection))
return &mongoClient{cl: c}, err
}
func (mc *mongoClient) Ping(ctx context.Context) error {
return mc.cl.Ping(ctx, readpref.Primary())
}
func (mc *mongoClient) Database(dbName string) Database {
db := mc.cl.Database(dbName)
return &mongoDatabase{db: db}
}
func (mc *mongoClient) UseSession(ctx context.Context, fn func(mongo.SessionContext) error) error {
return mc.cl.UseSession(ctx, fn)
}
func (mc *mongoClient) StartSession() (mongo.Session, error) {
session, err := mc.cl.StartSession()
return &mongoSession{session}, err
}
func (mc *mongoClient) Connect(ctx context.Context) error {
return mc.cl.Connect(ctx)
}
func (mc *mongoClient) Disconnect(ctx context.Context) error {
return mc.cl.Disconnect(ctx)
}
func (md *mongoDatabase) Collection(colName string) Collection {
collection := md.db.Collection(colName)
return &mongoCollection{coll: collection}
}
func (md *mongoDatabase) Client() Client {
client := md.db.Client()
return &mongoClient{cl: client}
}
func (mc *mongoCollection) FindOne(ctx context.Context, filter interface{}) SingleResult {
singleResult := mc.coll.FindOne(ctx, filter)
return &mongoSingleResult{sr: singleResult}
}
func (mc *mongoCollection) UpdateOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
return mc.coll.UpdateOne(ctx, filter, update, opts[:]...)
}
func (mc *mongoCollection) InsertOne(ctx context.Context, document interface{}) (interface{}, error) {
id, err := mc.coll.InsertOne(ctx, document)
return id.InsertedID, err
}
func (mc *mongoCollection) InsertMany(ctx context.Context, document []interface{}) ([]interface{}, error) {
res, err := mc.coll.InsertMany(ctx, document)
return res.InsertedIDs, err
}
func (mc *mongoCollection) DeleteOne(ctx context.Context, filter interface{}) (int64, error) {
count, err := mc.coll.DeleteOne(ctx, filter)
return count.DeletedCount, err
}
func (mc *mongoCollection) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (Cursor, error) {
findResult, err := mc.coll.Find(ctx, filter, opts...)
return &mongoCursor{mc: findResult}, err
}
func (mc *mongoCollection) Aggregate(ctx context.Context, pipeline interface{}) (Cursor, error) {
aggregateResult, err := mc.coll.Aggregate(ctx, pipeline)
return &mongoCursor{mc: aggregateResult}, err
}
func (mc *mongoCollection) UpdateMany(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
return mc.coll.UpdateMany(ctx, filter, update, opts[:]...)
}
func (mc *mongoCollection) CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error) {
return mc.coll.CountDocuments(ctx, filter, opts...)
}
func (sr *mongoSingleResult) Decode(v interface{}) error {
return sr.sr.Decode(v)
}
func (mr *mongoCursor) Close(ctx context.Context) error {
return mr.mc.Close(ctx)
}
func (mr *mongoCursor) Next(ctx context.Context) bool {
return mr.mc.Next(ctx)
}
func (mr *mongoCursor) Decode(v interface{}) error {
return mr.mc.Decode(v)
}
func (mr *mongoCursor) All(ctx context.Context, result interface{}) error {
return mr.mc.All(ctx, result)
}