-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
55 lines (46 loc) · 1.33 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
package grf
import (
"context"
"log"
"os"
"time"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Connects to mongodb using details fetched form the env variables.
func SetupDatabase() (*mongo.Database, func(), error) {
connectionString, dbname := GetDBDetails(false)
return getDBConnection(connectionString, dbname)
}
func SetupTestDatabase() (*mongo.Database, func(), error) {
connectionString, dbname := GetDBDetails(true)
return getDBConnection(connectionString, dbname)
}
func getDBConnection(connectionString, dbname string) (*mongo.Database, func(), error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(connectionString))
disconnect := func() {
cancel()
if err := client.Disconnect(ctx); err != nil {
panic(err)
}
}
db := client.Database(dbname)
return db, disconnect, err
}
// Loads the env file and gets the connection string and db name.
// Takes a boolean value to specificy whether to use test db or not.
func GetDBDetails(test bool) (uri, dbname string) {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
uri = os.Getenv("DATABASE_URI")
if test {
dbname = "test_db"
} else {
dbname = os.Getenv("DATABASE_NAME")
}
return
}