Skip to content

Commit 9829073

Browse files
authored
Merge pull request #49 from Kalissaac/add-connection-string
Add support for MongoDB connection string
2 parents 0db60a3 + ea23fab commit 9829073

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed

mongodb/README.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,30 @@ store := mongodb.New()
4141

4242
// Initialize custom config
4343
store := mongodb.New(mongodb.Config{
44-
Atlas: false,
4544
Host: "127.0.0.1",
4645
Port: 27017,
4746
Database: "fiber",
4847
Collection: "fiber_storage",
4948
Reset: false,
5049
})
50+
51+
// Initialize custom config using connection string
52+
store := mongodb.New(mongodb.Config{
53+
ConnectionURI: "mongodb://user:[email protected]:27017",
54+
Database: "fiber",
55+
Collection: "fiber_storage",
56+
Reset: false,
57+
})
58+
5159
```
5260

5361
### Config
5462
```go
5563
type Config struct {
56-
// Whether the DB is hosted on MongoDB Atlas
64+
// Connection string to use for DB. Will override all other authentication values if used
5765
//
58-
// Optional. Default is false
59-
Atlas bool
66+
// Optional. Default is ""
67+
ConnectionURI string
6068

6169
// Host name where the DB is hosted
6270
//
@@ -98,11 +106,11 @@ type Config struct {
98106
### Default Config
99107
```go
100108
var ConfigDefault = Config{
101-
Atlas: false,
102-
Host: "127.0.0.1",
103-
Port: 27017,
104-
Database: "fiber",
105-
Collection: "fiber_storage",
106-
Reset: false,
109+
ConnectionURI: "",
110+
Host: "127.0.0.1",
111+
Port: 27017,
112+
Database: "fiber",
113+
Collection: "fiber_storage",
114+
Reset: false,
107115
}
108116
```

mongodb/config.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package mongodb
22

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

1010
// Host name where the DB is hosted
1111
//
@@ -45,12 +45,12 @@ type Config struct {
4545

4646
// ConfigDefault is the default config
4747
var ConfigDefault = Config{
48-
Atlas: false,
49-
Host: "127.0.0.1",
50-
Port: 27017,
51-
Database: "fiber",
52-
Collection: "fiber_storage",
53-
Reset: false,
48+
ConnectionURI: "",
49+
Host: "127.0.0.1",
50+
Port: 27017,
51+
Database: "fiber",
52+
Collection: "fiber_storage",
53+
Reset: false,
5454
}
5555

5656
// Helper function to set default values

mongodb/mongodb.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,22 @@ func New(config ...Config) *Storage {
3333
cfg := configDefault(config...)
3434

3535
// Create data source name
36-
var dsn = "mongodb"
37-
if cfg.Atlas {
38-
dsn += "+srv://"
39-
} else {
40-
dsn += "://"
41-
}
42-
if cfg.Username != "" {
43-
dsn += url.QueryEscape(cfg.Username)
44-
}
45-
if cfg.Password != "" {
46-
dsn += ":" + cfg.Password
47-
}
48-
if cfg.Username != "" || cfg.Password != "" {
49-
dsn += "@"
50-
}
51-
// Cannot specify port when using MongoDB Atlas
52-
if cfg.Atlas {
53-
dsn += url.QueryEscape(cfg.Host)
36+
var dsn string
37+
38+
// Check if user supplied connection string
39+
if cfg.ConnectionURI != "" {
40+
dsn = cfg.ConnectionURI
5441
} else {
42+
dsn = "mongodb://"
43+
if cfg.Username != "" {
44+
dsn += url.QueryEscape(cfg.Username)
45+
}
46+
if cfg.Password != "" {
47+
dsn += ":" + cfg.Password
48+
}
49+
if cfg.Username != "" || cfg.Password != "" {
50+
dsn += "@"
51+
}
5552
dsn += fmt.Sprintf("%s:%d", url.QueryEscape(cfg.Host), cfg.Port)
5653
}
5754

0 commit comments

Comments
 (0)