-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmongo.go
More file actions
35 lines (28 loc) · 860 Bytes
/
mongo.go
File metadata and controls
35 lines (28 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package cycapi
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)
type MongoConn struct {
Conn *mongo.Client
}
// CreateMongoConnection Creates a DB Connection with the Database specified on the url
func CreateMongoConnection(driver, username, password, url string) (*MongoConn, error) {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(fmt.Sprintf("%s://%s:%s@%s", driver, username, password, url)))
if err != nil {
return nil, err
}
connection := MongoConn{
Conn: client,
}
return &connection, err
}
// CloseConnection Closes a DB Connection
func (db *MongoConn) CloseConnection() error {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
return db.Conn.Disconnect(ctx)
}