mirror of
https://github.com/gofiber/storage.git
synced 2025-09-28 13:22:18 +08:00
Modified storage to take in a pgxpool.Pool object
This commit is contained in:
@@ -17,7 +17,6 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error
|
|||||||
func (s *Storage) Delete(key string) error
|
func (s *Storage) Delete(key string) error
|
||||||
func (s *Storage) Reset() error
|
func (s *Storage) Reset() error
|
||||||
func (s *Storage) Close() error
|
func (s *Storage) Close() error
|
||||||
func (s *Storage) DB() pgxpool.Pool
|
|
||||||
```
|
```
|
||||||
### Installation
|
### Installation
|
||||||
Postgres is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
Postgres is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
|
||||||
@@ -42,13 +41,10 @@ store := postgres.New()
|
|||||||
|
|
||||||
// Initialize custom config
|
// Initialize custom config
|
||||||
store := postgres.New(postgres.Config{
|
store := postgres.New(postgres.Config{
|
||||||
Host: "127.0.0.1",
|
Db: dbPool,
|
||||||
Port: 5432,
|
|
||||||
Database: "fiber",
|
|
||||||
Table: "fiber_storage",
|
Table: "fiber_storage",
|
||||||
Reset: false,
|
Reset: false,
|
||||||
GCInterval: 10 * time.Second,
|
GCInterval: 10 * time.Second,
|
||||||
SslMode: "disable",
|
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -56,30 +52,10 @@ store := postgres.New(postgres.Config{
|
|||||||
```go
|
```go
|
||||||
// Config defines the config for storage.
|
// Config defines the config for storage.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
// Host name where the DB is hosted
|
// Db pgxpool.Pool object
|
||||||
//
|
//
|
||||||
// Optional. Default is "127.0.0.1"
|
// Required
|
||||||
Host string
|
Db pgxpool.Pool
|
||||||
|
|
||||||
// Port where the DB is listening on
|
|
||||||
//
|
|
||||||
// Optional. Default is 5432
|
|
||||||
Port int
|
|
||||||
|
|
||||||
// Server username
|
|
||||||
//
|
|
||||||
// Optional. Default is ""
|
|
||||||
Username string
|
|
||||||
|
|
||||||
// Server password
|
|
||||||
//
|
|
||||||
// Optional. Default is ""
|
|
||||||
Password string
|
|
||||||
|
|
||||||
// Database name
|
|
||||||
//
|
|
||||||
// Optional. Default is "fiber"
|
|
||||||
Database string
|
|
||||||
|
|
||||||
// Table name
|
// Table name
|
||||||
//
|
//
|
||||||
@@ -95,23 +71,16 @@ type Config struct {
|
|||||||
//
|
//
|
||||||
// Optional. Default is 10 * time.Second
|
// Optional. Default is 10 * time.Second
|
||||||
GCInterval time.Duration
|
GCInterval time.Duration
|
||||||
|
|
||||||
// The SSL mode for the connection
|
|
||||||
//
|
|
||||||
// Optional. Default is "disable"
|
|
||||||
SslMode string
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Default Config
|
### Default Config
|
||||||
```go
|
```go
|
||||||
|
// ConfigDefault is the default config
|
||||||
var ConfigDefault = Config{
|
var ConfigDefault = Config{
|
||||||
Host: "127.0.0.1",
|
Db: pgxpool.Pool{},
|
||||||
Port: 5432,
|
|
||||||
Database: "fiber",
|
|
||||||
Table: "fiber_storage",
|
Table: "fiber_storage",
|
||||||
Reset: false,
|
Reset: false,
|
||||||
GCInterval: 10 * time.Second,
|
GCInterval: 10 * time.Second,
|
||||||
SslMode: "disable",
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@@ -1,46 +1,22 @@
|
|||||||
package postgres
|
package postgres
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/jackc/pgx/pgxpool"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config defines the config for storage.
|
// Config defines the config for storage.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
// Host name where the DB is hosted
|
// Db pgxpool.Pool object
|
||||||
//
|
//
|
||||||
// Optional. Default is "127.0.0.1"
|
// Required
|
||||||
Host string
|
Db pgxpool.Pool
|
||||||
|
|
||||||
// Port where the DB is listening on
|
|
||||||
//
|
|
||||||
// Optional. Default is 5432
|
|
||||||
Port int
|
|
||||||
|
|
||||||
// Server username
|
|
||||||
//
|
|
||||||
// Optional. Default is ""
|
|
||||||
Username string
|
|
||||||
|
|
||||||
// Server password
|
|
||||||
//
|
|
||||||
// Optional. Default is ""
|
|
||||||
Password string
|
|
||||||
|
|
||||||
// Database name
|
|
||||||
//
|
|
||||||
// Optional. Default is "fiber"
|
|
||||||
Database string
|
|
||||||
|
|
||||||
// Table name
|
// Table name
|
||||||
//
|
//
|
||||||
// Optional. Default is "fiber_storage"
|
// Optional. Default is "fiber_storage"
|
||||||
Table string
|
Table string
|
||||||
|
|
||||||
// The SSL mode for the connection
|
|
||||||
//
|
|
||||||
// Optional. Default is "disable"
|
|
||||||
SslMode string
|
|
||||||
|
|
||||||
// Reset clears any existing keys in existing Table
|
// Reset clears any existing keys in existing Table
|
||||||
//
|
//
|
||||||
// Optional. Default is false
|
// Optional. Default is false
|
||||||
@@ -50,44 +26,13 @@ type Config struct {
|
|||||||
//
|
//
|
||||||
// Optional. Default is 10 * time.Second
|
// Optional. Default is 10 * time.Second
|
||||||
GCInterval time.Duration
|
GCInterval time.Duration
|
||||||
|
|
||||||
////////////////////////////////////
|
|
||||||
// Adaptor related config options //
|
|
||||||
////////////////////////////////////
|
|
||||||
|
|
||||||
// Maximum wait for connection, in seconds. Zero or
|
|
||||||
// n < 0 means wait indefinitely.
|
|
||||||
timeout time.Duration
|
|
||||||
|
|
||||||
// The maximum number of open connections to the database.
|
|
||||||
//
|
|
||||||
// If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than
|
|
||||||
// MaxIdleConns, then MaxIdleConns will be reduced to match the new
|
|
||||||
// MaxOpenConns limit.
|
|
||||||
//
|
|
||||||
// If n <= 0, then there is no limit on the number of open connections.
|
|
||||||
// The default is 0 (unlimited).
|
|
||||||
maxOpenConns int32
|
|
||||||
|
|
||||||
// The maximum amount of time a connection may be reused.
|
|
||||||
//
|
|
||||||
// Expired connections may be closed lazily before reuse.
|
|
||||||
//
|
|
||||||
// If d <= 0, connections are reused forever.
|
|
||||||
connMaxLifetime time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConfigDefault is the default config
|
// ConfigDefault is the default config
|
||||||
var ConfigDefault = Config{
|
var ConfigDefault = Config{
|
||||||
Host: "127.0.0.1",
|
|
||||||
Port: 5432,
|
|
||||||
Database: "fiber",
|
|
||||||
Table: "fiber_storage",
|
Table: "fiber_storage",
|
||||||
SslMode: "disable",
|
|
||||||
Reset: false,
|
Reset: false,
|
||||||
GCInterval: 10 * time.Second,
|
GCInterval: 10 * time.Second,
|
||||||
maxOpenConns: 100,
|
|
||||||
connMaxLifetime: 1 * time.Second,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to set default values
|
// Helper function to set default values
|
||||||
@@ -96,26 +41,13 @@ func configDefault(config ...Config) Config {
|
|||||||
if len(config) < 1 {
|
if len(config) < 1 {
|
||||||
return ConfigDefault
|
return ConfigDefault
|
||||||
}
|
}
|
||||||
|
|
||||||
// Override default config
|
// Override default config
|
||||||
cfg := config[0]
|
cfg := config[0]
|
||||||
|
|
||||||
// Set default values
|
// Set default values
|
||||||
if cfg.Host == "" {
|
|
||||||
cfg.Host = ConfigDefault.Host
|
|
||||||
}
|
|
||||||
if cfg.Port <= 0 {
|
|
||||||
cfg.Port = ConfigDefault.Port
|
|
||||||
}
|
|
||||||
if cfg.Database == "" {
|
|
||||||
cfg.Database = ConfigDefault.Database
|
|
||||||
}
|
|
||||||
if cfg.Table == "" {
|
if cfg.Table == "" {
|
||||||
cfg.Table = ConfigDefault.Table
|
cfg.Table = ConfigDefault.Table
|
||||||
}
|
}
|
||||||
if cfg.SslMode == "" {
|
|
||||||
cfg.SslMode = ConfigDefault.SslMode
|
|
||||||
}
|
|
||||||
if int(cfg.GCInterval.Seconds()) <= 0 {
|
if int(cfg.GCInterval.Seconds()) <= 0 {
|
||||||
cfg.GCInterval = ConfigDefault.GCInterval
|
cfg.GCInterval = ConfigDefault.GCInterval
|
||||||
}
|
}
|
||||||
|
@@ -4,16 +4,14 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"github.com/jackc/pgx/v4/pgxpool"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v4/pgxpool"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Storage interface that is implemented by storage providers
|
// Storage interface that is implemented by storage providers
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
db *pgxpool.Pool
|
db pgxpool.Pool
|
||||||
gcInterval time.Duration
|
gcInterval time.Duration
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
|
|
||||||
@@ -45,34 +43,7 @@ func New(config ...Config) *Storage {
|
|||||||
// Set default config
|
// Set default config
|
||||||
cfg := configDefault(config...)
|
cfg := configDefault(config...)
|
||||||
|
|
||||||
// Create data source name
|
db := cfg.Db
|
||||||
var dsn = "postgresql://"
|
|
||||||
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)
|
|
||||||
dsn += fmt.Sprintf("/%s?connect_timeout=%d&sslmode=%s",
|
|
||||||
url.QueryEscape(cfg.Database),
|
|
||||||
int64(cfg.timeout.Seconds()),
|
|
||||||
cfg.SslMode,
|
|
||||||
)
|
|
||||||
|
|
||||||
cnf, _ := pgxpool.ParseConfig(dsn)
|
|
||||||
cnf.MaxConns = cfg.maxOpenConns
|
|
||||||
cnf.MaxConnLifetime = cfg.connMaxLifetime
|
|
||||||
cnf.MaxConnIdleTime = cfg.connMaxLifetime
|
|
||||||
|
|
||||||
db, err := pgxpool.ConnectConfig(context.Background(), cnf)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
// Ping database
|
// Ping database
|
||||||
if err := db.Ping(context.Background()); err != nil {
|
if err := db.Ping(context.Background()); err != nil {
|
||||||
@@ -81,7 +52,7 @@ func New(config ...Config) *Storage {
|
|||||||
|
|
||||||
// Drop table if set to true
|
// Drop table if set to true
|
||||||
if cfg.Reset {
|
if cfg.Reset {
|
||||||
if _, err = db.Exec(context.Background(), fmt.Sprintf(dropQuery, cfg.Table)); err != nil {
|
if _, err := db.Exec(context.Background(), fmt.Sprintf(dropQuery, cfg.Table)); err != nil {
|
||||||
db.Close()
|
db.Close()
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -210,7 +181,3 @@ func (s *Storage) checkSchema(tableName string) {
|
|||||||
fmt.Printf(checkSchemaMsg, string(data))
|
fmt.Printf(checkSchemaMsg, string(data))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) DB() *pgxpool.Pool {
|
|
||||||
return s.db
|
|
||||||
}
|
|
||||||
|
@@ -3,7 +3,6 @@ package postgres
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -11,9 +10,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var testStore = New(Config{
|
var testStore = New(Config{
|
||||||
Database: os.Getenv("POSTGRES_DATABASE"),
|
|
||||||
Username: os.Getenv("POSTGRES_USERNAME"),
|
|
||||||
Password: os.Getenv("POSTGRES_PASSWORD"),
|
|
||||||
Reset: true,
|
Reset: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -167,18 +163,10 @@ func Test_SslRequiredMode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
_ = New(Config{
|
_ = New(Config{
|
||||||
Database: "fiber",
|
|
||||||
Username: "username",
|
|
||||||
Password: "password",
|
|
||||||
Reset: true,
|
Reset: true,
|
||||||
SslMode: "require",
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Postgres_Close(t *testing.T) {
|
func Test_Postgres_Close(t *testing.T) {
|
||||||
utils.AssertEqual(t, nil, testStore.Close())
|
utils.AssertEqual(t, nil, testStore.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Postgres_DB(t *testing.T) {
|
|
||||||
utils.AssertEqual(t, true, testStore.DB() != nil)
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user