-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind.go
More file actions
195 lines (167 loc) · 5.33 KB
/
Copy pathfind.go
File metadata and controls
195 lines (167 loc) · 5.33 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package morm
import (
"context"
"errors"
"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"
)
// Find sets the filter for the MongoDB query in the CollectQueryBuilder and specifies it's a find operation.
// Additional filter conditions can be provided as optional arguments.
func (qb *CollectQueryBuilder) Find(filter ...interface{}) *CollectQueryBuilder {
if len(filter) > 0 {
qb.filter = filter[0]
}
qb.method = "find"
return qb
}
// find performs the MongoDB find operation based on the CollectQueryBuilder configuration.
// It supports projection, sorting, skipping, and limiting of results.
// The results are decoded into the provided result interface.
func find(qb *CollectQueryBuilder, result interface{}) (interface{}, error) {
options := options.Find()
if qb.projection != nil {
options.SetProjection(qb.projection)
}
if qb.sort != nil {
options.SetSort(qb.sort)
}
if qb.skip != 0 {
options.SetSkip(qb.skip)
}
if qb.limit != 0 {
options.SetLimit(qb.limit)
}
cursor, err := qb.c.collection.Find(context.Background(), qb.filter, options)
if err != nil {
return nil, err
}
defer cursor.Close(context.Background())
var results []interface{}
for cursor.Next(context.Background()) {
if qb.popFields != nil {
result := reflect.New(qb.c.modelType.Elem()).Interface()
err := cursor.Decode(result)
if err != nil {
return nil, err
}
id := reflect.ValueOf(result).Elem().FieldByName("ID")
popRes, err := qb.virtual(qb.popFields, result, bson.M{"_id": id.Interface().(primitive.ObjectID)})
if err != nil {
return nil, err
}
results = append(results, popRes)
} else {
result := reflect.New(qb.c.modelType.Elem()).Interface()
err := cursor.Decode(result)
if err != nil {
return nil, err
}
results = append(results, result)
}
}
if err := cursor.Err(); err != nil {
return nil, err
}
return results, nil
}
// FindOne sets the filter for the MongoDB query in the CollectQueryBuilder and specifies it's a findone operation.
// The result is decoded into the provided result interface.
func (qb *CollectQueryBuilder) FindOne(filter interface{}) *CollectQueryBuilder {
qb.filter = filter
qb.method = "findone"
return qb
}
// findone performs the MongoDB findone operation based on the CollectQueryBuilder configuration.
// It supports projection, sorting, skipping, and populate fields.
// The result is decoded into the provided result interface.
func findone(qb *CollectQueryBuilder, result interface{}) (interface{}, error) {
options := options.FindOne()
if qb.projection != nil {
options.SetProjection(qb.projection)
}
if qb.sort != nil {
options.SetSort(qb.sort)
}
if qb.skip != 0 {
options.SetSkip(qb.skip)
}
if qb.popFields != nil {
result := reflect.New(qb.c.modelType.Elem()).Interface()
resp, err := qb.virtual(qb.popFields, result, qb.filter)
if err != nil {
return nil, err
}
return resp, nil
}
err := qb.c.collection.FindOne(context.Background(), qb.filter, options).Decode(result)
if err != nil {
return nil, err
}
return result, nil
}
// FindOneAndUpdate finds a single document in the specified collection based on the filter and updates it.
// It returns the updated document.
// The update includes setting the "updatedAt" field to the current time.
func (qb *CollectQueryBuilder) FindOneAndUpdate(filter interface{}, update interface{}, ctx ...context.Context) (interface{}, error) {
collection := qb.c.collection
var backgroundContext = context.Background()
if len(ctx) > 0 {
backgroundContext = ctx[0]
}
// Set updatedAt field to the current time
updateWithUpdatedAt := bson.M{
"$set": bson.M{
"updatedAt": time.Now(),
},
}
// Merge the provided update with the update for updatedAt
updateWithUpdatedAt["$set"].(bson.M)["$set"] = update
options := options.FindOneAndUpdate().SetReturnDocument(options.After)
result := collection.FindOneAndUpdate(backgroundContext, filter, updateWithUpdatedAt, options)
if result.Err() != nil {
return nil, result.Err()
}
// Decode the result into the original model
modelType, err := getModelType(qb.c.modelElemPtr.Interface())
if err != nil {
return nil, err
}
resultValue := reflect.New(modelType).Interface()
err = result.Decode(resultValue)
if err != nil {
return nil, err
}
return resultValue, nil
}
// FindOneAndRemove finds a single document in the specified collection based on the filter and removes it.
// It returns the removed document.
func (qb *CollectQueryBuilder) FindOneAndRemove(filter interface{}, ctx ...context.Context) (interface{}, error) {
collection := qb.c.collection
var backgroundContext = context.Background()
if len(ctx) > 0 {
backgroundContext = ctx[0]
}
options := options.FindOneAndDelete().SetProjection(bson.D{{Key: "_id", Value: 0}})
result := collection.FindOneAndDelete(backgroundContext, filter, options)
if result.Err() != nil {
if errors.Is(result.Err(), mongo.ErrNoDocuments) {
return nil, nil
}
return nil, result.Err()
}
// Decode the result into the original model
modelType, err := getModelType(qb.c.modelElemPtr.Interface())
if err != nil {
return nil, err
}
resultValue := reflect.New(modelType).Interface()
err = result.Decode(resultValue)
if err != nil {
return nil, err
}
return resultValue, nil
}