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.

30 lines
693 B

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