-
Notifications
You must be signed in to change notification settings - Fork 1
/
delete_item.go
59 lines (50 loc) · 1.57 KB
/
delete_item.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
package dynago
import (
"context"
"log"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
/**
* Used to delete a db record from dynamodb given a partition key and sort key
* @param pk the partition key of the record
* @param sk the sort key of the record
* @return true if the record was deleted, false otherwise
*/
func (t *Client) DeleteItem(ctx context.Context, pk string, sk string) error {
//delete item from dynamodb
input := &dynamodb.DeleteItemInput{
TableName: &t.TableName,
Key: map[string]types.AttributeValue{
"pk": &types.AttributeValueMemberS{Value: pk},
"sk": &types.AttributeValueMemberS{Value: sk},
},
}
resp, err := t.client.DeleteItem(ctx, input)
if err != nil && resp == nil {
log.Println("failed to delete record into database. Error:" + err.Error())
return err
}
return nil
}
type TransactDeleteItemsInput struct {
PartitionKeyValue Attribute
SortKeyValue Attribute
}
// TODO: [low priority] The aggregate size of the items in the transaction cannot exceed 4 MB.
func (t *Client) TransactDeleteItems(ctx context.Context, inputs []*TransactDeleteItemsInput) error {
requests := make([]types.TransactWriteItem, len(inputs))
for idx, in := range inputs {
requests[idx] = types.TransactWriteItem{
Delete: &types.Delete{TableName: &t.TableName,
Key: map[string]types.AttributeValue{
"pk": in.PartitionKeyValue,
"sk": in.SortKeyValue,
}},
}
}
_, err := t.client.TransactWriteItems(ctx, &dynamodb.TransactWriteItemsInput{
TransactItems: requests,
})
return err
}