Skip to content

Commit 51dcea1

Browse files
committed
feat: added pgxpool Config, Handler and Service interfaces and implementations
* Added pgxpool Config, Handler and Service interfaces and implementations * Remove redundant code * Improved config structs * Upgraded dependencies
1 parent c3e7644 commit 51dcea1

27 files changed

+752
-227
lines changed

errors.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ package go_databases
33
import "errors"
44

55
var (
6-
ErrNilDatabase = errors.New("database cannot be nil")
7-
ErrAlreadyConnected = errors.New("connection to database already established")
8-
ErrConnectionFailed = errors.New("failed to connect to database")
9-
ErrPingFailed = errors.New("failed to ping database")
10-
ErrNotConnected = errors.New("connection to database not established")
11-
ErrFailedToDisconnect = errors.New("failed to disconnect from database")
6+
ErrNilConfig = errors.New("config cannot be nil")
7+
ErrNilConnection = errors.New("connection cannot be nil")
8+
ErrNilPool = errors.New("pool cannot be nil")
9+
ErrAlreadyConnected = errors.New("connection to database already established")
10+
ErrConnectionFailed = errors.New("failed to connect to database")
11+
ErrPingFailed = errors.New("failed to ping database")
12+
ErrNotConnected = errors.New("connection to database not established")
13+
ErrFailedToDisconnect = errors.New("failed to disconnect from database")
14+
ErrEmptyDriverName = errors.New("driver name cannot be empty")
15+
ErrEmptyDataSourceName = errors.New("data source name cannot be empty")
16+
ErrNilQuery = errors.New("sql query cannot be nil")
17+
ErrNilRow = errors.New("sql row cannot be nil")
1218
)

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.23.4
55
require (
66
github.com/go-redis/redis/v8 v8.11.5
77
github.com/jackc/pgx/v5 v5.7.2
8-
github.com/ralvarezdev/go-logger v0.4.5
8+
github.com/ralvarezdev/go-logger v0.4.6
99
go.mongodb.org/mongo-driver v1.17.2
1010
golang.org/x/net v0.34.0
1111
gorm.io/gorm v1.25.12
@@ -17,12 +17,13 @@ require (
1717
github.com/golang/snappy v0.0.4 // indirect
1818
github.com/jackc/pgpassfile v1.0.0 // indirect
1919
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
20+
github.com/jackc/puddle/v2 v2.2.2 // indirect
2021
github.com/jinzhu/inflection v1.0.0 // indirect
2122
github.com/jinzhu/now v1.1.5 // indirect
2223
github.com/klauspost/compress v1.16.7 // indirect
2324
github.com/montanaflynn/stats v0.7.1 // indirect
24-
github.com/ralvarezdev/go-flags v0.3.1 // indirect
25-
github.com/ralvarezdev/go-strings v0.1.0 // indirect
25+
github.com/ralvarezdev/go-flags v0.3.2 // indirect
26+
github.com/ralvarezdev/go-strings v0.1.7 // indirect
2627
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
2728
github.com/xdg-go/scram v1.1.2 // indirect
2829
github.com/xdg-go/stringprep v1.0.4 // indirect

go.sum

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7Ulw
1919
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
2020
github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI=
2121
github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
22+
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
23+
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
2224
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
2325
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
2426
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
@@ -35,12 +37,12 @@ github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
3537
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
3638
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3739
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
38-
github.com/ralvarezdev/go-flags v0.3.1 h1:YxGYUonmr0dwJL+9oDY/4GqCl1CYwYDJExpGXXMc9rU=
39-
github.com/ralvarezdev/go-flags v0.3.1/go.mod h1:R3yVBYvzwqfOp26LidaiJ/zftVAnPC3pKunVpV/vosE=
40-
github.com/ralvarezdev/go-logger v0.4.5 h1:Lw7DdZUb4YSQDA7J9nv79soojw1eFHVVzO8wdE9d+Qg=
41-
github.com/ralvarezdev/go-logger v0.4.5/go.mod h1:vq47onxpED19/o3dh5/YbBKK0eZhqJENPgACYkqNNi8=
42-
github.com/ralvarezdev/go-strings v0.1.0 h1:bE4eEkdfsHwpgUw+s8/EdLPkRTPki/FTGZx8iEOOQBQ=
43-
github.com/ralvarezdev/go-strings v0.1.0/go.mod h1:8sFOqmPJpqzS7bTjf91EzUCITnwpmkfifwY80GxV5r8=
40+
github.com/ralvarezdev/go-flags v0.3.2 h1:l3f62CD5NysLAJH5XqSRGiakU/dn4pTGdX4yaBlAmMw=
41+
github.com/ralvarezdev/go-flags v0.3.2/go.mod h1:R3yVBYvzwqfOp26LidaiJ/zftVAnPC3pKunVpV/vosE=
42+
github.com/ralvarezdev/go-logger v0.4.6 h1:eZalm0WAdNuhzbaVh5D7gC6AFztdpqE/GQfjH2pipyE=
43+
github.com/ralvarezdev/go-logger v0.4.6/go.mod h1:zxuMDHqtV2eoJYtmo219lima0ZSk4p/AGj/cCc390P8=
44+
github.com/ralvarezdev/go-strings v0.1.7 h1:rO+4TJmHJTghRWl4Nq6gfnGSyNRerunJrkS03x1SLz4=
45+
github.com/ralvarezdev/go-strings v0.1.7/go.mod h1:8sFOqmPJpqzS7bTjf91EzUCITnwpmkfifwY80GxV5r8=
4446
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4547
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
4648
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

mongodb/collection.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// Collection represents a MongoDB collection
1010
type Collection struct {
11-
Name string
11+
name string
1212
Indexes *[]*mongo.IndexModel
1313
}
1414

@@ -18,8 +18,8 @@ func NewCollection(
1818
indexes *[]*mongo.IndexModel,
1919
) *Collection {
2020
return &Collection{
21-
Name: name,
22-
Indexes: indexes,
21+
name,
22+
indexes,
2323
}
2424
}
2525

@@ -28,7 +28,7 @@ func (c *Collection) CreateCollection(database *mongo.Database) (
2828
collection *mongo.Collection, err error,
2929
) {
3030
// Get the collection
31-
collection = database.Collection(c.Name)
31+
collection = database.Collection(c.name)
3232

3333
// Create the indexes
3434
if err = c.createIndexes(collection); err != nil {

mongodb/config.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package mongodb
2+
3+
import (
4+
"time"
5+
)
6+
7+
type (
8+
// Config interface
9+
Config interface {
10+
URI() string
11+
Timeout() time.Duration
12+
}
13+
14+
// ConnConfig struct
15+
ConnConfig struct {
16+
uri string
17+
timeout time.Duration
18+
}
19+
)
20+
21+
// NewConnConfig creates a new MongoDB connection configuration
22+
func NewConnConfig(uri string, timeout time.Duration) *ConnConfig {
23+
return &ConnConfig{
24+
uri,
25+
timeout,
26+
}
27+
}
28+
29+
// URI returns the MongoDB URI
30+
func (c *ConnConfig) URI() string {
31+
return c.uri
32+
}
33+
34+
// Timeout returns the MongoDB connection timeout
35+
func (c *ConnConfig) Timeout() time.Duration {
36+
return c.timeout
37+
}

mongodb/connection.go

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,55 @@ import (
55
"go.mongodb.org/mongo-driver/mongo"
66
"go.mongodb.org/mongo-driver/mongo/options"
77
"golang.org/x/net/context"
8-
"time"
98
)
109

1110
type (
12-
// ConnectionHandler interface
13-
ConnectionHandler interface {
11+
// ConnHandler interface
12+
ConnHandler interface {
1413
Connect() (*mongo.Client, error)
15-
GetClient() (*mongo.Client, error)
14+
Client() (*mongo.Client, error)
1615
Disconnect()
1716
}
1817

19-
// Config struct
20-
Config struct {
21-
Uri string
22-
Timeout time.Duration
23-
}
24-
25-
// DefaultConnectionHandler struct
26-
DefaultConnectionHandler struct {
27-
Ctx context.Context
28-
Cancel context.CancelFunc
29-
ClientOptions *options.ClientOptions
30-
Client *mongo.Client
18+
// DefaultConnHandler struct
19+
DefaultConnHandler struct {
20+
ctx context.Context
21+
cancel context.CancelFunc
22+
clientOptions *options.ClientOptions
23+
client *mongo.Client
3124
}
3225
)
3326

34-
// NewDefaultConnectionHandler creates a new connection
35-
func NewDefaultConnectionHandler(config *Config) (
36-
*DefaultConnectionHandler,
27+
// NewDefaultConnHandler creates a new connection
28+
func NewDefaultConnHandler(config Config) (
29+
*DefaultConnHandler,
3730
error,
3831
) {
3932
// Check if the config is nil
4033
if config == nil {
41-
return nil, ErrNilClient
34+
return nil, godatabases.ErrNilConfig
4235
}
4336

4437
// Set client options
45-
ctx, cancel := context.WithTimeout(context.Background(), config.Timeout)
46-
clientOptions := options.Client().ApplyURI(config.Uri)
47-
48-
return &DefaultConnectionHandler{
49-
Cancel: cancel,
50-
Ctx: ctx,
51-
ClientOptions: clientOptions,
52-
Client: nil,
38+
ctx, cancel := context.WithTimeout(context.Background(), config.Timeout())
39+
clientOptions := options.Client().ApplyURI(config.URI())
40+
41+
return &DefaultConnHandler{
42+
cancel: cancel,
43+
ctx: ctx,
44+
clientOptions: clientOptions,
5345
}, nil
5446
}
5547

5648
// Connect returns a new MongoDB client
57-
func (d *DefaultConnectionHandler) Connect() (*mongo.Client, error) {
49+
func (d *DefaultConnHandler) Connect() (*mongo.Client, error) {
5850
// Check if the connection is already established
59-
if d.Client != nil {
60-
return d.Client, godatabases.ErrAlreadyConnected
51+
if d.client != nil {
52+
return d.client, godatabases.ErrAlreadyConnected
6153
}
6254

6355
// Connect to MongoDB
64-
client, err := mongo.Connect(d.Ctx, d.ClientOptions)
56+
client, err := mongo.Connect(d.ctx, d.clientOptions)
6557

6658
// Create MongoDB Connection struct
6759
if err != nil {
@@ -75,33 +67,31 @@ func (d *DefaultConnectionHandler) Connect() (*mongo.Client, error) {
7567
}
7668

7769
// Set client
78-
d.Client = client
70+
d.client = client
7971

8072
return client, nil
8173
}
8274

83-
// GetClient returns the MongoDB client
84-
func (d *DefaultConnectionHandler) GetClient() (*mongo.Client, error) {
75+
// Client returns the MongoDB client
76+
func (d *DefaultConnHandler) Client() (*mongo.Client, error) {
8577
// Check if the connection is established
86-
if d.Client == nil {
78+
if d.client == nil {
8779
return nil, godatabases.ErrNotConnected
8880
}
8981

90-
return d.Client, nil
82+
return d.client, nil
9183
}
9284

9385
// Disconnect closes the MongoDB client connection
94-
func (d *DefaultConnectionHandler) Disconnect() {
95-
defer func() {
96-
// Check if the connection is established
97-
if d.Client == nil {
98-
return
99-
}
100-
101-
// Close the connection
102-
d.Cancel()
103-
if err := d.Client.Disconnect(d.Ctx); err != nil {
104-
panic(godatabases.ErrFailedToDisconnect)
105-
}
106-
}()
86+
func (d *DefaultConnHandler) Disconnect() {
87+
// Check if the connection is established
88+
if d.client == nil {
89+
return
90+
}
91+
92+
// Close the connection
93+
d.cancel()
94+
if err := d.client.Disconnect(d.ctx); err != nil {
95+
panic(godatabases.ErrFailedToDisconnect)
96+
}
10797
}

mongodb/errors.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ var (
66
ErrFailedToCreateDocument = errors.New("failed to create document")
77
ErrFailedToStartSession = errors.New("failed to start session")
88
ErrFailedToCreateIndex = "failed to create index '%v': %v"
9-
ErrNilConfig = errors.New("mongodb connection config cannot be nil")
109
ErrNilClient = errors.New("mongodb client cannot be nil")
1110
)

mongodb/index.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ import (
77
)
88

99
type FieldIndex struct {
10-
Name string
11-
Order Order
10+
name string
11+
order Order
1212
}
1313

1414
// NewFieldIndex creates a new field index
1515
func NewFieldIndex(name string, order Order) *FieldIndex {
16-
return &FieldIndex{Name: name, Order: order}
16+
return &FieldIndex{name, order}
1717
}
1818

1919
// NewUniqueIndex creates a new unique field index model
2020
func NewUniqueIndex(fieldIndex FieldIndex, unique bool) *mongo.IndexModel {
2121
return &mongo.IndexModel{
22-
Keys: bson.D{{fieldIndex.Name, fieldIndex.Order.OrderInt()}},
22+
Keys: bson.D{{fieldIndex.name, fieldIndex.order.OrderInt()}},
2323
Options: options.Index().SetUnique(unique),
2424
}
2525
}
@@ -41,7 +41,7 @@ func NewCompoundFieldIndex(
4141
for _, fieldIndex := range fieldIndexes {
4242
keys = append(
4343
keys,
44-
bson.E{Key: fieldIndex.Name, Value: fieldIndex.Order.OrderInt()},
44+
bson.E{Key: fieldIndex.name, Value: fieldIndex.order.OrderInt()},
4545
)
4646
}
4747

redis/config.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package redis
2+
3+
type (
4+
// Config interface
5+
Config interface {
6+
URI() string
7+
Password() string
8+
Database() int
9+
}
10+
11+
// ConnConfig struct
12+
ConnConfig struct {
13+
uri string
14+
password string
15+
database int
16+
}
17+
)
18+
19+
// NewConnConfig creates a new Redis config
20+
func NewConnConfig(uri, password string, database int) *ConnConfig {
21+
return &ConnConfig{
22+
uri,
23+
password,
24+
database,
25+
}
26+
}
27+
28+
// URI returns the URI
29+
func (c *ConnConfig) URI() string {
30+
return c.uri
31+
}
32+
33+
// Password returns the password
34+
func (c *ConnConfig) Password() string {
35+
return c.password
36+
}
37+
38+
// Database returns the database
39+
func (c *ConnConfig) Database() int {
40+
return c.database
41+
}

0 commit comments

Comments
 (0)