github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/documentdb/mongodb/mongodb.go (about) 1 package mongodb 2 3 import ( 4 "context" 5 "fmt" 6 "sync" 7 "time" 8 9 // "go.mongodb.org/mongo-driver/bson" 10 // "go.mongodb.org/mongo-driver/bson/primitive" 11 "go.mongodb.org/mongo-driver/mongo" 12 "go.mongodb.org/mongo-driver/mongo/options" 13 14 "github.com/mdaxf/iac/com" 15 "github.com/mdaxf/iac/logger" 16 ) 17 18 // DocDB is the interface for document database 19 type MyDocDB com.DocDB 20 21 var once sync.Once 22 23 // Connect establishes a connection to the document database. 24 // It returns an error if the connection fails. 25 func (db *MyDocDB) Connect() error { 26 // Function execution logging 27 iLog := logger.Log{ModuleName: logger.Framework, User: "System", ControllerName: "DocumentDatabase.Connect"} 28 startTime := time.Now() 29 defer func() { 30 elapsed := time.Since(startTime) 31 iLog.PerformanceWithDuration("documentdb.Connect", elapsed) 32 }() 33 34 // Recover from any panics and log the error 35 defer func() { 36 if err := recover(); err != nil { 37 iLog.Error(fmt.Sprintf("Connect Document Database defer error: %s", err)) 38 } 39 }() 40 41 // Log the database connection details 42 iLog.Info(fmt.Sprintf("Connect Document Database: %s %s", db.DatabaseType, db.DatabaseConnection)) 43 44 // Establish the database connection if it hasn't been done before 45 once.Do(func() { 46 // Set client options 47 clientOptions := options.Client().ApplyURI(db.DatabaseConnection) 48 49 // Connect to MongoDB 50 client, err := mongo.Connect(context.Background(), clientOptions) 51 if err != nil { 52 iLog.Error(fmt.Sprintf("Connect Document Database Error: %s", err.Error())) 53 return 54 } 55 56 // Check the connection 57 err = client.Ping(context.Background(), nil) 58 if err != nil { 59 iLog.Error(fmt.Sprintf("Connect Document Database Error: %s", err.Error())) 60 return 61 } 62 63 db.MongoDBClient = client 64 db.MongoDBDatabase = db.MongoDBClient.Database(db.DatabaseName) 65 66 }) 67 68 return nil 69 } 70 71 // Disconnect closes the connection to the document database. 72 // It returns an error if the disconnection fails. 73 func (db *MyDocDB) Disconnect() error { 74 // Function execution logging 75 iLog := logger.Log{ModuleName: logger.Framework, User: "System", ControllerName: "DocumentDatabase.Disconnect"} 76 startTime := time.Now() 77 defer func() { 78 elapsed := time.Since(startTime) 79 iLog.PerformanceWithDuration("documentdb.Disconnect", elapsed) 80 }() 81 82 // Recover from any panics and log the error 83 defer func() { 84 if err := recover(); err != nil { 85 iLog.Error(fmt.Sprintf("Disconnect Document Database defer error: %s", err)) 86 } 87 }() 88 89 // Log the database disconnection details 90 iLog.Info(fmt.Sprintf("Disconnect Document Database: %s %s", db.DatabaseType, db.DatabaseConnection)) 91 92 // Disconnect from MongoDB 93 err := db.MongoDBClient.Disconnect(context.Background()) 94 if err != nil { 95 iLog.Error(fmt.Sprintf("Disconnect Document Database Error: %s", err.Error())) 96 } 97 98 return err 99 } 100 101 // ReConnect reconnects to the document database. 102 // It returns an error if the reconnection fails. 103 func (db *MyDocDB) ReConnect() error { 104 // Function execution logging 105 iLog := logger.Log{ModuleName: logger.Framework, User: "System", ControllerName: "DocumentDatabase.ReConnect"} 106 startTime := time.Now() 107 defer func() { 108 elapsed := time.Since(startTime) 109 iLog.PerformanceWithDuration("documentdb.ReConnect", elapsed) 110 }() 111 112 // Recover from any panics and log the error 113 defer func() { 114 if err := recover(); err != nil { 115 iLog.Error(fmt.Sprintf("ReConnect Document Database defer error: %s", err)) 116 } 117 }() 118 119 // Log the database reconnection details 120 iLog.Info(fmt.Sprintf("ReConnect Document Database: %s %s", db.DatabaseType, db.DatabaseConnection)) 121 122 // Reconnect to MongoDB 123 err := db.Connect() 124 if err != nil { 125 iLog.Error(fmt.Sprintf("ReConnect Document Database Error: %s", err.Error())) 126 } 127 128 return err 129 } 130 131 func (db *MyDocDB) Ping() error { 132 // Function execution logging 133 iLog := logger.Log{ModuleName: logger.Framework, User: "System", ControllerName: "DocumentDatabase.Ping"} 134 startTime := time.Now() 135 defer func() { 136 elapsed := time.Since(startTime) 137 iLog.PerformanceWithDuration("documentdb.Ping", elapsed) 138 }() 139 140 // Recover from any panics and log the error 141 defer func() { 142 if err := recover(); err != nil { 143 iLog.Error(fmt.Sprintf("Ping Document Database defer error: %s", err)) 144 } 145 }() 146 147 // Ping the database 148 err := db.MongoDBClient.Ping(context.Background(), nil) 149 if err != nil { 150 iLog.Error(fmt.Sprintf("Ping Document Database Error: %s", err.Error())) 151 return err 152 } 153 154 return nil 155 }