gogame-old/db/db.go
db/db.goRaw
1package database
2
3import mgo "gopkg.in/mgo.v2"
4
5type Db struct {
6 Users *mgo.Collection
7 Planets *mgo.Collection
8 Galaxies *mgo.Collection
9}
10
11func New(url string, databaseName string) (*Db, error) {
12 session, err := mgo.Dial("mongodb://" + url)
13 if err != nil {
14 return nil, err
15 }
16 //defer session.Close()
17
18 // Optional. Switch the session to a monotonic behavior.
19 session.SetMode(mgo.Monotonic, true)
20
21 // Optional. Switch the session to a monotonic behavior.
22 session.SetMode(mgo.Monotonic, true)
23
24 db := Db{}
25 db.Users = session.DB(databaseName).C("users")
26 db.Planets = session.DB(databaseName).C("planets")
27 db.Galaxies = session.DB(databaseName).C("galaxies")
28
29 return &db, nil
30}