Skip to content

Commit

Permalink
Merge pull request #49 from Kalissaac/add-connection-string
Browse files Browse the repository at this point in the history
Add support for MongoDB connection string
  • Loading branch information
Fenny committed Dec 14, 2020
2 parents 0db60a3 + ea23fab commit 9829073
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
28 changes: 18 additions & 10 deletions mongodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,30 @@ store := mongodb.New()

// Initialize custom config
store := mongodb.New(mongodb.Config{
Atlas: false,
Host: "127.0.0.1",
Port: 27017,
Database: "fiber",
Collection: "fiber_storage",
Reset: false,
})

// Initialize custom config using connection string
store := mongodb.New(mongodb.Config{
ConnectionURI: "mongodb://user:[email protected]:27017",
Database: "fiber",
Collection: "fiber_storage",
Reset: false,
})

```

### Config
```go
type Config struct {
// Whether the DB is hosted on MongoDB Atlas
// Connection string to use for DB. Will override all other authentication values if used
//
// Optional. Default is false
Atlas bool
// Optional. Default is ""
ConnectionURI string

// Host name where the DB is hosted
//
Expand Down Expand Up @@ -98,11 +106,11 @@ type Config struct {
### Default Config
```go
var ConfigDefault = Config{
Atlas: false,
Host: "127.0.0.1",
Port: 27017,
Database: "fiber",
Collection: "fiber_storage",
Reset: false,
ConnectionURI: "",
Host: "127.0.0.1",
Port: 27017,
Database: "fiber",
Collection: "fiber_storage",
Reset: false,
}
```
18 changes: 9 additions & 9 deletions mongodb/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package mongodb

// Config defines the config for storage.
type Config struct {
// Whether the DB is hosted on MongoDB Atlas
// Connection string to use for DB. Will override all other authentication values if used
//
// Optional. Default is false
Atlas bool
// Optional. Default is ""
ConnectionURI string

// Host name where the DB is hosted
//
Expand Down Expand Up @@ -45,12 +45,12 @@ type Config struct {

// ConfigDefault is the default config
var ConfigDefault = Config{
Atlas: false,
Host: "127.0.0.1",
Port: 27017,
Database: "fiber",
Collection: "fiber_storage",
Reset: false,
ConnectionURI: "",
Host: "127.0.0.1",
Port: 27017,
Database: "fiber",
Collection: "fiber_storage",
Reset: false,
}

// Helper function to set default values
Expand Down
33 changes: 15 additions & 18 deletions mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,22 @@ func New(config ...Config) *Storage {
cfg := configDefault(config...)

// Create data source name
var dsn = "mongodb"
if cfg.Atlas {
dsn += "+srv://"
} else {
dsn += "://"
}
if cfg.Username != "" {
dsn += url.QueryEscape(cfg.Username)
}
if cfg.Password != "" {
dsn += ":" + cfg.Password
}
if cfg.Username != "" || cfg.Password != "" {
dsn += "@"
}
// Cannot specify port when using MongoDB Atlas
if cfg.Atlas {
dsn += url.QueryEscape(cfg.Host)
var dsn string

// Check if user supplied connection string
if cfg.ConnectionURI != "" {
dsn = cfg.ConnectionURI
} else {
dsn = "mongodb://"
if cfg.Username != "" {
dsn += url.QueryEscape(cfg.Username)
}
if cfg.Password != "" {
dsn += ":" + cfg.Password
}
if cfg.Username != "" || cfg.Password != "" {
dsn += "@"
}
dsn += fmt.Sprintf("%s:%d", url.QueryEscape(cfg.Host), cfg.Port)
}

Expand Down

0 comments on commit 9829073

Please sign in to comment.