github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/documents/documentdb.go (about)

     1  package documents
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/mdaxf/iac/logger"
     8  )
     9  
    10  // DocDB is the interface for document database
    11  
    12  var DocDBCon *DocDB
    13  
    14  // ConnectDB is the function to connect to document database
    15  // DatabaseType: mongodb
    16  // DatabaseConnection: mongodb://localhost:27017
    17  // DatabaseName: iac
    18  
    19  func ConnectDB(DatabaseType string, DatabaseConnection string, DatabaseName string) {
    20  	iLog := logger.Log{ModuleName: logger.Database, User: "System", ControllerName: "DocumentDatabase"}
    21  	startTime := time.Now()
    22  	defer func() {
    23  		elapsed := time.Since(startTime)
    24  		iLog.PerformanceWithDuration("documents.ConnectDB", elapsed)
    25  	}()
    26  	/*
    27  		defer func() {
    28  			if err := recover(); err != nil {
    29  				iLog.Error(fmt.Sprintf("There is error to documents.ConnectDB with error: %s", err))
    30  				return
    31  			}
    32  		}()
    33  	*/
    34  	if DatabaseType == "mongodb" {
    35  		var err error
    36  		DocDBCon, err = InitMongoDB(DatabaseConnection, DatabaseName)
    37  
    38  		if err != nil {
    39  			iLog.Error(fmt.Sprintf("initialize Documents Database (MongoDB) error: %s", err.Error()))
    40  		}
    41  	}
    42  
    43  }