forked from kkeuning/go-api-example
/
databaseSetup.go
62 lines (50 loc) · 1.46 KB
/
databaseSetup.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
package common
import (
"context"
"fmt"
"log"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
//Client variable stores the mongoDB Connection
var Client *mongo.Client
//Collection variable stores the mongoDB collection
var Collection *mongo.Collection
//ConnectToDB Method connect to MongoDB
func ConnectToDB(connectionString, databaseName, collectionName string) error {
logger.Info().Msg("Connecting to Mongo DB....")
var err error
Client, err = mongo.NewClient(options.Client().ApplyURI(connectionString))
if err != nil {
log.Println(err)
return err
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = Client.Connect(ctx)
if err != nil {
log.Println(err)
return err
}
// defer client.Disconnect(ctx) //TODO: Need to close this connection
logger.Info().Msg("connected to mongoDB")
Collection = Client.Database(databaseName).Collection(collectionName)
return nil
}
//GetDBConnection Get the Database connection
func GetDBConnection() (*mongo.Collection, error) {
logger.Debug().Msg("Get DB Connection..")
// Check the connection
err := Client.Ping(context.TODO(), nil)
if err != nil {
return nil, err
}
return Collection, nil
}
//DisconnectDB to disconnect the DB connection
func DisconnectDB() {
logger.Debug().Msg("Disconnect DB Connection..")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
defer Client.Disconnect(ctx)
fmt.Println("DB disconnected!")
}