return mongo instance on connect (for multiple database connect)
This commit is contained in:
parent
110127e76a
commit
2761a05c01
|
@ -12,7 +12,7 @@ import (
|
||||||
var db *mongo.Database
|
var db *mongo.Database
|
||||||
|
|
||||||
// Connect to mongo server
|
// Connect to mongo server
|
||||||
func Connect(host, user, password, dbName, mechanism, source string) error {
|
func Connect(host, user, password, dbName, mechanism, source string) (*mongo.Database, error) {
|
||||||
connectOptions := options.ClientOptions{}
|
connectOptions := options.ClientOptions{}
|
||||||
// Set auth if existed
|
// Set auth if existed
|
||||||
if user != "" && password != "" {
|
if user != "" && password != "" {
|
||||||
|
@ -28,14 +28,14 @@ func Connect(host, user, password, dbName, mechanism, source string) error {
|
||||||
client, err := mongo.Connect(context.Background(), connectOptions.ApplyURI(host))
|
client, err := mongo.Connect(context.Background(), connectOptions.ApplyURI(host))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error when connect to MongoDB database", host, err)
|
fmt.Println("Error when connect to MongoDB database", host, err)
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(aurora.Green("*** CONNECTED TO MONGODB: " + host))
|
fmt.Println(aurora.Green("*** CONNECTED TO MONGODB: " + host))
|
||||||
|
|
||||||
// Set data
|
// Set data
|
||||||
db = client.Database(dbName)
|
db = client.Database(dbName)
|
||||||
return nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetInstance ...
|
// GetInstance ...
|
||||||
|
|
Loading…
Reference in New Issue