-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongo.go
391 lines (361 loc) · 9.94 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
package mongo
// mongo module
//
// Copyright (c) 2019-2024 - Valentin Kuznetsov <vkuznet AT gmail dot com>
//
// References :
// using mgo driver:
// https://gist.github.com/boj/5412538
// https://gist.github.com/border/3489566
// using mongo driver:
// https://github.com/mongodb/mongo-go-driver/
// https://www.loginradius.com/blog/engineering/mongodb-as-datasource-in-golang/
import (
"context"
"fmt"
"html"
"log"
"strings"
"time"
bson "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const (
_ = iota
ServerError
DBError
QueryError
ParserError
ValidationError
)
// ServerErrorName and others provides human based definition of the error
const (
ServerErrorName = "Server error"
DBErrorName = "MongoDB error"
QueryErrorName = "Server query error"
ParserErrorName = "Server parser error"
ValidationErrorName = "Server validation error"
)
/*
// Record define Mongo record
type Record map[string]interface{}
// JsonStrings provides string representation of Record
func (r Record) JsonString() string {
// create pretty JSON representation of the record
data, _ := json.MarshalIndent(r, "", " ")
return string(data)
}
// ToString provides string representation of Record
func (r Record) String() string {
var out []string
for _, k := range MapKeys(r) {
if k == "_id" {
continue
}
switch v := r[k].(type) {
case int, int64:
out = append(out, fmt.Sprintf("%s:%d", k, v))
case float64:
d := int(v)
if float64(d) == v {
out = append(out, fmt.Sprintf("%s:%d", k, d))
} else {
out = append(out, fmt.Sprintf("%s:%f", k, v))
}
case []interface{}:
var vals []string
for i, val := range v {
if i == len(v)-1 {
vals = append(vals, fmt.Sprintf("%v", val))
} else {
vals = append(vals, fmt.Sprintf("%v,", val))
}
}
out = append(out, fmt.Sprintf("%s:%s", k, vals))
default:
out = append(out, fmt.Sprintf("%s:%v", k, r[k]))
}
}
return strings.Join(out, "\n")
}
*/
// ErrorRecord provides error record
func ErrorRecord(msg, etype string, ecode int) map[string]any {
erec := make(map[string]any)
erec["error"] = html.EscapeString(msg)
erec["type"] = html.EscapeString(etype)
erec["code"] = ecode
return erec
}
// GetValue function to get int value from record for given key
func GetValue(rec map[string]any, key string) interface{} {
var val map[string]any
keys := strings.Split(key, ".")
if len(keys) > 1 {
value, ok := rec[keys[0]]
if !ok {
log.Printf("Unable to find key value in Record %v, key %v\n", rec, key)
return ""
}
switch v := value.(type) {
case map[string]any:
val = v
case []map[string]any:
if len(v) > 0 {
val = v[0]
} else {
return ""
}
case []interface{}:
vvv := v[0]
if vvv != nil {
val = vvv.(map[string]any)
} else {
return ""
}
default:
log.Printf("Unknown type %v, rec %v, key %v keys %v\n", fmt.Sprintf("%T", v), v, key, keys)
return ""
}
if len(keys) == 2 {
return GetValue(val, keys[1])
}
return GetValue(val, strings.Join(keys[1:], "."))
}
value := rec[key]
return value
}
// helper function to return single entry (e.g. from a list) of given value
func singleEntry(data interface{}) interface{} {
switch v := data.(type) {
case []interface{}:
return v[0]
default:
return v
}
}
// GetStringValue function to get string value from record for given key
func GetStringValue(rec map[string]any, key string) (string, error) {
value := GetValue(rec, key)
val := fmt.Sprintf("%v", value)
return val, nil
}
// GetSingleStringValue function to get string value from record for given key
func GetSingleStringValue(rec map[string]any, key string) (string, error) {
value := singleEntry(GetValue(rec, key))
val := fmt.Sprintf("%v", value)
return val, nil
}
// GetIntValue function to get int value from record for given key
func GetIntValue(rec map[string]any, key string) (int, error) {
value := GetValue(rec, key)
val, ok := value.(int)
if ok {
return val, nil
}
return 0, fmt.Errorf("Unable to cast value for key '%s'", key)
}
// GetInt64Value function to get int value from record for given key
func GetInt64Value(rec map[string]any, key string) (int64, error) {
value := GetValue(rec, key)
out, ok := value.(int64)
if ok {
return out, nil
}
return 0, fmt.Errorf("Unable to cast value for key '%s'", key)
}
// Connection defines connection to MongoDB
type Connection struct {
Client *mongo.Client
URI string
}
// InitMongoDB initializes MongoDB connection object
func InitMongoDB(uri string) {
Mongo = Connection{URI: uri}
}
// Connect provides connection to MongoDB
func (m *Connection) Connect() *mongo.Client {
var err error
if m.Client != nil {
return m.Client
}
client, err := mongo.NewClient(options.Client().ApplyURI(m.URI))
if err != nil {
log.Fatal(err)
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
m.Client = client
return client
}
// Mongo holds MongoDB connection
var Mongo Connection
// InsertAny insert records into MongoDB
func InsertAny(dbname, collname string, records []any) {
client := Mongo.Connect()
ctx := context.TODO()
c := client.Database(dbname).Collection(collname)
for _, rec := range records {
if _, err := c.InsertOne(ctx, &rec); err != nil {
log.Printf("Fail to insert record %v, error %v\n", rec, err)
}
}
}
// UpsertAny upsert records into MongoDB
func UpsertAny(dbname, collname string, records []any) error {
client := Mongo.Connect()
ctx := context.TODO()
c := client.Database(dbname).Collection(collname)
for _, rec := range records {
spec := bson.M{}
update := bson.D{{"$set", rec}}
opts := options.Update().SetUpsert(true)
if _, err := c.UpdateOne(ctx, spec, update, opts); err != nil {
log.Printf("Fail to insert record %v, error %v\n", rec, err)
return err
}
}
return nil
}
// Insert records into MongoDB
func Insert(dbname, collname string, records []map[string]any) {
client := Mongo.Connect()
ctx := context.TODO()
c := client.Database(dbname).Collection(collname)
for _, rec := range records {
if _, err := c.InsertOne(ctx, &rec); err != nil {
log.Printf("Fail to insert record %v, error %v\n", rec, err)
}
}
}
// UpsertRecord insert record with given spec to MongoDB
func UpsertRecord(dbname, collname string, spec bson.M, rec bson.M) error {
client := Mongo.Connect()
ctx := context.TODO()
c := client.Database(dbname).Collection(collname)
opts := options.Update().SetUpsert(true)
if _, err := c.UpdateOne(ctx, spec, rec, opts); err != nil {
log.Printf("Fail to insert record %v, error %v\n", rec, err)
return err
}
return nil
}
// Upsert records into MongoDB
func Upsert(dbname, collname, attr string, records []map[string]any) error {
client := Mongo.Connect()
ctx := context.TODO()
c := client.Database(dbname).Collection(collname)
for _, rec := range records {
value := rec[attr].(string)
if value == "" {
continue
}
spec := bson.M{attr: value}
update := bson.D{{"$set", rec}}
opts := options.Update().SetUpsert(true)
if _, err := c.UpdateOne(ctx, spec, update, opts); err != nil {
log.Printf("Fail to insert record %v, error %v\n", rec, err)
return err
}
}
return nil
}
// Get records from MongoDB
func Get(dbname, collname string, spec bson.M, idx, limit int) []map[string]any {
out := []map[string]any{}
client := Mongo.Connect()
ctx := context.TODO()
c := client.Database(dbname).Collection(collname)
var err error
if limit > 0 {
opts := options.Find().SetSkip(int64(idx)).SetLimit(int64(limit))
cur, err := c.Find(ctx, spec, opts)
if err != nil {
log.Printf("ERROR: spec=%+v, error=%v", spec, err)
}
cur.All(ctx, &out)
} else {
opts := options.Find().SetSkip(int64(idx))
cur, err := c.Find(ctx, spec, opts)
if err != nil {
log.Printf("ERROR: spec=%+v, error=%v", spec, err)
}
cur.All(ctx, &out)
}
if err != nil {
log.Printf("Unable to get records, error %v\n", err)
}
return out
}
// GetSorted records from MongoDB sorted by given key
func GetSorted(dbname, collname string, spec bson.M, skeys []string) []map[string]any {
out := []map[string]any{}
client := Mongo.Connect()
ctx := context.TODO()
c := client.Database(dbname).Collection(collname)
var sortSpec bson.M
for _, s := range skeys {
sortSpec[s] = 1
}
opts := options.Find().SetSort(sortSpec)
cur, err := c.Find(ctx, spec, opts)
cur.All(ctx, &out)
if err != nil {
log.Printf("Unable to sort records, error %v\n", err)
// try to fetch all unsorted data
cur, err := c.Find(ctx, spec)
if err != nil {
log.Printf("Unable to find records, error %v\n", err)
out = append(out, ErrorRecord(fmt.Sprintf("%v", err), DBErrorName, DBError))
return out
}
cur.All(ctx, &out)
}
return out
}
// helper function to present in bson selected fields
func sel(q ...string) (r bson.M) {
r = make(bson.M, len(q))
for _, s := range q {
r[s] = 1
}
return
}
// Update inplace for given spec
func Update(dbname, collname string, spec, newdata bson.M) {
client := Mongo.Connect()
ctx := context.TODO()
c := client.Database(dbname).Collection(collname)
_, err := c.UpdateOne(ctx, spec, newdata)
if err != nil {
log.Printf("Unable to update record, spec %v, data %v, error %v\n", spec, newdata, err)
}
}
// Count gets number records from MongoDB
func Count(dbname, collname string, spec bson.M) int {
client := Mongo.Connect()
ctx := context.TODO()
c := client.Database(dbname).Collection(collname)
nrec, err := c.CountDocuments(ctx, spec)
if err != nil {
log.Printf("Unable to count records, spec %v, error %v\n", spec, err)
}
return int(nrec)
}
// Remove records from MongoDB
func Remove(dbname, collname string, spec bson.M) error {
client := Mongo.Connect()
ctx := context.TODO()
c := client.Database(dbname).Collection(collname)
results, err := c.DeleteMany(ctx, spec)
if err != nil {
log.Printf("Unable to remove records, spec %v, error %v\n", spec, err)
} else {
log.Printf("mongo remove results %+v", results)
}
return err
}