-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
store.go
40 lines (35 loc) · 903 Bytes
/
store.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
package mongodb
import (
"context"
"crypto/tls"
"fmt"
pkgMongo "github.com/plgd-dev/hub/v2/pkg/mongodb"
"go.opentelemetry.io/otel/trace"
)
type Store struct {
*pkgMongo.Store
}
func NewStore(ctx context.Context, cfg pkgMongo.Config, tls *tls.Config, tracerProvider trace.TracerProvider) (*Store, error) {
m, err := pkgMongo.NewStore(ctx, cfg, tls, tracerProvider)
if err != nil {
return nil, err
}
s := Store{m}
s.SetOnClear(func(c context.Context) error {
return s.clearDatabases(ctx)
})
return &s, nil
}
func (s *Store) clearDatabases(ctx context.Context) error {
var errors []error
if err := s.Collection(resLinkedAccountCName).Drop(ctx); err != nil {
errors = append(errors, err)
}
if err := s.Collection(resLinkedCloudCName).Drop(ctx); err != nil {
errors = append(errors, err)
}
if len(errors) > 0 {
return fmt.Errorf("cannot clear: %v", errors)
}
return nil
}