-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.go
82 lines (73 loc) · 3.36 KB
/
model.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package Sleep
import (
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
// Model struct represents a collection in MongoDB.
// It inherits from mgo.Collection and overwrides the functions below to provide more
// functionality and to create compatibility with Sleep.
type Model struct {
*mgo.Collection
//C is the underlying mgo.collection value for this model.
//Refer to http://godoc.org/labix.org/v2/mgo#Collection for full usage information
C *mgo.Collection
z *Sleep
}
func newModel(collection *mgo.Collection, z *Sleep) *Model {
model := &Model{collection, collection, z}
return model
}
// CreateDoc conditions an instance of the model to become a document.
//
// What it means in pratical terms is that Create sets a value for the schema's *Sleep.Document anonymous field. This will allow Sleep to work with the value as a document.
// Calling this function is only necessary when wishing to create documents "manually".
// It is not necessary to call this function on a value that will be holding the result of a query; Sleep will do that.
//
// After a document is created with this function, the document will expose all of the public methods and fields of the Sleep.Model struct as its own.
func (m *Model) CreateDoc(i interface{}) {
m.z.CreateDoc(i)
}
// Find starts and returns a chainable *Query value
// This function passes the passed value to mgo.Collection.Find
//
// To borrow from the mgo docs: "The document(argument) may be a map or a struct value capable of being marshalled with bson.
// The map may be a generic one using interface{} for its key and/or values, such as bson.M, or it may be a properly typed map.
// Providing nil as the document is equivalent to providing an empty document such as bson.M{}".
//
// Further reading: http://godoc.org/labix.org/v2/mgo#Collection.Find
func (m *Model) Find(query interface{}) *Query {
return &Query{query: query, z: m.z,
populate: make(map[string]*Query),
populated: make(map[string]interface{}), c: m.C}
}
// FindId is a convenience function equivalent to:
//
// query := myModel.Find(bson.M{"_id": id})
//
// Unlike the Mgo.Collection.FindId function, this function will accept Id both in hex representation as a string or a bson.ObjectId.
//
// FindId will return a chainable *Query value
func (m *Model) FindId(id interface{}) *Query {
return m.Find(bson.M{"_id": getObjectId(id)})
}
// RemoveId removes a document from the collection based on its _id field.
// Same as mgo.Collection.RemoveId, except that it accepts the Id as a string or bson.ObjectId
//
// See http://godoc.org/labix.org/v2/mgo#Collection.RemoveId
func (m *Model) RemoveId(id interface{}) error {
return m.C.RemoveId(getObjectId(id))
}
// UpdateId updates a document in the collection based on its _id field.
// Same as mgo.Collection.UpdateId, except that it accepts the Id as a string or bson.ObjectId
//
// See http://godoc.org/labix.org/v2/mgo#Collection.UpdateId
func (m *Model) UpdateId(id interface{}, change interface{}) error {
return m.C.UpdateId(getObjectId(id), change)
}
// UpsertId updates or inserts a document in the collection based on its _id field.
// Same as mgo.Collection.UpsertId, except that it accepts the Id as a string or bson.ObjectId
//
// See http://godoc.org/labix.org/v2/mgo#Collection.UpsertId
func (m *Model) UpsertId(id interface{}, change interface{}) (*mgo.ChangeInfo, error) {
return m.C.UpsertId(getObjectId(id), change)
}