forked from dugancathal/dynago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_client_test.go
167 lines (146 loc) · 4.34 KB
/
example_client_test.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package dynago_test
import (
"fmt"
"github.com/bemobi/dynago"
"github.com/bemobi/dynago/schema"
)
func ExampleClient_BatchGet(client *dynago.Client) {
key1 := dynago.HashKey("Id", 5)
key2 := dynago.HashKey("Id", 7)
result, err := client.BatchGet().
Get("Topics", key1, key2).
Get("Users", dynago.HashKey("UserId", 4)).
ProjectionExpression("Users", "UserId, FirstName, Email").
Execute()
if err == nil {
for _, record := range result.Responses["Topics"] {
fmt.Printf("Topic %d: %s\n", record["Id"], record["Title"])
}
}
}
func ExampleClient_BatchWrite(client *dynago.Client) {
record1 := dynago.Document{"Id": 1, "Name": "Person1"}
record2 := dynago.Document{"Id": 2, "Name": "Person2"}
// We can put and delete at the same time to multiple tables.
client.BatchWrite().
Put("Table1", record1, record2).
Put("Table2", dynago.Document{"Name": "Other"}).
Delete("Table2", dynago.HashKey("Id", 42)).
Execute()
}
func ExampleClient_CreateTable_basic(client *dynago.Client) {
// NewCreateRequest creates a table with simple defaults.
// You can use chaining to set the hash and range keys.
table1 := schema.NewCreateRequest("TableName").
HashKey("UserId", schema.Number).
RangeKey("Date", schema.String)
table1.ProvisionedThroughput.ReadCapacityUnits = 45
client.CreateTable(table1)
}
func ExampleClient_CreateTable_full(client *dynago.Client) {
// Most of the time we don't need the full syntax for making create requests
// It's shown here mostly for purpose of documentation
req := &schema.CreateRequest{
TableName: "PersonalPages",
AttributeDefinitions: []schema.AttributeDefinition{
{"UserId", schema.Number},
{"Title", schema.String},
},
KeySchema: []schema.KeySchema{
{"UserId", schema.HashKey},
{"Title", schema.RangeKey},
},
ProvisionedThroughput: schema.ProvisionedThroughput{
ReadCapacityUnits: 45,
WriteCapacityUnits: 72,
},
}
if response, err := client.CreateTable(req); err == nil {
fmt.Printf("table created, status %s", response.TableDescription.TableStatus)
}
}
func ExampleClient_ListTables_paging(client *dynago.Client) {
cursor := client.ListTables().Limit(100)
for cursor != nil {
response, err := cursor.Execute()
if err != nil {
break
}
fmt.Printf("%v", response.TableNames)
cursor = response.Next()
}
}
func ExampleClient_PutItem(client *dynago.Client) {
doc := dynago.Document{
"Id": 42,
"Name": "Bob",
"Address": dynago.Document{
"City": "Boston",
"State": "MA",
},
}
_, err := client.PutItem("Person", doc).Execute()
if err != nil {
fmt.Printf("PUT failed: %v", err)
}
}
func ExampleClient_Query(client *dynago.Client) {
result, err := client.Query("table").
KeyConditionExpression("Foo = :val AND begins_with(Title, :prefix)").
Param(":val", 45).Param(":prefix", "The adventures of").
Execute()
if err == nil {
for _, row := range result.Items {
fmt.Printf("ID: %s, Foo: %d", row["Id"], row["Foo"])
}
}
}
func ExampleClient_Query_pagination(client *dynago.Client) {
query := client.Query("table").
KeyConditionExpression("Foo = :val").
Limit(50)
// Keep getting results in a loop until there are no more.
for query != nil {
result, err := query.Execute()
if err != nil {
break
}
for _, item := range result.Items {
fmt.Printf("Result ID %d\n", item["Id"])
}
query = result.Next()
}
}
func ExampleClient_Scan_parallel(client *dynago.Client) {
numSegments := 10
baseScan := client.Scan("Table").Limit(1000)
// spin up numSegments goroutines each working on a table segment
for i := 0; i < numSegments; i++ {
go func(scan *dynago.Scan) {
for scan != nil {
result, _ := scan.Execute()
// do something with result.Items
scan = result.Next()
}
}(baseScan.Segment(i, numSegments))
}
}
func ExampleClient_UpdateItem(client *dynago.Client) {
_, err := client.UpdateItem("Person", dynago.HashKey("Id", 42)).
UpdateExpression("SET Name=:name").
Param(":name", "Joe").
Execute()
if err != nil {
fmt.Printf("UpdateItem failed: %v", err)
}
}
func ExampleClient_UpdateItem_atomicIncrement(client *dynago.Client, key dynago.Document) {
result, err := client.UpdateItem("Products", key).
UpdateExpression("SET ViewCount = ViewCount + :incr").
Param(":incr", 5).
ReturnValues(dynago.ReturnUpdatedNew).
Execute()
if err == nil {
fmt.Printf("View count is now %d", result.Attributes["ViewCount"])
}
}