Add Postgres and MySQL connection strings (#429)

* Add ConnectionURI to Postgres

* Add ConnectionURI to MySQL

* Update Postgres sample db name
This commit is contained in:
Kalissaac
2022-08-14 22:48:29 -07:00
committed by GitHub
parent d27ddd4739
commit c38c925975
5 changed files with 64 additions and 18 deletions

View File

@@ -48,11 +48,23 @@ store := mysql.New(mysql.Config{
Reset: false,
GCInterval: 10 * time.Second,
})
// Initialize custom config using connection string
store := postgres.New(postgres.Config{
ConnectionURI: "mysql://user:password@localhost:3306/fiber"
Reset: false,
GCInterval: 10 * time.Second,
})
```
### Config
```go
type Config struct {
// Connection string to use for DB. Will override all other authentication values if used
//
// Optional. Default is ""
ConnectionURI string
// Host name where the DB is hosted
//
// Optional. Default is "127.0.0.1"
@@ -98,6 +110,7 @@ type Config struct {
### Default Config
```go
var ConfigDefault = Config{
ConnectionURI: "",
Host: "127.0.0.1",
Port: 3306,
Database: "fiber",

View File

@@ -7,6 +7,11 @@ import (
// 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 ""
ConnectionURI string
// Host name where the DB is hosted
//
// Optional. Default is "127.0.0.1"
@@ -58,6 +63,7 @@ type Config struct {
// ConfigDefault is the default config
var ConfigDefault = Config{
ConnectionURI: "",
Host: "127.0.0.1",
Port: 3306,
Database: "fiber",
@@ -70,6 +76,9 @@ var ConfigDefault = Config{
}
func (c Config) dsn() string {
if c.ConnectionURI != "" {
return c.ConnectionURI
}
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", c.Username, c.Password, c.Host, c.Port, c.Database)
}