Skip to content

Added 2 function and one struct #566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: v2-unstable
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions session_wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package mgo

import (
"gopkg.in/mgo.v2"
)

type Auth struct {
Username string
Password string
Url string
DatabaseName string
Mode mgo.Mode
Refresh bool
}

func MongoConnection(credential Auth) (*mgo.Session, *mgo.Database, error) {
session, _ := mgo.Dial(credential.Url)

db := session.DB(credential.DatabaseName)
err := db.Login(credential.Username, credential.Password)
if err != nil {
return nil, nil, err
}
session.SetMode(credential.Mode, credential.Refresh)
return session, db, nil
}

func UseScheme(db *mgo.Database, nameScheme string) *mgo.Collection {

return db.C(nameScheme)
}


/*
READ ME
To use this wrapper, you have to create a structure, which will be the subject of the MongoConnection function,


MongoConnection returns three values; the first one is the connection session to the * mgo.Session type database, the second is a * mgo.Database, and third will not be nil if you have got an error.


The UseScheme function takes an mgo.Database type (the second argument returned by MongoConnection) and returns a * mgo.Collection


*/