-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_mongo.go
46 lines (38 loc) · 960 Bytes
/
db_mongo.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
package app
import (
"errors"
"github.com/cnlisea/ant/app/db/mongo"
)
func (a *App) DBMongoRegister(name string,
user string, password string,
addr []string, dbName string,
replicaSet string, connTimeout int,
active int, idle int, idleTimeout int) error {
if a.mongo != nil && a.mongo[name] != nil {
return ErrMongoInstanceAlreadyExisted
}
client, err := mongo.NewClient(a.ctx, user, password, addr, dbName, replicaSet, connTimeout, active, idle, idleTimeout)
if err != nil {
return errors.New("mongo client new fail, err:" + err.Error())
}
if a.mongo == nil {
a.mongo = make(map[string]*mongo.Client, 1)
}
a.mongo[name] = client
return nil
}
func (a *App) DBMongoInstance(name ...string) *mongo.Client {
if a.mongo == nil {
return nil
}
if name != nil && len(name) > 0 {
return a.mongo[name[0]]
}
return a.mongo[""]
}
func (a *App) DBMongoAllClose() {
ctx := a.Context()
for _, c := range a.mongo {
c.Close(ctx)
}
}