-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb.go
62 lines (51 loc) · 2.41 KB
/
mongodb.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
package mongodb
import (
"context"
"errors"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
const (
SortDescending = -1
_idField = "_id"
)
type DB interface {
FindById(ctx context.Context, collection string, id string, result interface{}) error
FindOne(ctx context.Context, collection string, result interface{}, opts ...Option) error
Find(ctx context.Context, collection string, result interface{}, opts ...Option) error
UpdateMany(ctx context.Context, collection string, filter, update interface{}, options *options.UpdateOptions) error
UpdateOne(ctx context.Context, collection string, filter, update interface{}, options *options.UpdateOptions) error
UpdateOneRaw(ctx context.Context, collection string, filter, update interface{}, options *options.UpdateOptions) error
UpdateManyRaw(ctx context.Context, collection string, filter, update interface{}, options *options.UpdateOptions) error
Insert(ctx context.Context, collection string, document interface{}) error
GetCollection(ctx context.Context, collection string) *mongo.Collection
EnsureIndexes(ctx context.Context, collection string, model []mongo.IndexModel) error
Delete(ctx context.Context, collection string, filter interface{}, opts *options.DeleteOptions) error
WithTransaction(callback func(sc mongo.SessionContext) (interface{}, error)) (interface{}, error)
FindOneAndUpdate(ctx context.Context, collection string, filter, update interface{}, opts *options.FindOneAndUpdateOptions, result interface{}) error
Count(ctx context.Context, collection string, filter interface{}, opts ...*options.CountOptions) (int64, error)
BulkWriteRaw(ctx context.Context, collection string, operations []mongo.WriteModel, opts ...*options.BulkWriteOptions) error
Close()
}
type updateFn func(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
func New(config *Config) (DB, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(config.URL))
if err != nil {
return nil, err
}
err = client.Ping(ctx, readpref.Primary())
if err != nil {
return nil, err
}
if config.Database == "" {
return nil, errors.New("no db name detected")
}
db := client.Database(config.Database)
return &dbImpl{
db: db,
}, nil
}