/
table.go
134 lines (126 loc) · 3.57 KB
/
table.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package table
import (
"fmt"
"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/seanpburke/graphql-api-demo/pkg/config"
)
// Item is an interface for items that go in the table.
type Item interface {
MakePK() string // Constructs the item's partition (HASH) key.
MakeSK() string // Constructs the item's sort (RANGE) key.
Init() // sets item.PK and item.SK
}
func GetItem(item Item) error {
result, err := config.Config.DDB.GetItem(&dynamodb.GetItemInput{
TableName: aws.String(config.Config.Table),
Key: map[string]*dynamodb.AttributeValue{
"PK": {
S: aws.String(item.MakePK()),
},
"SK": {
S: aws.String(item.MakeSK()),
},
},
})
if err != nil {
return fmt.Errorf("table.GetItem %w", err)
}
return dynamodbattribute.UnmarshalMap(result.Item, item)
}
func PutItem(i Item) error {
i.Init()
av, err := dynamodbattribute.MarshalMap(i)
if err != nil {
return fmt.Errorf("customer.PutItem dynamodbattribute.MarshalMap failed, %w", err)
}
input := &dynamodb.PutItemInput{
Item: av,
TableName: aws.String(config.Config.Table),
}
if _, err = config.Config.DDB.PutItem(input); err != nil {
return fmt.Errorf("dynamodb.PutItem(%s) failed, %w ", config.Config.Table, err)
}
return nil
}
func CreateTable() (*dynamodb.CreateTableOutput, error) {
input := &dynamodb.CreateTableInput{
AttributeDefinitions: []*dynamodb.AttributeDefinition{
{
AttributeName: aws.String("PK"),
AttributeType: aws.String("S"),
},
{
AttributeName: aws.String("SK"),
AttributeType: aws.String("S"),
},
{
AttributeName: aws.String("GSI2PK"),
AttributeType: aws.String("S"),
},
},
KeySchema: []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String("PK"),
KeyType: aws.String(dynamodb.KeyTypeHash),
},
{
AttributeName: aws.String("SK"),
KeyType: aws.String(dynamodb.KeyTypeRange),
},
},
GlobalSecondaryIndexes: []*dynamodb.GlobalSecondaryIndex{
{ // GSI1 is a reverse index PK -> SK
IndexName: aws.String("GSI1"),
KeySchema: []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String("SK"),
KeyType: aws.String(dynamodb.KeyTypeHash),
},
{
AttributeName: aws.String("PK"),
KeyType: aws.String(dynamodb.KeyTypeRange),
},
},
Projection: &dynamodb.Projection{
ProjectionType: aws.String(dynamodb.ProjectionTypeAll),
},
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(1),
WriteCapacityUnits: aws.Int64(1),
},
},
{ // GSI2 is a secondary index GSI2PK -> PK
IndexName: aws.String("GSI2"),
KeySchema: []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String("GSI2PK"),
KeyType: aws.String(dynamodb.KeyTypeHash),
},
{
AttributeName: aws.String("PK"),
KeyType: aws.String(dynamodb.KeyTypeRange),
},
},
Projection: &dynamodb.Projection{
ProjectionType: aws.String(dynamodb.ProjectionTypeAll),
},
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(1),
WriteCapacityUnits: aws.Int64(1),
},
},
},
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(1),
WriteCapacityUnits: aws.Int64(1),
},
TableName: aws.String(config.Config.Table),
}
output, err := config.Config.DDB.CreateTable(input)
if err != nil {
err = fmt.Errorf("CreateTable(%q) %w", config.Config.Table, err)
}
return output, err
}