-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
delete.go
43 lines (35 loc) · 1010 Bytes
/
delete.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
package mongodb
import (
"context"
"errors"
"github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventstore"
"github.com/plgd-dev/kit/v2/strings"
"go.mongodb.org/mongo-driver/bson"
)
func getDeviceIDFilter(queries []eventstore.DeleteQuery) bson.A {
deviceIDs := make(strings.Set)
for _, q := range queries {
if q.GroupID != "" {
deviceIDs.Add(q.GroupID)
}
}
deviceIDFilter := make(bson.A, 0, len(deviceIDs))
for deviceID := range deviceIDs {
deviceIDFilter = append(deviceIDFilter, deviceID)
}
return deviceIDFilter
}
// Delete documents with given group ids
func (s *EventStore) Delete(ctx context.Context, queries []eventstore.DeleteQuery) error {
deviceIDFilter := getDeviceIDFilter(queries)
if len(deviceIDFilter) == 0 {
return errors.New("failed to delete documents: invalid query")
}
col := s.client().Database(s.DBName()).Collection(getEventCollectionName())
_, err := col.DeleteMany(ctx, bson.M{
groupIDKey: bson.M{
"$in": deviceIDFilter,
},
})
return err
}