forked from free5gc/MongoDBLibrary
/
api_mongoDB.go
251 lines (195 loc) · 6.56 KB
/
api_mongoDB.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
package MongoDBLibrary
import (
"context"
"encoding/json"
"time"
jsonpatch "github.com/evanphx/json-patch"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/free5gc/MongoDBLibrary/logger"
)
var Client *mongo.Client = nil
var dbName string
func SetMongoDB(setdbName string, url string) {
if Client != nil {
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(url))
defer cancel()
if err != nil {
//defer cancel()
logger.MongoDBLog.Panic(err.Error())
}
Client = client
dbName = setdbName
}
func RestfulAPIGetOne(collName string, filter bson.M) map[string]interface{} {
collection := Client.Database(dbName).Collection(collName)
var result map[string]interface{}
collection.FindOne(context.TODO(), filter).Decode(&result)
return result
}
func RestfulAPIGetMany(collName string, filter bson.M) []map[string]interface{} {
collection := Client.Database(dbName).Collection(collName)
var resultArray []map[string]interface{}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
cur, err := collection.Find(ctx, filter)
defer cancel()
if err != nil {
logger.MongoDBLog.Fatal(err)
}
defer cur.Close(ctx)
for cur.Next(ctx) {
var result map[string]interface{}
err := cur.Decode(&result)
if err != nil {
logger.MongoDBLog.Fatal(err)
}
resultArray = append(resultArray, result)
}
if err := cur.Err(); err != nil {
logger.MongoDBLog.Fatal(err)
}
return resultArray
}
func RestfulAPIPutOne(collName string, filter bson.M, putData map[string]interface{}) bool {
collection := Client.Database(dbName).Collection(collName)
var checkItem map[string]interface{}
collection.FindOne(context.TODO(), filter).Decode(&checkItem)
if checkItem == nil {
collection.InsertOne(context.TODO(), putData)
return false
} else {
collection.UpdateOne(context.TODO(), filter, bson.M{"$set": putData})
return true
}
}
func RestfulAPIPutOneNotUpdate(collName string, filter bson.M, putData map[string]interface{}) bool {
collection := Client.Database(dbName).Collection(collName)
var checkItem map[string]interface{}
collection.FindOne(context.TODO(), filter).Decode(&checkItem)
if checkItem == nil {
collection.InsertOne(context.TODO(), putData)
return false
} else {
// collection.UpdateOne(context.TODO(), filter, bson.M{"$set": putData})
return true
}
}
func RestfulAPIPutMany(collName string, filterArray []bson.M, putDataArray []map[string]interface{}) bool {
collection := Client.Database(dbName).Collection(collName)
var checkItem map[string]interface{}
for i, putData := range putDataArray {
checkItem = nil
filter := filterArray[i]
collection.FindOne(context.TODO(), filter).Decode(&checkItem)
if checkItem == nil {
collection.InsertOne(context.TODO(), putData)
} else {
collection.UpdateOne(context.TODO(), filter, bson.M{"$set": putData})
}
}
if checkItem == nil {
return false
} else {
return true
}
}
func RestfulAPIDeleteOne(collName string, filter bson.M) {
collection := Client.Database(dbName).Collection(collName)
collection.DeleteOne(context.TODO(), filter)
}
func RestfulAPIDeleteMany(collName string, filter bson.M) {
collection := Client.Database(dbName).Collection(collName)
collection.DeleteMany(context.TODO(), filter)
}
func RestfulAPIMergePatch(collName string, filter bson.M, patchData map[string]interface{}) bool {
collection := Client.Database(dbName).Collection(collName)
var originalData map[string]interface{}
result := collection.FindOne(context.TODO(), filter)
if err := result.Decode(&originalData); err != nil { // Data doesn't exist in DB
return false
} else {
delete(originalData, "_id")
original, _ := json.Marshal(originalData)
patchDataByte, err := json.Marshal(patchData)
if err != nil {
logger.MongoDBLog.Panic(err)
}
modifiedAlternative, err := jsonpatch.MergePatch(original, patchDataByte)
if err != nil {
logger.MongoDBLog.Panic(err)
}
var modifiedData map[string]interface{}
json.Unmarshal(modifiedAlternative, &modifiedData)
collection.UpdateOne(context.TODO(), filter, bson.M{"$set": modifiedData})
return true
}
}
func RestfulAPIJSONPatch(collName string, filter bson.M, patchJSON []byte) bool {
collection := Client.Database(dbName).Collection(collName)
var originalData map[string]interface{}
result := collection.FindOne(context.TODO(), filter)
if err := result.Decode(&originalData); err != nil { // Data doesn't exist in DB
return false
} else {
delete(originalData, "_id")
original, _ := json.Marshal(originalData)
patch, err := jsonpatch.DecodePatch(patchJSON)
if err != nil {
logger.MongoDBLog.Panic(err)
}
modified, err := patch.Apply(original)
if err != nil {
logger.MongoDBLog.Panic(err)
}
var modifiedData map[string]interface{}
json.Unmarshal(modified, &modifiedData)
collection.UpdateOne(context.TODO(), filter, bson.M{"$set": modifiedData})
return true
}
}
func RestfulAPIJSONPatchExtend(collName string, filter bson.M, patchJSON []byte, dataName string) bool {
collection := Client.Database(dbName).Collection(collName)
var originalDataCover map[string]interface{}
result := collection.FindOne(context.TODO(), filter)
if err := result.Decode(&originalDataCover); err != nil { // Data does'nt exist in db
return false
} else {
delete(originalDataCover, "_id")
originalData := originalDataCover[dataName]
original, _ := json.Marshal(originalData)
jsonpatch.DecodePatch(patchJSON)
patch, err := jsonpatch.DecodePatch(patchJSON)
if err != nil {
logger.MongoDBLog.Panic(err)
}
modified, err := patch.Apply(original)
if err != nil {
logger.MongoDBLog.Panic(err)
}
var modifiedData map[string]interface{}
json.Unmarshal(modified, &modifiedData)
collection.UpdateOne(context.TODO(), filter, bson.M{"$set": bson.M{dataName: modifiedData}})
return true
}
}
func RestfulAPIPost(collName string, filter bson.M, postData map[string]interface{}) bool {
collection := Client.Database(dbName).Collection(collName)
var checkItem map[string]interface{}
collection.FindOne(context.TODO(), filter).Decode(&checkItem)
if checkItem == nil {
collection.InsertOne(context.TODO(), postData)
return false
} else {
collection.UpdateOne(context.TODO(), filter, bson.M{"$set": postData})
return true
}
}
func RestfulAPIPostMany(collName string, filter bson.M, postDataArray []interface{}) bool {
collection := Client.Database(dbName).Collection(collName)
collection.InsertMany(context.TODO(), postDataArray)
return false
}