-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.go
37 lines (29 loc) · 882 Bytes
/
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
package mongo
import (
"context"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func Connect(context context.Context, uri string) (*mongo.Client, error) {
client, err := mongo.Connect(context, options.Client().ApplyURI(uri))
if err != nil {
return nil, err
}
log.Info("Successfully connected to the Database!")
return client, err
}
func Disconnect(context context.Context, client *mongo.Client) {
if err := client.Disconnect(context); err != nil {
log.Panic("Unable to disconnect from the database", err)
panic(err)
}
log.Info("Successfully disconnected from the Database.")
}
func Ping(client *mongo.Client) {
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
log.Panic(err)
}
log.Println("Pinged the database")
}