mirror of
https://github.com/gofiber/storage.git
synced 2025-10-06 09:07:14 +08:00
Allow reuse of an existing db connection when creating a MySQL Store
This commit is contained in:
@@ -51,8 +51,16 @@ store := mysql.New(mysql.Config{
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Initialize custom config using connection string
|
// Initialize custom config using connection string
|
||||||
store := postgres.New(postgres.Config{
|
store := mysql.New(mysql.Config{
|
||||||
ConnectionURI: "mysql://user:password@localhost:3306/fiber"
|
ConnectionURI: "<username>:<pw>@tcp(<HOST>:<port>)/<dbname>"
|
||||||
|
Reset: false,
|
||||||
|
GCInterval: 10 * time.Second,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Initialize custom config using sql db connection
|
||||||
|
db, _ := sql.Open("mysql", "<username>:<pw>@tcp(<HOST>:<port>)/<dbname>")
|
||||||
|
store := mysql.New(mysql.Config{
|
||||||
|
Db: db,
|
||||||
Reset: false,
|
Reset: false,
|
||||||
GCInterval: 10 * time.Second,
|
GCInterval: 10 * time.Second,
|
||||||
})
|
})
|
||||||
@@ -61,6 +69,11 @@ store := postgres.New(postgres.Config{
|
|||||||
### Config
|
### Config
|
||||||
```go
|
```go
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
// DB Will override ConnectionURI and all other authentication values if used
|
||||||
|
//
|
||||||
|
// Optional. Default is nil
|
||||||
|
Db *sql.DB
|
||||||
|
|
||||||
// Connection string to use for DB. Will override all other authentication values if used
|
// Connection string to use for DB. Will override all other authentication values if used
|
||||||
//
|
//
|
||||||
// Optional. Default is ""
|
// Optional. Default is ""
|
||||||
|
@@ -1,12 +1,18 @@
|
|||||||
package mysql
|
package mysql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config defines the config for storage.
|
// Config defines the config for storage.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
// DB Will override ConnectionURI and all other authentication values if used
|
||||||
|
//
|
||||||
|
// Optional. Default is nil
|
||||||
|
Db *sql.DB
|
||||||
|
|
||||||
// Connection string to use for DB. Will override all other authentication values if used
|
// Connection string to use for DB. Will override all other authentication values if used
|
||||||
//
|
//
|
||||||
// Optional. Default is ""
|
// Optional. Default is ""
|
||||||
@@ -63,6 +69,7 @@ type Config struct {
|
|||||||
|
|
||||||
// ConfigDefault is the default config
|
// ConfigDefault is the default config
|
||||||
var ConfigDefault = Config{
|
var ConfigDefault = Config{
|
||||||
|
Db: nil,
|
||||||
ConnectionURI: "",
|
ConnectionURI: "",
|
||||||
Host: "127.0.0.1",
|
Host: "127.0.0.1",
|
||||||
Port: 3306,
|
Port: 3306,
|
||||||
|
@@ -40,19 +40,27 @@ var (
|
|||||||
|
|
||||||
// New creates a new storage
|
// New creates a new storage
|
||||||
func New(config ...Config) *Storage {
|
func New(config ...Config) *Storage {
|
||||||
|
var err error
|
||||||
|
var db *sql.DB
|
||||||
|
|
||||||
// Set default config
|
// Set default config
|
||||||
cfg := configDefault(config...)
|
cfg := configDefault(config...)
|
||||||
|
|
||||||
// Create db
|
if cfg.Db != nil {
|
||||||
db, err := sql.Open("mysql", cfg.dsn())
|
// Use passed db
|
||||||
if err != nil {
|
db = cfg.Db
|
||||||
panic(err)
|
} else {
|
||||||
}
|
// Create db
|
||||||
|
db, err = sql.Open("mysql", cfg.dsn())
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
// Set options
|
// Set options
|
||||||
db.SetMaxOpenConns(cfg.maxOpenConns)
|
db.SetMaxOpenConns(cfg.maxOpenConns)
|
||||||
db.SetMaxIdleConns(cfg.maxIdleConns)
|
db.SetMaxIdleConns(cfg.maxIdleConns)
|
||||||
db.SetConnMaxLifetime(cfg.connMaxLifetime)
|
db.SetConnMaxLifetime(cfg.connMaxLifetime)
|
||||||
|
}
|
||||||
|
|
||||||
// Ping database to ensure a connection has been made
|
// Ping database to ensure a connection has been made
|
||||||
if err := db.Ping(); err != nil {
|
if err := db.Ping(); err != nil {
|
||||||
|
Reference in New Issue
Block a user