You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
797 B

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