Skip to content

Commit e59c005

Browse files
committed
refac: mongo connection
1 parent 358cbb1 commit e59c005

3 files changed

Lines changed: 24 additions & 17 deletions

File tree

connections/database/interface.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"net/http"
66

7-
"go.mongodb.org/mongo-driver/mongo"
87
"gorm.io/gorm"
98
)
109

@@ -23,13 +22,12 @@ type DBConnection interface {
2322
}
2423

2524
type IDBQueryClient interface {
26-
Connect() error
2725
GetDB() interface{}
26+
Connect() error
2827
}
2928

3029
type DBQueryClient struct {
31-
Client *mongo.Client
32-
Params *DBConnectionParams
30+
Client IDBQueryClient
3331
}
3432

3533
type DBConnectionParams struct {

connections/database/mongoDB.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,46 @@ package database
33
import (
44
"context"
55
"fmt"
6-
"os"
76
"time"
87

98
"go.mongodb.org/mongo-driver/mongo"
109
"go.mongodb.org/mongo-driver/mongo/options"
1110
"go.mongodb.org/mongo-driver/mongo/readpref"
1211
)
1312

14-
func (c *DBQueryClient) Connect() error {
15-
uri := os.Getenv("MONGO_URI")
13+
type MongoConnection struct {
14+
db *mongo.Client
15+
}
16+
17+
func NewMongoConnection(params *DBConnectionParams) (*MongoConnection, error) {
18+
uri := params.URI
1619
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
1720
defer cancel()
1821

1922
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
2023
if err != nil {
21-
fmt.Println("Failed to connect to db")
24+
fmt.Printf("Failed to connect to db: %v \n", err)
25+
return nil, err
2226
}
2327

2428
// Ping the primary
2529
if err := client.Ping(ctx, readpref.Primary()); err != nil {
26-
fmt.Println("Failed to ping after connecting to the db")
30+
return nil, err
2731
}
2832
fmt.Println("Successfully connected and pinged.")
2933

30-
c.Client = client
31-
return err
34+
return &MongoConnection{db: client}, nil
35+
}
36+
37+
func (c *MongoConnection) GetDB() *mongo.Client {
38+
return c.db
3239
}
3340

34-
func (c *DBQueryClient) GetDB() *mongo.Client {
35-
err := c.Connect()
41+
func (c *DBQueryClient) Connect(params *DBConnectionParams) (*mongo.Client, error) {
42+
conn, err := NewMongoConnection(params)
3643
if err != nil {
37-
panic(err)
44+
return nil, err
3845
}
3946

40-
return c.Client
47+
return conn.GetDB(), nil
4148
}

connections/database/mongoDB_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package database
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"testing"
78

89
"go.mongodb.org/mongo-driver/bson"
@@ -12,13 +13,14 @@ import (
1213

1314
func TestBuffer_InsertRead(t *testing.T) {
1415
var c DBQueryClient
15-
err := c.Connect()
16+
params := &DBConnectionParams{URI: os.Getenv("MONGO_URI")}
17+
client, err := c.Connect(params)
1618
if err != nil {
1719
fmt.Println(err)
1820
t.Fail()
1921
}
2022

21-
coll := c.Client.Database("test").Collection("sample")
23+
coll := client.Database("test").Collection("sample")
2224
res, err := coll.InsertOne(context.TODO(), bson.D{{Key: "name", Value: "Alice"}})
2325
if err != nil {
2426
fmt.Println(err)

0 commit comments

Comments
 (0)