Skip to content

Commit

Permalink
feat: Add support for custom DialInfo (#12, #15)
Browse files Browse the repository at this point in the history
* Add support for custom DialInfo

Lets users provide their own DialInfo if they need to specify more configuration than mongodm's config allows

* Fix minor syntax error

* chore: Update readme for custom dialinfo and add test

* chore: Add reference link to mgo.DialInfo
  • Loading branch information
moehlone authored Feb 7, 2018
1 parent e159d08 commit ebb881e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 10 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,42 @@ Load your localisation file and parse it until you get a `map[string]string` typ
DatabaseName: "mongodm_sample",
DatabaseUser: "admin",
DatabasePassword: "admin",
// The option `DatabaseSource` is the database used to establish
// credentials and privileges with a MongoDB server. Defaults to the value
// of `DatabaseName`, if that is set, or "admin" otherwise.
DatabaseSource: "admin",
Locals: localMap["en-US"],
}

connection, err := mongodm.Connect(dbConfig)

if err != nil {
fmt.Println("Database connection error: %v", err)
}
```

You can also pass a custom DialInfo from mgo ([`*mgo.DialInfo`](https://godoc.org/labix.org/v2/mgo#DialInfo)). If used, all config attributes starting with `Database` will be ignored:

```go
file, err := ioutil.ReadFile("locals.json")

if err != nil {
fmt.Printf("File error: %v\n", err)
os.Exit(1)
}

var localMap map[string]map[string]string
json.Unmarshal(file, &localMap)

dbConfig := &mongodm.Config{
DialInfo: &mgo.DialInfo{
Addrs: []string{"127.0.0.1"},
Timeout: 3 * time.Second,
Database: "mongodm_sample",
Username: "admin",
Password: "admin",
Source: "admin",
},
Locals: localMap["en-US"],
}

Expand Down
23 changes: 15 additions & 8 deletions mongodm.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ type (
DatabaseName string
DatabaseUser string
DatabasePassword string
Source string
DatabaseSource string
DialInfo *mgo.DialInfo
Locals map[string]string
}

Expand Down Expand Up @@ -295,13 +296,19 @@ func (self *Connection) Open() (err error) {
}
}()

info := &mgo.DialInfo{
Addrs: self.Config.DatabaseHosts,
Timeout: 3 * time.Second,
Database: self.Config.DatabaseName,
Username: self.Config.DatabaseUser,
Password: self.Config.DatabasePassword,
Source: self.Config.Source,
var info *mgo.DialInfo

if self.Config.DialInfo == info {
info = &mgo.DialInfo{
Addrs: self.Config.DatabaseHosts,
Timeout: 3 * time.Second,
Database: self.Config.DatabaseName,
Username: self.Config.DatabaseUser,
Password: self.Config.DatabasePassword,
Source: self.Config.DatabaseSource,
}
} else {
info = self.Config.DialInfo
}

session, err := mgo.DialWithInfo(info)
Expand Down
31 changes: 29 additions & 2 deletions mongodm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"testing"

mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)

Expand All @@ -16,6 +17,7 @@ const (
DBName string = "mongodm_test"
DBUser string = "admin"
DBPass string = "admin"
DBSource string = "admin"
DBTestCollection string = "_testCollection"
DBTestRelCollection string = "_testRelationCollection"
)
Expand Down Expand Up @@ -76,8 +78,8 @@ func TestConnection(t *testing.T) {
// with a MongoDB server. Defaults to the value of Database, if that is
// set, or "admin" otherwise.
// see https://godoc.org/labix.org/v2/mgo#DialInfo
Source: "admin",
Locals: localMap["en-US"],
DatabaseSource: "admin",
Locals: localMap["en-US"],
}

db, err := Connect(dbConfig)
Expand All @@ -101,6 +103,31 @@ func TestConnection(t *testing.T) {
}
}

func TestConnectionWithCustomDialInfo(t *testing.T) {

var localMap map[string]map[string]string
json.Unmarshal(localsFile, &localMap)

dbConfig := &Config{
DialInfo: &mgo.DialInfo{
Addrs: []string{DBHost},
Database: DBName,
Username: DBUser,
Password: DBPass,
Source: DBSource,
},
Locals: localMap["en-US"],
}

_, err := Connect(dbConfig)

if err != nil {

t.Error("DB: Connection error", err)

}
}

func TestConnectionWithoutExtendedConfig(t *testing.T) {

dbConfig := &Config{
Expand Down

0 comments on commit ebb881e

Please sign in to comment.