-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.go
277 lines (210 loc) · 7.78 KB
/
operations.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package mongodb
import (
"context"
"errors"
"fmt"
"strings"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Count will return a count of the total number documents in the provided collection determinted by the bson filter.
//
// A value of -1 will be returned if any error occurs during this operation.
func (op *Operator) Count(collection string, filter interface{}, opts ...*options.CountOptions) (int64, error) {
if filter == nil {
return -1, ErrNilFilter
}
c, err := op.getCollection(collection)
if err != nil {
return -1, fmt.Errorf("%s, %w", ErrCount.Error(), err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(op.config.TimeoutMS)*time.Millisecond)
defer cancel()
count, err := c.CountDocuments(ctx, filter, opts...)
if err != nil {
return -1, fmt.Errorf("%s, %w", ErrCount.Error(), err)
}
return count, nil
}
// DeleteMany will delete multiple documents in the provided collection determined by the bson filter.
//
// A value of -1 will be returned if any error occurs during this operation.
func (op *Operator) DeleteMany(collection string, filter interface{}, opts ...*options.DeleteOptions) (int64, error) {
if filter == nil {
return -1, ErrNilFilter
}
col, err := op.getCollection(collection)
if err != nil {
return -1, fmt.Errorf("%s; %w", ErrDeleteMany.Error(), err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(op.config.TimeoutMS)*time.Millisecond)
defer cancel()
res, err := col.DeleteMany(ctx, filter, opts...)
if err != nil {
return -1, fmt.Errorf("%s, %w", ErrDeleteMany.Error(), err)
}
return res.DeletedCount, nil
}
// DeleteOne will delete at most one document from the provided collection using the provided bson filter.
//
// The deleted document will be decoded into the provided target, which should be a pointer.
func (op *Operator) DeleteOne(collection string, filter, target interface{}, opts ...*options.FindOneAndDeleteOptions) error {
if filter == nil {
return ErrNilFilter
}
if target == nil {
return ErrNilTarget
}
col, err := op.getCollection(collection)
if err != nil {
return fmt.Errorf("%s; %w", ErrDeleteOne.Error(), err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(op.config.TimeoutMS)*time.Millisecond)
defer cancel()
res := col.FindOneAndDelete(ctx, filter, opts...)
if res.Err() != nil && !errors.Is(res.Err(), mongo.ErrNoDocuments) {
return fmt.Errorf("%s; %w", ErrDeleteOne, res.Err())
}
if err := res.Decode(target); err != nil {
return fmt.Errorf("failed to decode deleted document to target interface; %w", err)
}
return nil
}
// FindMany will return a slice of documents from the provided collection determined by the bson filter.
//
// The target must be a non-nil pointer to a slice of desired type.
func (op *Operator) FindMany(collection string, filter, target interface{}, opts ...*options.FindOptions) error {
if filter == nil {
return ErrNilFilter
}
if target == nil {
return ErrNilTarget
}
c, err := op.getCollection(collection)
if err != nil {
return fmt.Errorf("%s, %w", ErrFindMany.Error(), err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(op.config.TimeoutMS)*time.Millisecond)
defer cancel()
cur, err := c.Find(ctx, filter, opts...)
if err != nil {
return fmt.Errorf("%s, %w", ErrFindMany.Error(), err)
}
if err := cur.All(context.Background(), target); err != nil {
return fmt.Errorf("%s, %w", ErrFindMany.Error(), err)
}
return nil
}
// FindOne will return a single document from the provided collection determined by the bson filter.
//
// The target must be a non-nil pointer to a slice of desired type.
func (op *Operator) FindOne(collection string, filter, target interface{}, opts ...*options.FindOneOptions) error {
if filter == nil {
return ErrNilFilter
}
if target == nil {
return ErrNilTarget
}
c, err := op.getCollection(collection)
if err != nil {
return fmt.Errorf("%s, %w", ErrFindOne.Error(), err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(op.config.TimeoutMS)*time.Millisecond)
defer cancel()
res := c.FindOne(ctx, filter, opts...)
if err := res.Err(); err != nil {
return fmt.Errorf("%s failed to find document; %w", ErrFindOne.Error(), err)
}
if err := res.Decode(target); err != nil {
return fmt.Errorf("%s, failed to decode result to target; %w", ErrFindOne.Error(), err)
}
return nil
}
// InsertMany will insert multiple documents into the provided collection.
func (op *Operator) InsertMany(collection string, payload []interface{}, opts ...*options.InsertManyOptions) ([]interface{}, error) {
if payload == nil {
return nil, ErrNilPayload
}
if len(payload) <= 0 {
return nil, fmt.Errorf("provided payload slice cannot be empty")
}
c, err := op.getCollection(collection)
if err != nil {
return nil, fmt.Errorf("failed to get collection '%s'; %w", collection, err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(op.config.TimeoutMS)*time.Millisecond)
defer cancel()
res, err := c.InsertMany(ctx, payload, opts...)
if err != nil {
return nil, fmt.Errorf("%s; %w", ErrInsertMany.Error(), err)
}
return res.InsertedIDs, nil
}
// InsertOne will insert a single payload into the provided collection.
func (op *Operator) InsertOne(collection string, payload interface{}, opts ...*options.InsertOneOptions) error {
if payload == nil {
return ErrNilPayload
}
c, err := op.getCollection(collection)
if err != nil {
return fmt.Errorf("%s, %w", ErrInsertOne.Error(), err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(op.config.TimeoutMS)*time.Millisecond)
defer cancel()
if _, err := c.InsertOne(ctx, payload, opts...); err != nil {
return fmt.Errorf("%s, %w", ErrInsertOne.Error(), err)
}
return nil
}
// UpdateMany will perform an update on multiple documents in the provided collection using the update parameter in the payload.
//
// The count returned represents the total number of matched documents.
//
// A value of -1 will be returned if any error occurs during this operation.
func (op *Operator) UpdateMany(collection string, filter, payload interface{}, opts ...*options.UpdateOptions) (int64, error) {
if filter == nil {
return -1, ErrNilFilter
}
if payload == nil {
return -1, ErrNilPayload
}
c, err := op.getCollection(collection)
if err != nil {
return -1, fmt.Errorf("%s, %w", ErrUpdateMany.Error(), err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(op.config.TimeoutMS)*time.Millisecond)
defer cancel()
res, err := c.UpdateMany(ctx, filter, bson.M{"$set": payload}, opts...)
if err != nil {
return -1, fmt.Errorf("%s, %w", ErrUpdateMany.Error(), err)
}
return res.MatchedCount, nil
}
// UpdateOne will update a single document in the provided collection using the update parameters defined in the payload.
func (op *Operator) UpdateOne(collection string, filter, payload interface{}, opts ...*options.FindOneAndUpdateOptions) error {
if filter == nil {
return ErrNilFilter
}
if payload == nil {
return ErrNilPayload
}
c, err := op.getCollection(collection)
if err != nil {
return fmt.Errorf("%s, %w", ErrUpdateOne.Error(), err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(op.config.TimeoutMS)*time.Millisecond)
defer cancel()
res := c.FindOneAndUpdate(ctx, filter, bson.M{"$set": payload}, opts...)
if res.Err() != nil && errors.Is(res.Err(), mongo.ErrNoDocuments) {
return fmt.Errorf("%s; %w", ErrUpdateOne.Error(), err)
}
return nil
}
func (op *Operator) getCollection(collection string) (*mongo.Collection, error) {
if strings.TrimSpace(collection) == "" {
return nil, ErrEmptyCollectionName
}
return op.client.Database(op.config.Database).Collection(collection), nil
}