/
store.go
85 lines (72 loc) · 2.26 KB
/
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
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
package dynamodbstore
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/SKF/go-eventsource/eventsource"
)
type store struct {
db *dynamodb.DynamoDB
tableName string
consistentRead bool
}
// New ...
func New(db *dynamodb.DynamoDB, tableName string) eventsource.Store {
return &store{
db: db,
tableName: tableName,
consistentRead: true,
}
}
//LoadByAggregate ...
func (store *store) LoadByAggregate(ctx context.Context, aggregateID string) (records []eventsource.Record, err error) {
records = []eventsource.Record{}
key := map[string]*dynamodb.AttributeValue{
":id": {S: &aggregateID},
}
input := dynamodb.QueryInput{
TableName: &store.tableName,
KeyConditionExpression: aws.String("aggregateId = :id"),
ExpressionAttributeValues: key,
ConsistentRead: &store.consistentRead,
}
output, err := store.db.QueryWithContext(ctx, &input)
if err != nil {
log.
WithField("input", input).
WithField("error", err).
Error("Couldn't scan pages")
err = errors.Wrap(err, "couldn't scan pages")
return
}
err = dynamodbattribute.UnmarshalListOfMaps(output.Items, &records)
if err != nil {
log.
WithField("error", err).
Error("Couldn't unmarshal list of maps")
err = errors.Wrap(err, "couldn't unmarshal list of maps")
return
}
return records, err
}
// LoadBySequenceID ...
func (store *store) LoadBySequenceID(ctx context.Context, sequenceID string, limit int) (records []eventsource.Record, err error) {
err = errors.New("operation not supported on DynamoDB")
log.Error(err.Error())
return
}
// LoadBySequenceIDAndType ...
func (store *store) LoadBySequenceIDAndType(ctx context.Context, sequenceID string, eventType string, limit int) (records []eventsource.Record, err error) {
err = errors.New("operation not supported on DynamoDB")
log.Error(err.Error())
return
}
// LoadByTimestamp
func (store *store) LoadByTimestamp(ctx context.Context, timestamp int64, limit int) (records []eventsource.Record, err error) {
err = errors.New("operation not supported on DynamoDB")
log.Error(err.Error())
return
}