diff --git a/mongodb/README.md b/mongodb/README.md index 6f7f500d..5322bb20 100644 --- a/mongodb/README.md +++ b/mongodb/README.md @@ -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:password@127.0.0.1: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 // @@ -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, } ``` diff --git a/mongodb/config.go b/mongodb/config.go index bcc1fed8..7e496bc8 100644 --- a/mongodb/config.go +++ b/mongodb/config.go @@ -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 // @@ -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 diff --git a/mongodb/mongodb.go b/mongodb/mongodb.go index 95797d10..b33c9974 100644 --- a/mongodb/mongodb.go +++ b/mongodb/mongodb.go @@ -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) }