-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
46 lines (40 loc) · 957 Bytes
/
storage.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 mongo
import (
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/pkg/errors"
)
// Storage supply a interface for make Queryset.
type Storage interface {
Query(Collection) QuerySet
Raw(Collection, bson.M) QuerySet
}
type mongoStorage struct {
sess *mgo.Session
}
// NewStorage create a new mongodb storage.
func NewStorage(sess *mgo.Session) Storage {
return &mongoStorage{
sess: sess,
}
}
func (ms *mongoStorage) Query(query Collection) QuerySet {
qs := &mongoQuerySet{
sess: ms.sess,
database: query.Database(),
collection: query.Collection(),
}
realQuery, err := structToBsonM(query)
qs.err = errors.Wrap(err, "convert to bson.M failed")
qs.query = realQuery
return qs
}
func (ms *mongoStorage) Raw(base Collection, query bson.M) QuerySet {
qs := &mongoQuerySet{
sess: ms.sess,
database: base.Database(),
collection: base.Collection(),
query: query,
}
return qs
}