-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
38 lines (32 loc) · 818 Bytes
/
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
package main
import (
"context"
"fmt"
"log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var dbClient *mongo.Client
func initDB() {
DbURI := getEnvVariable(ENV_DB_URI)
client, err := mongo.NewClient(options.Client().ApplyURI(DbURI))
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), dbConnectTimeout)
defer cancel()
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to Database!")
dbClient = client
}
func getCollection(collectionName string) *mongo.Collection {
dbName := getEnvVariable(ENV_DB_NAME)
collectionJobs := dbClient.Database(dbName).Collection(collectionName)
return collectionJobs
}
func getJobsCollectionName() string {
return getEnvVariable(ENV_COLLECTION_NAME)
}