-
Notifications
You must be signed in to change notification settings - Fork 2
/
mgo.go
51 lines (43 loc) · 809 Bytes
/
mgo.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
package ifth
import (
"github.com/globalsign/mgo"
"github.com/pkg/errors"
)
type DB interface {
Dial(dsn string) error
Connect() (interface{}, error)
}
type Mgo struct {
Session *mgo.Session
}
var Client DB
func InitMgo(dsn string) (DB, error) {
Client = &Mgo{}
err := Client.Dial(dsn)
if err != nil {
return nil, err
}
return Client, nil
}
func GetMgo() *mgo.Session {
session, err := Client.Connect()
if err != nil {
return nil
}
return session.(*mgo.Session)
}
func (m *Mgo) Dial(dsn string) error {
session, err := mgo.Dial(dsn)
if err != nil {
return err
}
m.Session = session
return nil
}
func (m *Mgo) Connect() (interface{}, error) {
if m.Session == nil {
return nil, errors.New("doesn't connect to mongo db")
}
session := m.Session.Copy()
return session, nil
}