-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect.go
57 lines (48 loc) · 1.45 KB
/
connect.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package mongo
//noinspection GoInvalidPackageImport
import (
"github.com/ddspog/mongo/internal/connecter"
"github.com/globalsign/mgo"
)
// Connecter represents method of connection to Mongo, using real
// database of a temporary database.
type Connecter = connecter.MongoConnecter
var (
// NewConnecter returns a new real database connecter.
NewConnecter = connecter.New
// NewTestableConnecter returns a temporary database connecter.
NewTestableConnecter = connecter.NewTestable
// conn connects with MongoDB
conn = NewConnecter()
)
// InitConnecter with the real database connecter, or with testable
// version if given as parameter.
func InitConnecter(c ...Connecter) {
conn = connecter.New()
if len(c) == 1 && c[0] != nil {
conn = c[0]
}
}
// Connect to MongoDB of server.
// It tries to connect with MONGODB_URL, but without defining this
// environment variable, tries to connect with default URL.
func Connect() (err error) {
err = conn.Connect()
return
}
// Disconnect undo the connection made. Preparing package for a new
// connection.
func Disconnect() {
conn.Disconnect()
}
// ConsumeDatabaseOnSession clones a session and use it to creates a
// Databaser object to be consumed in f function. Closes session after
// consume of Databaser object.
func ConsumeDatabaseOnSession(f func(*mgo.Database)) {
conn.ConsumeDatabaseOnSession(f)
}
// Session return connected mongo session.
func Session() (s *mgo.Session) {
s = conn.Session()
return
}