forked from wshops/wgm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wgm.go
163 lines (152 loc) · 4.82 KB
/
wgm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package wgm
import (
"context"
"errors"
"github.com/qiniu/qmgo"
"time"
)
// instance is a global variable of type *wgm.
// It is used to store an instance of the wgm struct, which is responsible for managing a connection to a database and providing methods for interacting with the database.
// Example usage:
// To initialize the instance, you can call the InitWgm function, passing in a connection URI and a database name.
//
// err := InitWgm(connectionUri, databaseName)
//
// To close the connection to the database, you can call the CloseAll function.
//
// CloseAll()
//
// To perform a database ping, you can call the Ping function.
//
// err := Ping()
//
// To retrieve a qmgo.Collection object for a specific collection name, you can call the Col function.
//
// collection := Col(collectionName)
//
// To retrieve the context associated with the instance, you can call the Ctx function.
//
// ctx := Ctx()
//
// To paginate through a collection of documents, you can call the FindPage function.
//
// totalDoc, totalPage := FindPage(model, filter, result, pageSize, currentPage)
//
// To paginate through a collection of documents with additional options, you can call the FindPageWithOption function.
//
// totalDoc, totalPage := FindPageWithOption(model, filter, result, pageSize, currentPage, option)
//
// To check if a document exists in the collection, you can call the FindOne function.
//
// hasResult := FindOne(model, filter)
//
// To find a document by its ID, you can call the FindById function.
//
// found, err := FindById(collectionName, id, result)
//
// To insert a document into the collection, you can call the Insert function.
//
// insertResult, err := Insert(model)
//
// The `wgm` type is a struct that has the following methods:
// - GetModelCollection: returns a qmgo.Collection object for a specific model.
// - GetCollection: returns a qmgo.Collection object for a specific collection name.
// - Ctx: returns the context associated with the wgm instance.
// - newCtxWithTimeout: creates a new context with a timeout duration.
// - newCtx: creates a new context with a default timeout duration.
// The `IDefaultModel` type is an interface that defines several methods related to working with database models.
var instance *wgm
type wgm struct {
client *qmgo.Client
dbName string
}
// InitWgm initializes the connection to the specified database using the provided connection URI and database name.
// Parameters:
// - connectionUri: The URI of the MongoDB connection.
// - databaseName: The name of the database to connect to.
// Returns:
// - error: An error if the connection could not be established.
// Usage example:
//
// err := InitWgm("mongodb://localhost:27017/", "wshop_test")
// if err != nil {
// // Handle error
// }
//
// Usage example:
//
// err := InitWgm("wrong://localhost:27017/", "wshop_test")
// if err != nil {
// // Handle error
// }
//
// Usage example:
//
// SetupDefaultConnection()
//
// Deprecated: Use InitWgm instead.
func InitWgm(connectionUri string, databaseName string) error {
ctx := context.Background()
client, err := qmgo.NewClient(ctx, &qmgo.Config{
Uri: connectionUri,
Database: databaseName,
})
if err != nil {
return err
}
instance = &wgm{
client: client,
dbName: databaseName,
}
return nil
}
// NewWGM initializes the connection to the specified database using the provided connection URI and database name.
func NewWGM(connectionUri string, databaseName string) error {
ctx := context.Background()
client, err := qmgo.NewClient(ctx, &qmgo.Config{
Uri: connectionUri,
Database: databaseName,
})
if err != nil {
return err
}
instance = &wgm{
client: client,
dbName: databaseName,
}
return nil
}
// newCtxWithTimeout creates a new context with the specified timeout.
// Parameters:
// - timeout: The duration of the timeout.
// Returns:
// - context.Context: The newly created context.
func (w *wgm) newCtxWithTimeout(timeout time.Duration) context.Context {
ctx, _ := context.WithTimeout(context.Background(), timeout)
return ctx
}
// newCtx creates a new context with a default timeout of 10 seconds.
// This method is used internally by the Ctx() method.
//
// Returns:
// - context.Context: The newly created context.
func (w *wgm) newCtx() context.Context {
return w.newCtxWithTimeout(10 * time.Second)
}
func CloseAll() error {
if instance == nil {
return errors.New("must initialize WGM first, by calling NewWGM() method")
}
if err := instance.client.Close(instance.Ctx()); err != nil {
return err
}
return nil
}
// Ping checks if the WGM instance is initialized and then performs a ping operation on the MongoDB connection.
// If the WGM instance is not initialized, it logs an error message and returns an error.
func Ping() error {
if instance == nil {
return errors.New("must initialize WGM first, by calling NewWGM() method")
}
return instance.client.Ping(10)
}