-
Notifications
You must be signed in to change notification settings - Fork 33
/
mongodm.go
332 lines (243 loc) · 8.68 KB
/
mongodm.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
This package is an object document mapper for mongodb which uses the mgo adapter.
First step is to create a model, for example:
type User struct {
mongodm.DocumentBase `json:",inline" bson:",inline"`
FirstName string `json:"firstname" bson:"firstname"`
LastName string `json:"lastname" bson:"lastname"`
UserName string `json:"username" bson:"username"`
Messages interface{} `json:"messages" bson:"messages" model:"Message" relation:"1n" autosave:"true"`
}
It is important that each schema embeds the IDocumentBase type (mongodm.DocumentBase) and make sure that it is tagged as 'inline' for json and bson.
This base type also includes the default values id, createdAt, updatedAt and deleted. Those values are set automatically from the ODM.
The given example also uses a relation (User has Messages). Relations must always be from type interface{} for storing bson.ObjectId OR a completely
populated object. And of course we also need the related model for each stored message:
type Message struct {
mongodm.DocumentBase `json:",inline" bson:",inline"`
Sender string `json:"sender" bson:"sender"`
Receiver string `json:"receiver" bson:"receiver"`
Text string `json:"text" bson:"text"`
}
Note that when you are using relations, each model will be stored in his own collection. So the values are not embedded and instead stored as object ID
or array of object ID's.
To configure a relation the ODM understands three more tags:
model:"Message"
This must be the struct type you want to relate to.
Default: none, must be set
relation:"1n"
It is important that you specify the relation type one-to-one or one-to-many because the ODM must decide whether it accepts an array or object.
Possible: "1n", "11"
Default: "11"
autosave:"true"
If you manipulate values of the message relation in this example and then call 'Save()' on the user instance, this flag decides if this is possible or not.
When autosave is activated, all relations will also be saved recursively. Otherwise you have to call 'Save()' manually for each relation.
Possible: "true", "false"
Default: "false"
But it is not necessary to always create relations - you also can use embedded types:
type Customer struct {
mongodm.DocumentBase `json:",inline" bson:",inline"`
FirstName string `json:"firstname" bson:"firstname"`
LastName string `json:"lastname" bson:"lastname"`
Address *Address `json:"address" bson:"address"`
}
type Address struct {
City string `json:"city" bson:"city"`
Street string `json:"street" bson:"street"`
ZipCode int16 `json:"zip" bson:"zip"`
}
Persisting a customer instance to the database would result in embedding an complete address object. You can embed all supported types.
Now that you got some models it is important to create a connection to the database and to register these models for the ODM.
*/
package mongodm
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"path"
"reflect"
"runtime"
"strings"
"time"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
const REL_11 string = "11" // one-to-one relation
const REL_1N string = "1n" // one-to-many relation
var locals map[string]string
type (
//Simple config object which has to be passed/set to create a new connection
Config struct {
DatabaseHosts []string
DatabaseName string
DatabaseUser string
DatabasePassword string
DatabaseSource string
DialInfo *mgo.DialInfo
Locals map[string]string
}
//The "Database" object which stores all connections
Connection struct {
Config *Config
Session *mgo.Session
modelRegistry map[string]*Model
typeRegistry map[string]reflect.Type
}
//Interface which each collection document (model) hast to implement
IDocumentBase interface {
GetId() bson.ObjectId
SetId(bson.ObjectId)
SetCreatedAt(time.Time)
GetCreatedAt() time.Time
SetUpdatedAt(time.Time)
GetUpdatedAt() time.Time
SetDeleted(bool)
IsDeleted() bool
SetCollection(*mgo.Collection)
SetDocument(document IDocumentBase)
SetConnection(*Connection)
Save() error
Update(interface{}) (error, map[string]interface{})
Validate(...interface{}) (bool, []error)
DefaultValidate() (bool, []error)
}
)
/*
Use this method to connect to a mongo db instance. The only parameter which is expected is a *mongodm.Config object.
For example:
dbConfig := &mongodm.Config{
DatabaseHosts: []string{"localhost"},
DatabaseName: "mongodm_sample",
}
connection, err := mongodm.Connect(dbConfig)
if err != nil {
log.E("Database connection error: %v", err)
}
After this step you can register your created models. See: func (*Connection) Register
*/
func Connect(config *Config) (*Connection, error) {
con := &Connection{
Config: config,
Session: nil,
modelRegistry: make(map[string]*Model),
typeRegistry: make(map[string]reflect.Type),
}
if config.Locals == nil {
if _, filename, _, ok := runtime.Caller(0); ok {
filepath := path.Join(path.Dir(filename), "locals.json")
file, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
}
var localMap map[string]map[string]string
json.Unmarshal(file, &localMap)
locals = localMap["en-US"]
} else {
panic("No caller information to read default localisation file")
}
} else {
locals = config.Locals
}
err := con.Open()
return con, err
}
func (self *Connection) document(typeName string) IDocumentBase {
typeNameLC := strings.ToLower(typeName)
if _, ok := self.typeRegistry[typeNameLC]; ok {
reflectType := self.typeRegistry[typeNameLC]
document := reflect.New(reflectType).Interface().(IDocumentBase)
self.modelRegistry[typeNameLC].New(document)
return document
}
panic(fmt.Sprintf("DB: Type '%v' is not registered", typeName))
}
func L(key string, values ...interface{}) string {
if locals != nil {
if _, ok := locals[key]; ok {
return fmt.Sprintf(locals[key], values...)
}
}
return key
}
/*
To create actions on each collection you have to request a model instance with this method.
Make sure that you registered your collections and schemes first, otherwise it will panic.
For example:
User := connection.Model("User")
User.Find() ...
*/
func (self *Connection) Model(typeName string) *Model {
typeNameLC := strings.ToLower(typeName)
if _, ok := self.modelRegistry[typeNameLC]; ok {
return self.modelRegistry[typeNameLC]
}
panic(fmt.Sprintf("DB: Type '%v' is not registered", typeName))
}
/*
It is necessary to register your created models to the ODM to work with. Within this process
the ODM creates an internal model and type registry to work fully automatically and consistent.
Make sure you already created a connection. Registration expects a pointer to an IDocumentBase
type and the collection name where the documents should be stored in.
For example:
connection.Register(&User{}, "users")
connection.Register(&Message{}, "messages")
connection.Register(&Customer{}, "customers")
*/
func (self *Connection) Register(document IDocumentBase, collectionName string) {
if document == nil {
panic("document can not be nil")
}
reflectType := reflect.TypeOf(document)
typeName := strings.ToLower(reflectType.Elem().Name())
//check if model was already registered
if _, ok := self.modelRegistry[typeName]; !ok {
collection := self.Session.DB("").C(collectionName) // empty string returns db name from dial info
model := &Model{collection, self}
self.modelRegistry[typeName] = model
self.typeRegistry[typeName] = reflectType.Elem()
fmt.Sprintf("Registered type '%v' for collection '%v'", typeName, collectionName)
} else {
fmt.Sprintf("Tried to register type '%v' twice", typeName)
}
}
//Opens a database connection manually if the config was set.
//This method gets called automatically from the Connect() method.
func (self *Connection) Open() (err error) {
defer func() {
if r := recover(); r != nil {
if e, ok := r.(error); ok {
err = e
} else if e, ok := r.(string); ok {
err = errors.New(e)
} else {
err = errors.New(fmt.Sprint(r))
}
}
}()
var info *mgo.DialInfo
if self.Config.DialInfo == info {
info = &mgo.DialInfo{
Addrs: self.Config.DatabaseHosts,
Timeout: 3 * time.Second,
Database: self.Config.DatabaseName,
Username: self.Config.DatabaseUser,
Password: self.Config.DatabasePassword,
Source: self.Config.DatabaseSource,
}
} else {
info = self.Config.DialInfo
}
session, err := mgo.DialWithInfo(info)
if err != nil {
return err
}
self.Session = session
self.Session.SetMode(mgo.Monotonic, true)
return nil
}
//Closes an existing database connection
func (self *Connection) Close() {
if self.Session != nil {
self.Session.Close()
}
}