@@ -11,51 +11,50 @@ import (
1111	"go.mongodb.org/mongo-driver/mongo/options" 
1212)
1313
14- var  mongodb  * mongo.Database 
15- 
16- // GetDb returns the mongodb client. 
17- func  GetDb () * mongo.Database  {
18- 	return  mongodb 
19- }
14+ var  client  * mongo.Client 
2015
2116// GetClient returns the mongodb client. 
2217func  GetClient () * mongo.Client  {
23- 	return  mongodb . Client () 
18+ 	return  client 
2419}
2520
26- // ConnectDB initializes a MongoDB client and returns the database reference. 
27- func  ConnectDb (config  * MongodbConfig ) {
21+ // GetDb returns the mongodb client. 
22+ func  GetDb (dbName  string ) * mongo.Database  {
23+ 	return  client .Database (dbName )
24+ }
25+ 
26+ // Connect Mongodb function will initialize and connect to mongodb based on the URL, Port and Host passed via config 
27+ func  ConnectDb (config  * MongodbConfigV2 ) {
2828	// Get MongoDB URI from environment variable if set, otherwise use default 
2929	mongoDbUrl  :=  fmt .Sprintf ("mongodb://%s:%s@%s:%s" , config .Username , config .Password , config .Host , config .Port )
3030
3131	clientOpts  :=  options .Client ().ApplyURI (mongoDbUrl )
3232	ctx , cancel  :=  context .WithTimeout (context .Background (), 10 * time .Second )
3333	defer  cancel ()
3434
35- 	client , err  :=  mongo .Connect (ctx , clientOpts )
35+ 	var  err  error 
36+ 	client , err  =  mongo .Connect (ctx , clientOpts )
3637
3738	if  err  !=  nil  {
38- 		log .Fatalf ("Failed to connect to MongoDB: %v" , err )
39+ 		log .Fatalf ("❌  Failed to connect to MongoDB: %v" , err )
3940	}
4041
4142	// Verify connection 
4243	if  err  :=  client .Ping (ctx , nil ); err  !=  nil  {
43- 		log .Fatalf ("Failed to ping MongoDB: %v" , err )
44+ 		log .Fatalf ("❌  Failed to ping MongoDB: %v" , err )
4445	}
4546
46- 	log .Printf ("Connected to MongoDB on port: %s" , config .Port )
47- 
48- 	mongodb  =  client .Database (config .DbName )
47+ 	log .Printf ("✅ Connected to MongoDB on port: %s" , config .Port )
4948}
5049
5150// Create Collections 
5251func  CreateCollections (ctx  context.Context , dbName  string , collectionName  string ) {
5352	// List existing collections 
54- 	existingCollections , err  :=  GetDb ().ListCollectionNames (ctx , bson.D {})
53+ 	existingCollections , err  :=  GetDb (dbName ).ListCollectionNames (ctx , bson.D {})
5554	log .Printf ("List of Collections in %s: %v" , dbName , existingCollections )
5655
5756	if  err  !=  nil  {
58- 		log .Fatalf ("Failed to list collections in DB %s: %v" , dbName , err )
57+ 		log .Fatalf ("❌  Failed to list collections in DB %s: %v" , dbName , err )
5958	}
6059
6160	existingMap  :=  make (map [string ]bool )
@@ -66,12 +65,12 @@ func CreateCollections(ctx context.Context, dbName string, collectionName string
6665
6766	// Create only missing collections 
6867	if  ! existingMap [collectionName ] {
69- 		if  err  :=  GetDb ().CreateCollection (ctx , collectionName ); err  !=  nil  {
70- 			log .Fatalf ("Failed to create collection %s: %v" , collectionName , err )
68+ 		if  err  :=  GetDb (dbName ).CreateCollection (ctx , collectionName ); err  !=  nil  {
69+ 			log .Fatalf ("❌  Failed to create collection %s: %v" , collectionName , err )
7170		}
7271
73- 		log .Printf ("Created collection: %s" , collectionName )
72+ 		log .Printf ("✅  Created collection: %s" , collectionName )
7473	} else  {
75- 		log .Printf ("Collection already present: %s" , collectionName )
74+ 		log .Printf ("✅  Collection already present: %s" , collectionName )
7675	}
7776}
0 commit comments