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