/
db.go
145 lines (116 loc) · 2.95 KB
/
db.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
package mongolang
/*
Methods for accessing MongoDB Databases and
initializing the framework.
*/
import (
"context"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Disconnect disconnects the MongoDB and
// cleans up any other resources, resetting the MonGolang structure
func (mg *DB) Disconnect() {
if mg.Client != nil {
if err := mg.Client.Disconnect(context.Background()); err != nil {
if mg.Err == nil {
mg.Err = err
}
}
}
mg.Client = nil
mg.Database = nil
mg.Name = ""
}
// clientOkay returns true if the mg.Client is okay
func (mg *DB) clientOkay() bool {
if mg.Client == nil {
if mg.Err == nil {
mg.Err = ErrNotConnected
}
return false
}
return true
}
// checkDBOkay checks if the mg.Client and mg.Database
// are properly initialized
func (mg *DB) dbOkay() bool {
if !mg.clientOkay() {
return false
}
if mg.Database == nil {
if mg.Err == nil {
mg.Err = ErrNotConnectedDB
}
return false
}
return true
}
// InitMonGolang initializes the connection
// to the MongoDB Database
func (mg *DB) InitMonGolang(connectionURI string) *DB {
mg.Disconnect()
// get MongoDB Client
mg.Client, mg.Err = mongo.NewClient(options.Client().ApplyURI(connectionURI))
if mg.Err != nil {
mg.Client = nil
return mg
}
// Connect to Database
ctx, ctxCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer ctxCancel()
mg.Err = mg.Client.Connect(ctx)
if mg.Err != nil {
mg.Client = nil
}
return mg
}
// Use connects the MongoDB Client to the specified Database.
// The MonGolangDB needs to be inialized via mg.InitMonGolang()
// before calling this method.
func (mg *DB) Use(dbName string) *DB {
// exit if we don't have an mg.Client
if !mg.clientOkay() {
return mg
}
mg.Name = dbName
mg.Database = mg.Client.Database(dbName)
mg.Err = nil
return mg
}
// Coll returns a collection for a given name
// If there was a previous error
// don't set coll.MongoColl
func (mg *DB) Coll(collectionName string) *Coll {
coll := new(Coll)
coll.DB = mg
coll.CollName = collectionName
// return if we don't have a Database or Client
if !mg.dbOkay() {
return coll
}
coll.MongoColl = mg.Database.Collection(collectionName, nil)
return coll
}
// ShowDBs returns a list of Database Names
func (mg *DB) ShowDBs() []string {
if !mg.clientOkay() {
var result []string
return result
}
databases, err := mg.Client.ListDatabaseNames(context.Background(), bson.M{})
mg.Err = err
return databases
}
// ShowCollections returns a list of collections for current Database
func (mg *DB) ShowCollections() []string {
if !mg.dbOkay() {
var result []string
return result
}
collections, err := mg.Database.ListCollectionNames(context.Background(), bson.M{})
mg.Err = err
return collections
}