-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction_items.go
54 lines (46 loc) · 1.37 KB
/
transaction_items.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
package dynago
import (
"context"
"log"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
func (t *Client) WithDeleteItem(pk string, sk string) types.TransactWriteItem {
return types.TransactWriteItem{
Delete: &types.Delete{
TableName: &t.TableName,
Key: map[string]types.AttributeValue{
"pk": &types.AttributeValueMemberS{Value: pk},
"sk": &types.AttributeValueMemberS{Value: sk},
},
},
}
}
func (t *Client) WithPutItem(pk string, sk string, item interface{}) types.TransactWriteItem {
av, err := attributevalue.MarshalMap(item)
if err != nil {
log.Println("Failed to Marshal item" + err.Error())
return types.TransactWriteItem{}
}
keys := map[string]types.AttributeValue{
"pk": &types.AttributeValueMemberS{Value: pk},
"sk": &types.AttributeValueMemberS{Value: sk},
}
for k, v := range keys {
av[k] = v
}
return types.TransactWriteItem{
Put: &types.Put{
TableName: &t.TableName,
Item: av,
},
}
}
// TransactItems is a synchronous for writing or deletion operation performed in dynamodb grouped together
func (t *Client) TransactItems(ctx context.Context, input ...types.TransactWriteItem) error {
_, err := t.client.TransactWriteItems(ctx, &dynamodb.TransactWriteItemsInput{
TransactItems: input,
})
return err
}