-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.go
505 lines (454 loc) · 13.6 KB
/
mongo.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package morm
import (
"context"
"errors"
"net/http"
"reflect"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/94peter/morm/conn"
"github.com/94peter/morm/format"
)
type MgoAggregate interface {
GetPipeline(q bson.M) mongo.Pipeline
Collection
}
type MgoDBModel interface {
DisableCheckBeforeSave(b bool)
SetDB(db *mongo.Database)
BatchUpdate(doclist []DocInter, getField func(d DocInter) bson.D, u LogUser) (failed []DocInter, err error)
BatchSave(doclist []DocInter, u LogUser) (inserted []interface{}, failed []DocInter, err error)
Save(d DocInter, u LogUser) (interface{}, error)
RemoveAll(d DocInter, q primitive.M, u LogUser) (int64, error)
RemoveByID(d DocInter, u LogUser) (int64, error)
UpdateOne(d DocInter, fields bson.D, u LogUser) (int64, error)
UpdateAll(d DocInter, q bson.M, fields bson.D, u LogUser) (int64, error)
UnsetFields(d DocInter, q bson.M, fields []string, u LogUser) (int64, error)
Upsert(d DocInter, u LogUser) (interface{}, error)
FindByID(d DocInter) error
FindOne(d DocInter, q bson.M, option ...*options.FindOneOptions) error
Find(d DocInter, q bson.M, option ...*options.FindOptions) (interface{}, error)
FindAndExec(
d DocInter, q bson.M,
exec func(i interface{}) error,
opts ...*options.FindOptions,
) error
PipeFindOne(aggr MgoAggregate, filter bson.M) error
PipeFind(aggr MgoAggregate, filter bson.M, opts ...*options.AggregateOptions) (interface{}, error)
PipeFindAndExec(
aggr MgoAggregate, q bson.M,
exec func(i interface{}) error,
opts ...*options.AggregateOptions,
) error
PagePipeFind(aggr MgoAggregate, filter bson.M, sort bson.M, limit, page int64) (interface{}, error)
PageFind(d DocInter, q bson.M, limit, page int64, opts ...*options.FindOptions) (interface{}, error)
CountDocuments(d Collection, q bson.M) (int64, error)
GetPaginationSource(d DocInter, q bson.M, opts ...*options.FindOptions) format.PaginationSource
GetPipePaginationSource(aggr MgoAggregate, q bson.M, sort bson.M) format.PaginationSource
CreateCollection(dlist ...DocInter) error
//Reference to customer code, use for aggregate pagination
CountAggrDocuments(aggr MgoAggregate, q bson.M) (int64, error)
GetPipeMatchPaginationSource(aggr MgoAggregate, q bson.M, sort bson.M) format.PaginationSource
NewFindMgoDS(d DocInter, q bson.M, opts ...*options.FindOptions) MgoDS
NewPipeFindMgoDS(d MgoAggregate, q bson.M, opts ...*options.AggregateOptions) MgoDS
}
func NewMgoModel(ctx context.Context, db *mongo.Database) MgoDBModel {
return &mgoModelImpl{
db: db,
ctx: ctx,
selfCtx: context.Background(),
}
}
func NewMgoModelByReq(req *http.Request) MgoDBModel {
mgodbclt := conn.GetMgoDbConnFromReq(req)
if mgodbclt == nil {
panic("database not set in req")
}
return &mgoModelImpl{
db: mgodbclt.GetDbConn(),
ctx: req.Context(),
selfCtx: context.Background(),
}
}
type mgoModelImpl struct {
disableCheckBeforeSave bool
db *mongo.Database
ctx context.Context
selfCtx context.Context
}
func (mm *mgoModelImpl) DisableCheckBeforeSave(b bool) {
mm.disableCheckBeforeSave = b
}
func (mm *mgoModelImpl) SetDB(db *mongo.Database) {
mm.db = db
}
func (mm *mgoModelImpl) FindAndExec(
d DocInter, q bson.M,
exec func(i interface{}) error,
opts ...*options.FindOptions,
) error {
var err error
collection := mm.db.Collection(d.GetC())
sortCursor, err := collection.Find(mm.ctx, q, opts...)
if err != nil {
return nil
}
val := reflect.ValueOf(d)
if val.Kind() == reflect.Ptr {
val = reflect.Indirect(val)
}
var newValue reflect.Value
var newDoc DocInter
for sortCursor.Next(mm.ctx) {
newValue = reflect.New(val.Type())
newDoc = newValue.Interface().(DocInter)
err = sortCursor.Decode(newDoc)
if err != nil {
return err
}
err = exec(newDoc)
if err != nil {
return err
}
}
w2 := reflect.ValueOf(newValue)
if w2.IsZero() {
return nil
}
for i := 0; i < val.NumField(); i++ {
f := val.Field(i)
f.Set(newValue.Elem().Field(i))
}
return err
}
func (mm *mgoModelImpl) CountDocuments(d Collection, q bson.M) (int64, error) {
return mm.db.Collection(d.GetC()).CountDocuments(mm.ctx, q)
}
func (mm *mgoModelImpl) isCollectExisted(d DocInter) bool {
names, err := mm.db.ListCollectionNames(mm.selfCtx, bson.D{{Key: "name", Value: d.GetC()}})
if ce, ok := err.(mongo.CommandError); ok {
return ce.Name == "OperationNotSupportedInTransaction"
}
return isStrInList(d.GetC(), names...)
}
func (mm *mgoModelImpl) CreateCollection(dlist ...DocInter) (err error) {
for _, d := range dlist {
// check collection exist
if !mm.isCollectExisted(d) {
if len(d.GetIndexes()) > 0 {
_, err = mm.db.Collection(d.GetC()).Indexes().CreateMany(mm.ctx, d.GetIndexes())
} else {
err = mm.db.CreateCollection(mm.ctx, d.GetC())
}
if err != nil {
return err
}
}
}
return
}
func (mm *mgoModelImpl) BatchUpdate(doclist []DocInter, getField func(d DocInter) bson.D, u LogUser) (failed []DocInter, err error) {
if len(doclist) == 0 {
return
}
collection := mm.db.Collection(doclist[0].GetC())
var operations []mongo.WriteModel
for _, d := range doclist {
op := mongo.NewUpdateOneModel()
op.SetFilter(bson.M{"_id": d.GetID()})
op.SetUpdate(bson.D{
{Key: "$set", Value: getField(d)},
})
op.SetUpsert(true)
operations = append(operations, op)
}
bulkOption := options.BulkWriteOptions{}
_, err = collection.BulkWrite(mm.ctx, operations, &bulkOption)
if excep, ok := err.(mongo.BulkWriteException); ok {
for _, e := range excep.WriteErrors {
failed = append(failed, doclist[e.Index])
}
}
return
}
func (mm *mgoModelImpl) BatchSave(doclist []DocInter, u LogUser) (inserted []interface{}, failed []DocInter, err error) {
if len(doclist) == 0 {
inserted = nil
return
}
collection := mm.db.Collection(doclist[0].GetC())
if !mm.disableCheckBeforeSave {
err := mm.CreateCollection(doclist[0])
if err != nil {
return nil, doclist, err
}
}
ordered := false
var batch []interface{}
for _, d := range doclist {
if u != nil {
d.SetCreator(u)
}
batch = append(batch, d)
}
var result *mongo.InsertManyResult
result, err = collection.InsertMany(mm.ctx, batch, &options.InsertManyOptions{Ordered: &ordered})
if result != nil {
inserted = result.InsertedIDs
}
if excep, ok := err.(mongo.BulkWriteException); ok {
for _, e := range excep.WriteErrors {
failed = append(failed, doclist[e.Index])
}
}
return
}
func (mm *mgoModelImpl) Save(d DocInter, u LogUser) (interface{}, error) {
if !mm.disableCheckBeforeSave {
err := mm.CreateCollection(d)
if err != nil {
return primitive.NilObjectID, err
}
}
if u != nil {
d.SetCreator(u)
}
collection := mm.db.Collection(d.GetC())
result, err := collection.InsertOne(mm.ctx, d.GetDoc())
if err != nil {
return primitive.NilObjectID, err
}
return result.InsertedID, err
}
func (mm *mgoModelImpl) RemoveAll(d DocInter, q primitive.M, u LogUser) (int64, error) {
collection := mm.db.Collection(d.GetC())
result, err := collection.DeleteMany(mm.ctx, q)
return result.DeletedCount, err
}
func (mm *mgoModelImpl) RemoveByID(d DocInter, u LogUser) (int64, error) {
collection := mm.db.Collection(d.GetC())
result, err := collection.DeleteOne(mm.ctx, bson.M{"_id": d.GetID()})
return result.DeletedCount, err
}
func (mm *mgoModelImpl) UpdateOne(d DocInter, fields bson.D, u LogUser) (int64, error) {
if u != nil {
fields = append(fields, primitive.E{Key: "records", Value: d.AddRecord(u, "updated")})
}
collection := mm.db.Collection(d.GetC())
result, err := collection.UpdateOne(mm.ctx, bson.M{"_id": d.GetID()},
bson.D{
{Key: "$set", Value: fields},
},
)
if result != nil {
return result.ModifiedCount, err
}
return 0, err
}
func (mm *mgoModelImpl) UpdateAll(d DocInter, q bson.M, fields bson.D, u LogUser) (int64, error) {
updated := bson.D{
{Key: "$set", Value: fields},
}
if u != nil {
updated = append(updated, primitive.E{Key: "$push", Value: primitive.M{"records": NewRecord(time.Now(), u.GetAccount(), u.GetName(), "updated")}})
}
collection := mm.db.Collection(d.GetC())
result, err := collection.UpdateMany(mm.ctx, q, updated)
if result != nil {
return result.ModifiedCount, err
}
return 0, err
}
func (mm *mgoModelImpl) UnsetFields(d DocInter, q bson.M, fields []string, u LogUser) (int64, error) {
collection := mm.db.Collection(d.GetC())
m := primitive.M{}
for _, k := range fields {
m[k] = ""
}
result, err := collection.UpdateMany(mm.ctx, q,
bson.D{
{Key: "$unset", Value: m},
},
)
if result != nil {
return result.ModifiedCount, err
}
return 0, err
}
func (mm *mgoModelImpl) Upsert(d DocInter, u LogUser) (interface{}, error) {
err := mm.CreateCollection(d)
if err != nil {
return primitive.NilObjectID, err
}
collection := mm.db.Collection(d.GetC())
_, err = collection.UpdateOne(mm.ctx, bson.M{"_id": d.GetID()}, bson.M{"$set": d.GetDoc()}, options.Update().SetUpsert(true))
if err != nil {
return primitive.NilObjectID, err
}
return d.GetID(), nil
}
func (mm *mgoModelImpl) FindByID(d DocInter) error {
return mm.FindOne(d, bson.M{"_id": d.GetID()})
}
func (mm *mgoModelImpl) FindOne(d DocInter, q bson.M, option ...*options.FindOneOptions) error {
if mm.db == nil {
return errors.New("db is nil")
}
if d == nil {
return errors.New("doc is nil")
}
collection := mm.db.Collection(d.GetC())
return collection.FindOne(mm.ctx, q, option...).Decode(d)
}
func (mm *mgoModelImpl) Find(d DocInter, q bson.M, option ...*options.FindOptions) (interface{}, error) {
myType := reflect.TypeOf(d)
slice := reflect.MakeSlice(reflect.SliceOf(myType), 0, 0).Interface()
collection := mm.db.Collection(d.GetC())
sortCursor, err := collection.Find(mm.ctx, q, option...)
if err != nil {
return nil, err
}
err = sortCursor.All(mm.ctx, &slice)
if err != nil {
return nil, err
}
return slice, err
}
func (mm *mgoModelImpl) PipeFind(aggr MgoAggregate, filter bson.M, opts ...*options.AggregateOptions) (interface{}, error) {
myType := reflect.TypeOf(aggr)
slice := reflect.MakeSlice(reflect.SliceOf(myType), 0, 0).Interface()
collection := mm.db.Collection(aggr.GetC())
sortCursor, err := collection.Aggregate(mm.ctx, aggr.GetPipeline(filter), opts...)
if err != nil {
return nil, err
}
err = sortCursor.All(mm.ctx, &slice)
if err != nil {
return nil, err
}
return slice, err
}
func (mm *mgoModelImpl) PipeFindAndExec(aggr MgoAggregate, filter bson.M, exec func(i interface{}) error, opts ...*options.AggregateOptions) error {
collection := mm.db.Collection(aggr.GetC())
sortCursor, err := collection.Aggregate(mm.ctx, aggr.GetPipeline(filter), opts...)
if err != nil {
return err
}
val := reflect.ValueOf(aggr)
if val.Kind() == reflect.Ptr {
val = reflect.Indirect(val)
}
var newValue reflect.Value
var newDoc DocInter
for sortCursor.Next(mm.ctx) {
newValue = reflect.New(val.Type())
newDoc = newValue.Interface().(DocInter)
err = sortCursor.Decode(newDoc)
if err != nil {
return err
}
err = exec(newDoc)
if err != nil {
return err
}
}
w2 := reflect.ValueOf(newValue)
if w2.IsZero() {
return nil
}
for i := 0; i < val.NumField(); i++ {
f := val.Field(i)
f.Set(newValue.Elem().Field(i))
}
return err
}
func (mm *mgoModelImpl) PipeFindOne(aggr MgoAggregate, filter bson.M) error {
collection := mm.db.Collection(aggr.GetC())
sortCursor, err := collection.Aggregate(mm.ctx, aggr.GetPipeline(filter))
if err != nil {
return err
}
if sortCursor.Next(mm.ctx) {
err = sortCursor.Decode(aggr)
if err != nil {
return err
}
}
return nil
}
func (mm *mgoModelImpl) PageFind(d DocInter, filter bson.M, limit, page int64, opts ...*options.FindOptions) (interface{}, error) {
if limit <= 0 {
limit = 50
}
if page <= 0 {
page = 1
}
skip := limit * (page - 1)
findopt := options.Find().SetSkip(skip).SetLimit(limit)
opts = append(opts, findopt)
myType := reflect.TypeOf(d)
slice := reflect.MakeSlice(reflect.SliceOf(myType), 0, 0).Interface()
collection := mm.db.Collection(d.GetC())
sortCursor, err := collection.Find(mm.ctx, filter, opts...)
if err != nil {
return nil, err
}
err = sortCursor.All(mm.ctx, &slice)
return slice, err
}
func (mm *mgoModelImpl) PagePipeFind(aggr MgoAggregate, filter bson.M, sort bson.M, limit, page int64) (interface{}, error) {
if limit <= 0 {
limit = 50
}
if page <= 0 {
page = 1
}
skip := limit * (page - 1)
myType := reflect.TypeOf(aggr)
slice := reflect.MakeSlice(reflect.SliceOf(myType), 0, 0).Interface()
collection := mm.db.Collection(aggr.GetC())
pl := append(aggr.GetPipeline(filter), bson.D{{Key: "$sort", Value: sort}}, bson.D{{Key: "$skip", Value: skip}}, bson.D{{Key: "$limit", Value: limit}})
sortCursor, err := collection.Aggregate(mm.ctx, pl)
if err != nil {
return nil, err
}
err = sortCursor.All(mm.ctx, &slice)
if err != nil {
return nil, err
}
return slice, err
}
// ----- New added code -----
func (mm *mgoModelImpl) AggrCountDocuments(aggr MgoAggregate, q bson.M) (int64, error) {
return mm.db.Collection(aggr.GetC()).CountDocuments(mm.ctx, q)
}
type countMgoAggregate struct {
Count int
}
func (mm *mgoModelImpl) CountAggrDocuments(aggr MgoAggregate, q bson.M) (int64, error) {
collection := mm.db.Collection(aggr.GetC())
pl := append(aggr.GetPipeline(q), bson.D{{Key: "$count", Value: "count"}})
sortCursor, err := collection.Aggregate(mm.ctx, pl)
if err != nil {
return 0, err
}
var obj countMgoAggregate
if sortCursor.Next(mm.ctx) {
err = sortCursor.Decode(&obj)
if err != nil {
return 0, err
}
}
return int64(obj.Count), nil
}
func isStrInList(input string, target ...string) bool {
for _, paramName := range target {
if input == paramName {
return true
}
}
return false
}