-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodbx.go
45 lines (37 loc) · 1.17 KB
/
mongodbx.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
package mongodbx
import (
"github.com/blackhorseya/mundo/pkg/configx"
"github.com/blackhorseya/mundo/pkg/contextx"
"github.com/pkg/errors"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/mongodb"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// NewClientWithDSN returns a new mongo client with dsn.
func NewClientWithDSN(dsn string) (*mongo.Client, error) {
opts := options.Client().ApplyURI(dsn)
client, err := mongo.Connect(contextx.Background(), opts)
if err != nil {
return nil, err
}
return client, nil
}
// NewClient returns a new mongo client.
func NewClient() (*mongo.Client, error) {
return NewClientWithDSN(configx.C.Storage.Mongodb.DSN)
}
// Container is used to represent a mongodb container.
type Container struct {
*mongodb.MongoDBContainer
}
// NewContainer returns a new mongodb container.
func NewContainer(ctx contextx.Contextx) (*Container, error) {
container, err := mongodb.RunContainer(ctx, testcontainers.WithImage("mongo:6"))
if err != nil {
return nil, errors.Wrap(err, "run mongodb container")
}
return &Container{
MongoDBContainer: container,
}, nil
}