Skip to content

Instantly share code, notes, and snippets.

@vuongngo
Created September 4, 2017 10:28
Show Gist options
  • Save vuongngo/2b263e4521570f0e1c4e20f6686e012d to your computer and use it in GitHub Desktop.
Save vuongngo/2b263e4521570f0e1c4e20f6686e012d to your computer and use it in GitHub Desktop.
package db
import (
"errors"
"gopkg.in/mgo.v2"
)
/*
Mongo construction
*/
//Mongo struct hold uri based on environment
type MONGO struct {
Uri string
Database string
Session *mgo.Session
}
//Establish connection to mongodb
func (mongo *MONGO) Dial() {
session, err := mgo.Dial(mongo.Uri)
if err != nil {
panic(err)
}
mongo.Session = session
}
// Get session
func (mongo *MONGO) GetSession() (*mgo.Database, *mgo.Session) {
if mongo.Session == nil {
panic(errors.New("Db session not exist"))
return nil, nil
}
newSession := mongo.Session.Copy()
db := newSession.DB(mongo.Database)
return db, newSession
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment