-
Notifications
You must be signed in to change notification settings - Fork 0
/
gom.go
57 lines (43 loc) · 1.05 KB
/
gom.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 gom
import (
"context"
"errors"
"go.mongodb.org/mongo-driver/mongo"
"github.com/eaciit/toolkit"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
// Gom struct
type Gom struct {
mongo mongoDB
}
// NewGom = Create new
func NewGom() *Gom {
return new(Gom)
}
// Init = Init
func (g *Gom) Init(config Config) {
g.mongo.SetConfig(config)
g.mongo.SetClient()
}
// Set = Get set query with gom
func (g *Gom) Set(SetParams *SetParams) *Set {
s := newSet(g, SetParams)
return s
}
// CheckClient = Check connection successfull or not
func (g *Gom) CheckClient() error {
err := g.mongo.Client.Ping(context.Background(), readpref.Primary())
if err != nil {
return errors.New(toolkit.Sprintf("Couldn't connect to database : %s", err.Error()))
}
toolkit.Println(toolkit.Sprintf("Connected to database: %s", g.mongo.ConnectionString))
return nil
}
// GetClient = Get active client
func (g *Gom) GetClient() *mongo.Client {
return g.mongo.Client
}
// GetDatabase = Get database name
func (g *Gom) GetDatabase() string {
return g.mongo.Config.Database
}