/
connections.go
37 lines (29 loc) · 982 Bytes
/
connections.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 repository
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
// NewMongoDatabase establish connection with a MongoDB database using the Configuration passed as parameter
func NewMongoDatabase(config Configuration) (mongoDatabase *mongo.Database, err error) {
mongoClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(config.String()))
if err != nil {
return
}
err = mongoClient.Ping(context.TODO(), readpref.Primary())
if err != nil {
return
}
mongoDatabase = mongoClient.Database(config.Database)
err = mongoDatabase.Client().Ping(context.TODO(), readpref.Primary())
return
}
// NewMD works same that function NewMongoDatabase but, it does not return an error instead it panics
func NewMD(config Configuration) *mongo.Database {
mongoDatabase, err := NewMongoDatabase(config)
if err != nil {
panic(err)
}
return mongoDatabase
}