@@ -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
1110type (
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}
0 commit comments