generated from sv-tools/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.go
165 lines (140 loc) · 4.41 KB
/
client.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
164
165
package mongoifc
import (
"context"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
// Client is an interface for `mongo.Client` structure
// Documentation: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Client
type Client interface {
Connect(ctx context.Context) error
Database(name string, opts ...*options.DatabaseOptions) Database
Disconnect(ctx context.Context) error
ListDatabaseNames(
ctx context.Context,
filter interface{},
opts ...*options.ListDatabasesOptions,
) ([]string, error)
ListDatabases(
ctx context.Context,
filter interface{},
opts ...*options.ListDatabasesOptions,
) (mongo.ListDatabasesResult, error)
NumberSessionsInProgress() int
Ping(ctx context.Context, rp *readpref.ReadPref) error
StartSession(opts ...*options.SessionOptions) (Session, error)
Timeout() *time.Duration
UseSession(
ctx context.Context,
fn func(sc SessionContext) error,
) error
UseSessionWithOptions(
ctx context.Context,
opts *options.SessionOptions,
fn func(sc SessionContext) error,
) error
Watch(
ctx context.Context,
pipeline interface{},
opts ...*options.ChangeStreamOptions,
) (ChangeStream, error)
}
type client struct {
cl *mongo.Client
}
// Deprecated: Use [mongo.Connect] instead.
func (c *client) Connect(ctx context.Context) error {
return c.cl.Connect(ctx)
}
func (c *client) Database(name string, opts ...*options.DatabaseOptions) Database {
return wrapDatabase(c.cl.Database(name, opts...), c)
}
func (c *client) Disconnect(ctx context.Context) error {
return c.cl.Disconnect(ctx)
}
func (c *client) ListDatabaseNames(
ctx context.Context,
filter interface{},
opts ...*options.ListDatabasesOptions,
) ([]string, error) {
return c.cl.ListDatabaseNames(ctx, filter, opts...)
}
func (c *client) ListDatabases(
ctx context.Context,
filter interface{},
opts ...*options.ListDatabasesOptions,
) (mongo.ListDatabasesResult, error) {
return c.cl.ListDatabases(ctx, filter, opts...)
}
func (c *client) NumberSessionsInProgress() int {
return c.cl.NumberSessionsInProgress()
}
func (c *client) Ping(ctx context.Context, rp *readpref.ReadPref) error {
return c.cl.Ping(ctx, rp)
}
func (c *client) StartSession(opts ...*options.SessionOptions) (Session, error) {
ss, err := c.cl.StartSession(opts...)
if err != nil {
return nil, err
}
return wrapSession(ss, c), nil
}
func (c *client) Timeout() *time.Duration {
return c.cl.Timeout()
}
func (c *client) UseSession(ctx context.Context, fn func(sc SessionContext) error) error {
return c.cl.UseSession(ctx, wrapFn1(fn, c))
}
func (c *client) UseSessionWithOptions(
ctx context.Context,
opts *options.SessionOptions,
fn func(sc SessionContext) error,
) error {
return c.cl.UseSessionWithOptions(ctx, opts, wrapFn1(fn, c))
}
func (c *client) Watch(
ctx context.Context,
pipeline interface{},
opts ...*options.ChangeStreamOptions,
) (ChangeStream, error) {
cs, err := c.cl.Watch(ctx, pipeline, opts...)
if err != nil {
return nil, err
}
return wrapChangeStream(cs), nil
}
// WrapClient returns an instance of Client interface for given mongo.Client object
func WrapClient(cl *mongo.Client) Client {
return &client{cl: cl}
}
// UnWrapClient returns original mongo.Client
func UnWrapClient(cl Client) *mongo.Client {
return cl.(*client).cl
}
// Connect is a wrapper for `mongo.Connect` function to return the object as `Client` interface
// Documentation: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Connect
func Connect(ctx context.Context, opts ...*options.ClientOptions) (Client, error) {
cl, err := mongo.Connect(ctx, opts...)
if err != nil {
return nil, err
}
return WrapClient(cl), nil
}
// NewClient is a wrapper for `mongo.NewClient` function to return the object as `Client` interface
// Documentation: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#NewClient
//
// Deprecated: Use [Connect] instead.
func NewClient(opts ...*options.ClientOptions) (Client, error) {
cl, err := mongo.NewClient(opts...)
if err != nil {
return nil, err
}
return WrapClient(cl), nil
}
// WithSession is a wrapper for `mongo.WithSession` function to call then `mongo.WithSession` function
// Documentation: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#WithSession
func WithSession(ctx context.Context, sess Session, fn func(sc SessionContext) error) error {
return mongo.WithSession(ctx, sess.(*session).ss, wrapFn1(fn, sess.Client().(*client)))
}