@@ -13,6 +13,8 @@ import (
13
13
14
14
const StorageContextKey = "storage"
15
15
16
+ var tableNameMissingError = errors .New ("table name must not be empty" )
17
+
16
18
// Storage provides wrapper methods for interacting with DynamoDB
17
19
type Storage struct {
18
20
client * dynamodb.Client
@@ -51,7 +53,7 @@ func (s *Storage) Store(table string, item interface{}) error {
51
53
// Load retrieves the value at key and unmarshals it into item.
52
54
func (s * Storage ) Load (table , attrName , attrVal string , item interface {}) error {
53
55
if table == "" {
54
- return errors . New ( "table must not be empty" )
56
+ return tableNameMissingError
55
57
}
56
58
if attrName == "" {
57
59
return errors .New ("attrName must not be empty" )
@@ -84,7 +86,7 @@ func (s *Storage) Load(table, attrName, attrVal string, item interface{}) error
84
86
// Delete deletes key.
85
87
func (s * Storage ) Delete (table , attrName , attrVal string ) error {
86
88
if table == "" {
87
- return errors . New ( "table must not be empty" )
89
+ return tableNameMissingError
88
90
}
89
91
if attrName == "" {
90
92
return errors .New ("attrName must not be empty" )
@@ -101,3 +103,34 @@ func (s *Storage) Delete(table, attrName, attrVal string) error {
101
103
_ , err := s .client .DeleteItem (ctx , input )
102
104
return err
103
105
}
106
+
107
+ // ScanApiKey a table using apiKey-index
108
+ func (s * Storage ) ScanApiKey (table , apiKey string , items any ) error {
109
+ if table == "" {
110
+ return tableNameMissingError
111
+ }
112
+
113
+ input := & dynamodb.ScanInput {
114
+ FilterExpression : aws .String ("apiKey = :val" ),
115
+ ExpressionAttributeValues : map [string ]types.AttributeValue {
116
+ ":val" : & types.AttributeValueMemberS {Value : apiKey },
117
+ },
118
+ TableName : aws .String (table ),
119
+ }
120
+
121
+ ctx := context .Background ()
122
+ result , err := s .client .Scan (ctx , input )
123
+ if err != nil {
124
+ return err
125
+ }
126
+
127
+ if result .LastEvaluatedKey != nil {
128
+ return errors .New ("too many results, pagination has not been implemented" )
129
+ }
130
+
131
+ err = attributevalue .UnmarshalListOfMaps (result .Items , & items )
132
+ if err != nil {
133
+ return err
134
+ }
135
+ return nil
136
+ }
0 commit comments