Add Conn() support to all storage drivers. (#451)

* Add DB() support for Redis driver

* Added support for DB() to all drivers

* Fixed typo in README and Lint issue

* Fix lint issue with ristretto db

* Fix lint issue with bbolt db

* Rename DB() to Conn()

* Replace all instances of _DB with _Conn

* Update all the README files

* Return ArangoDB Client instead of DB
This commit is contained in:
Juan Calderon-Perez
2022-08-15 01:58:13 -04:00
committed by GitHub
parent c38c925975
commit 3a8b8d4f71
40 changed files with 138 additions and 8 deletions

View File

@@ -57,6 +57,9 @@ type Storage interface {
* [Badger](/badger) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Local+Storage%22"> * [Badger](/badger) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Local+Storage%22">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Local%20Storage?label=%F0%9F%A7%AA%20&style=flat&color=75C46B"> <img src="https://img.shields.io/github/workflow/status/gofiber/storage/Local%20Storage?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a> </a>
* [Bbolt](/bbolt) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Bbolt%22">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Bbolt?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>
* [DynamoDB](/dynamodb) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22DynamoDB%22"> * [DynamoDB](/dynamodb) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22DynamoDB%22">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/DynamoDB?label=%F0%9F%A7%AA%20&style=flat&color=75C46B"> <img src="https://img.shields.io/github/workflow/status/gofiber/storage/DynamoDB?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a> </a>
@@ -83,7 +86,4 @@ type Storage interface {
</a> </a>
* [S3](/s3) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22S3%22"> * [S3](/s3) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22S3%22">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/S3?label=%F0%9F%A7%AA%20&style=flat&color=75C46B"> <img src="https://img.shields.io/github/workflow/status/gofiber/storage/S3?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a>
* [Bbolt](/bbolt) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Bbolt%22">
<img src="https://img.shields.io/github/workflow/status/gofiber/storage/Bbolt?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
</a> </a>

View File

@@ -16,6 +16,7 @@ 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) Conn() driver.Client
``` ```
### Installation ### Installation
ArangoDB is tested on the 2 last (1.14/1.15) [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet: ArangoDB is tested on the 2 last (1.14/1.15) [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:

View File

@@ -246,3 +246,8 @@ func (s *Storage) gc() {
} }
} }
} }
// Return database client
func (s *Storage) Conn() driver.Client {
return s.client
}

View File

@@ -132,3 +132,7 @@ func Test_ARANGODB_Non_UTF8(t *testing.T) {
func Test_ARANGODB_Close(t *testing.T) { func Test_ARANGODB_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close()) utils.AssertEqual(t, nil, testStore.Close())
} }
func Test_ARANGODB_Conn(t *testing.T) {
utils.AssertEqual(t, true, testStore.Conn() != nil)
}

View File

@@ -19,6 +19,7 @@ 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) Conn() *badger.DB
``` ```
### Installation ### Installation

View File

@@ -123,3 +123,8 @@ func (s *Storage) gc() {
} }
} }
} }
// Return database client
func (s *Storage) Conn() *badger.DB {
return s.db
}

View File

@@ -119,3 +119,7 @@ func Test_Badger_Reset(t *testing.T) {
func Test_Badger_Close(t *testing.T) { func Test_Badger_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close()) utils.AssertEqual(t, nil, testStore.Close())
} }
func Test_Badger_Conn(t *testing.T) {
utils.AssertEqual(t, true, testStore.Conn() != nil)
}

View File

@@ -17,6 +17,7 @@ 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) Conn() *bbolt.DB
``` ```
### Installation ### Installation
Bbolt 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: Bbolt 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:

View File

@@ -104,3 +104,8 @@ func (s *Storage) Reset() error {
func (s *Storage) Close() error { func (s *Storage) Close() error {
return s.conn.Close() return s.conn.Close()
} }
// Return database client
func (s *Storage) Conn() *bbolt.DB {
return s.conn
}

View File

@@ -95,3 +95,7 @@ func Test_Bbolt_Reset(t *testing.T) {
func Test_Bbolt_Close(t *testing.T) { func Test_Bbolt_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close()) utils.AssertEqual(t, nil, testStore.Close())
} }
func Test_Bbolt_Conn(t *testing.T) {
utils.AssertEqual(t, true, testStore.Conn() != nil)
}

View File

@@ -23,6 +23,7 @@ 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) Conn() *awsdynamodb.Client
``` ```
### Installation ### Installation

View File

@@ -264,3 +264,8 @@ func returnAWSConfig(cfg Config) (aws.Config, error) {
}), }),
) )
} }
// Return database client
func (s *Storage) Conn() *awsdynamodb.Client {
return s.db
}

View File

@@ -105,3 +105,7 @@ func Test_DynamoDB_Reset(t *testing.T) {
func Test_DynamoDB_Close(t *testing.T) { func Test_DynamoDB_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close()) utils.AssertEqual(t, nil, testStore.Close())
} }
func Test_DynamoDB_Conn(t *testing.T) {
utils.AssertEqual(t, true, testStore.Conn() != nil)
}

View File

@@ -17,6 +17,7 @@ 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) Conn() *mc.Client
``` ```
### Installation ### Installation

View File

@@ -122,3 +122,8 @@ func (s *Storage) releaseItem(item *mc.Item) {
s.items.Put(item) s.items.Put(item)
} }
} }
// Return database client
func (s *Storage) Conn() *mc.Client {
return s.db
}

View File

@@ -119,3 +119,7 @@ func Test_Memcache_Reset(t *testing.T) {
func Test_Memcache_Close(t *testing.T) { func Test_Memcache_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close()) utils.AssertEqual(t, nil, testStore.Close())
} }
func Test_Memcache_Conn(t *testing.T) {
utils.AssertEqual(t, true, testStore.Conn() != nil)
}

View File

@@ -18,6 +18,7 @@ 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) Conn() map[string]entry
``` ```
### Installation ### Installation

View File

@@ -116,3 +116,8 @@ func (s *Storage) gc() {
} }
} }
} }
// Return database client
func (s *Storage) Conn() map[string]entry {
return s.db
}

View File

@@ -119,3 +119,7 @@ func Test_Memory_Reset(t *testing.T) {
func Test_Memory_Close(t *testing.T) { func Test_Memory_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close()) utils.AssertEqual(t, nil, testStore.Close())
} }
func Test_Memory_Conn(t *testing.T) {
utils.AssertEqual(t, true, testStore.Conn() != nil)
}

View File

@@ -17,6 +17,7 @@ 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) Conn() *mongo.Database
``` ```
### Installation ### Installation
MongoDB 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: MongoDB 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:

View File

@@ -199,3 +199,8 @@ func (s *Storage) releaseItem(item *item) {
s.items.Put(item) s.items.Put(item)
} }
} }
// Return database client
func (s *Storage) Conn() *mongo.Database {
return s.db
}

View File

@@ -121,3 +121,7 @@ func Test_MongoDB_Reset(t *testing.T) {
func Test_MongoDB_Close(t *testing.T) { func Test_MongoDB_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close()) utils.AssertEqual(t, nil, testStore.Close())
} }
func Test_MongoDB_Conn(t *testing.T) {
utils.AssertEqual(t, true, testStore.Conn() != nil)
}

View File

@@ -17,6 +17,7 @@ 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) Conn() *sql.DB
``` ```
### Installation ### Installation
MySQL 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: MySQL 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:

View File

@@ -165,6 +165,11 @@ func (s *Storage) Close() error {
return s.db.Close() return s.db.Close()
} }
// Return database client
func (s *Storage) Conn() *sql.DB {
return s.db
}
// gcTicker starts the gc ticker // gcTicker starts the gc ticker
func (s *Storage) gcTicker() { func (s *Storage) gcTicker() {
ticker := time.NewTicker(s.gcInterval) ticker := time.NewTicker(s.gcInterval)

View File

@@ -162,3 +162,7 @@ func Test_MYSQL_Non_UTF8(t *testing.T) {
func Test_MYSQL_Close(t *testing.T) { func Test_MYSQL_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close()) utils.AssertEqual(t, nil, testStore.Close())
} }
func Test_MYSQL_Conn(t *testing.T) {
utils.AssertEqual(t, true, testStore.Conn() != nil)
}

View File

@@ -17,6 +17,7 @@ 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) Conn() *sql.DB
``` ```
### 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:

View File

@@ -189,6 +189,11 @@ func (s *Storage) Close() error {
return s.db.Close() return s.db.Close()
} }
// Return database client
func (s *Storage) Conn() *sql.DB {
return s.db
}
// gcTicker starts the gc ticker // gcTicker starts the gc ticker
func (s *Storage) gcTicker() { func (s *Storage) gcTicker() {
ticker := time.NewTicker(s.gcInterval) ticker := time.NewTicker(s.gcInterval)

View File

@@ -177,3 +177,7 @@ func Test_SslRequiredMode(t *testing.T) {
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_Conn(t *testing.T) {
utils.AssertEqual(t, true, testStore.Conn() != nil)
}

View File

@@ -17,6 +17,7 @@ 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) Conn() *redis.Client
``` ```
### Installation ### Installation
Redis 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: Redis 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:
@@ -86,11 +87,11 @@ type Config struct {
// Optional. Default is 0 // Optional. Default is 0
Database int Database int
// URL the standard format redis url to parse all other options. If this is set all other config options, Host, Port, Username, Password, Database have no effect. // URL the standard format redis url to parse all other options. If this is set all other config options, Host, Port, Username, Password, Database have no effect.
// //
// Example: redis://<user>:<pass>@localhost:6379/<db> // Example: redis://<user>:<pass>@localhost:6379/<db>
// Optional. Default is "" // Optional. Default is ""
URL string URL string
// Reset clears any existing keys in existing Collection // Reset clears any existing keys in existing Collection
// //

View File

@@ -99,3 +99,8 @@ func (s *Storage) Reset() error {
func (s *Storage) Close() error { func (s *Storage) Close() error {
return s.db.Close() return s.db.Close()
} }
// Return database client
func (s *Storage) Conn() *redis.Client {
return s.db
}

View File

@@ -123,6 +123,10 @@ func Test_Redis_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close()) utils.AssertEqual(t, nil, testStore.Close())
} }
func Test_Redis_Conn(t *testing.T) {
utils.AssertEqual(t, true, testStore.Conn() != nil)
}
func Test_Redis_Initalize_WithURL(t *testing.T) { func Test_Redis_Initalize_WithURL(t *testing.T) {
testStoreUrl := New(Config{ testStoreUrl := New(Config{
URL: "redis://localhost:6379", URL: "redis://localhost:6379",

View File

@@ -18,6 +18,7 @@ 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) Conn() *ristretto.Cache
``` ```
### Installation ### Installation

View File

@@ -89,3 +89,8 @@ func (s *Storage) Close() error {
s.cache.Close() s.cache.Close()
return nil return nil
} }
// Return database client
func (s *Storage) Conn() *ristretto.Cache {
return s.cache
}

View File

@@ -126,3 +126,7 @@ func Test_Ristretto_Reset(t *testing.T) {
func Test_Ristretto_Close(t *testing.T) { func Test_Ristretto_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close()) utils.AssertEqual(t, nil, testStore.Close())
} }
func Test_Ristretto_Conn(t *testing.T) {
utils.AssertEqual(t, true, testStore.Conn() != nil)
}

View File

@@ -20,6 +20,7 @@ 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) Conn() *s3.Client
``` ```
### Installation ### Installation
S3 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: S3 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:

View File

@@ -159,6 +159,11 @@ func (s *Storage) Close() error {
return nil return nil
} }
// Return database client
func (s *Storage) Conn() *s3.Client {
return s.svc
}
// Context for making requests will timeout if a non-zero timeout is configured // Context for making requests will timeout if a non-zero timeout is configured
func (s *Storage) requestContext() (context.Context, context.CancelFunc) { func (s *Storage) requestContext() (context.Context, context.CancelFunc) {
if s.requestTimeout > 0 { if s.requestTimeout > 0 {

View File

@@ -105,3 +105,7 @@ func Test_S3_Reset(t *testing.T) {
func Test_S3_Close(t *testing.T) { func Test_S3_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close()) utils.AssertEqual(t, nil, testStore.Close())
} }
func Test_S3_Conn(t *testing.T) {
utils.AssertEqual(t, true, testStore.Conn() != nil)
}

View File

@@ -17,6 +17,7 @@ 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) Conn() *sql.DB
``` ```
### Installation ### Installation
SQLite3 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: SQLite3 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:

View File

@@ -169,3 +169,8 @@ func (s *Storage) gcTicker() {
func (s *Storage) gc(t time.Time) { func (s *Storage) gc(t time.Time) {
_, _ = s.db.Exec(s.sqlGC, t.Unix()) _, _ = s.db.Exec(s.sqlGC, t.Unix())
} }
// Return database client
func (s *Storage) Conn() *sql.DB {
return s.db
}

View File

@@ -159,3 +159,7 @@ func Test_SQLite3_Non_UTF8(t *testing.T) {
func Test_SQLite3_Close(t *testing.T) { func Test_SQLite3_Close(t *testing.T) {
utils.AssertEqual(t, nil, testStore.Close()) utils.AssertEqual(t, nil, testStore.Close())
} }
func Test_SQLite3_Conn(t *testing.T) {
utils.AssertEqual(t, true, testStore.Conn() != nil)
}