mirror of
https://github.com/gofiber/storage.git
synced 2025-10-11 19:40:15 +08:00
Add ability to supply Mongo connection uri
This commit is contained in:
@@ -48,11 +48,25 @@ store := mongodb.New(mongodb.Config{
|
||||
Collection: "fiber_storage",
|
||||
Reset: false,
|
||||
})
|
||||
|
||||
// Initialize custom config using connection string
|
||||
store := mongodb.New(mongodb.Config{
|
||||
Connection: "mongodb://user:password@127.0.0.1:27017",
|
||||
Database: "fiber",
|
||||
Collection: "fiber_storage",
|
||||
Reset: false,
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
### Config
|
||||
```go
|
||||
type Config struct {
|
||||
// Connection string to use for DB. Will override all other authentication values if used
|
||||
//
|
||||
// Optional. Default is ""
|
||||
Connection string
|
||||
|
||||
// Whether the DB is hosted on MongoDB Atlas
|
||||
//
|
||||
// Optional. Default is false
|
||||
@@ -98,6 +112,7 @@ type Config struct {
|
||||
### Default Config
|
||||
```go
|
||||
var ConfigDefault = Config{
|
||||
Connection: "",
|
||||
Atlas: false,
|
||||
Host: "127.0.0.1",
|
||||
Port: 27017,
|
||||
|
@@ -2,6 +2,11 @@ package mongodb
|
||||
|
||||
// Config defines the config for storage.
|
||||
type Config struct {
|
||||
// Connection string to use for DB. Will override all other authentication values if used
|
||||
//
|
||||
// Optional. Default is ""
|
||||
Connection string
|
||||
|
||||
// Whether the DB is hosted on MongoDB Atlas
|
||||
//
|
||||
// Optional. Default is false
|
||||
@@ -45,6 +50,7 @@ type Config struct {
|
||||
|
||||
// ConfigDefault is the default config
|
||||
var ConfigDefault = Config{
|
||||
Connection: "",
|
||||
Atlas: false,
|
||||
Host: "127.0.0.1",
|
||||
Port: 27017,
|
||||
|
@@ -33,7 +33,13 @@ func New(config ...Config) *Storage {
|
||||
cfg := configDefault(config...)
|
||||
|
||||
// Create data source name
|
||||
var dsn = "mongodb"
|
||||
var dsn string
|
||||
|
||||
// Check if user supplied connection string
|
||||
if cfg.Connection != "" {
|
||||
dsn = cfg.Connection
|
||||
} else {
|
||||
dsn = "mongodb"
|
||||
if cfg.Atlas {
|
||||
dsn += "+srv://"
|
||||
} else {
|
||||
@@ -54,6 +60,7 @@ func New(config ...Config) *Storage {
|
||||
} else {
|
||||
dsn += fmt.Sprintf("%s:%d", url.QueryEscape(cfg.Host), cfg.Port)
|
||||
}
|
||||
}
|
||||
|
||||
// Set mongo options
|
||||
opt := options.Client()
|
||||
|
Reference in New Issue
Block a user