-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
194 lines (162 loc) · 5.01 KB
/
Copy pathmain.go
File metadata and controls
194 lines (162 loc) · 5.01 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"fmt"
"log"
"github.com/execute008/goelectrodb/electrodb"
)
// Simple quick start example showing basic CRUD operations
func main() {
fmt.Println("ElectroDB Go - Quick Start Example")
fmt.Println("===================================")
// 1. Define a simple user schema
userSchema := &electrodb.Schema{
Service: "MyApp",
Entity: "User",
Table: "users-table",
Attributes: map[string]*electrodb.AttributeDefinition{
// Required attributes
"userId": {
Type: electrodb.AttributeTypeString,
Required: true,
},
"email": {
Type: electrodb.AttributeTypeString,
Required: true,
},
// Optional attributes
"firstName": {Type: electrodb.AttributeTypeString},
"lastName": {Type: electrodb.AttributeTypeString},
"age": {Type: electrodb.AttributeTypeNumber},
"status": {
Type: electrodb.AttributeTypeEnum,
EnumValues: []interface{}{"active", "inactive"},
Default: func() interface{} { return "active" },
},
},
// Define primary key structure
Indexes: map[string]*electrodb.IndexDefinition{
"primary": {
PK: electrodb.FacetDefinition{
Field: "pk",
Facets: []string{"userId"},
},
},
"byEmail": {
Index: stringPtr("gsi1"),
PK: electrodb.FacetDefinition{
Field: "gsi1pk",
Facets: []string{"email"},
},
},
},
// Enable automatic timestamps
Timestamps: &electrodb.TimestampsConfig{
CreatedAt: "createdAt",
UpdatedAt: "updatedAt",
},
}
// 2. Create the entity
entity, err := electrodb.NewEntity(userSchema, nil)
if err != nil {
log.Fatalf("Failed to create entity: %v", err)
}
fmt.Println("✅ Entity created successfully")
// 3. Create (Put) a new user
fmt.Println("📝 Creating user...")
putParams, err := entity.Put(electrodb.Item{
"userId": "user-123",
"email": "john@example.com",
"firstName": "John",
"lastName": "Doe",
"age": 30,
}).Params()
if err != nil {
log.Fatalf("Failed to create put params: %v", err)
}
fmt.Printf(" Put operation params generated\n")
fmt.Printf(" Table: %s\n", putParams["TableName"])
fmt.Printf(" Item includes: userId, email, firstName, lastName, age\n")
fmt.Printf(" Auto-added: createdAt, updatedAt, pk\n\n")
// 4. Get a user
fmt.Println("🔍 Getting user...")
getParams, err := entity.Get(electrodb.Keys{
"userId": "user-123",
}).Params()
if err != nil {
log.Fatalf("Failed to create get params: %v", err)
}
fmt.Printf(" Get operation params generated\n")
fmt.Printf(" Table: %s\n", getParams["TableName"])
fmt.Printf(" Key: {userId: user-123}\n\n")
// 5. Update a user
fmt.Println("✏️ Updating user...")
updateParams, err := entity.Update(electrodb.Keys{
"userId": "user-123",
}).
Set(map[string]interface{}{
"firstName": "Johnny",
"lastName": "Doe",
}).
Add(map[string]interface{}{
"age": 1, // Increment age
}).
Params()
if err != nil {
log.Fatalf("Failed to create update params: %v", err)
}
fmt.Printf(" Update operation params generated\n")
fmt.Printf(" Table: %s\n", updateParams["TableName"])
fmt.Printf(" Expression: %s\n", updateParams["UpdateExpression"])
fmt.Printf(" Note: updatedAt automatically updated\n\n")
// 6. Query users
fmt.Println("🔎 Querying users...")
queryParams, err := entity.Query("primary").
Query("user-123").
Params()
if err != nil {
log.Fatalf("Failed to create query params: %v", err)
}
fmt.Printf(" Query operation params generated\n")
fmt.Printf(" Table: %s\n", queryParams["TableName"])
fmt.Printf(" Key Condition: %s\n\n", queryParams["KeyConditionExpression"])
// 7. Delete a user
fmt.Println("🗑️ Deleting user...")
deleteParams, err := entity.Delete(electrodb.Keys{
"userId": "user-123",
}).Params()
if err != nil {
log.Fatalf("Failed to create delete params: %v", err)
}
fmt.Printf(" Delete operation params generated\n")
fmt.Printf(" Table: %s\n", deleteParams["TableName"])
fmt.Printf(" Key: {userId: user-123}\n\n")
// 8. Batch operations
fmt.Println("📦 Batch operations...")
// Batch Get
batchGetOp := entity.BatchGet([]electrodb.Keys{
{"userId": "user-1"},
{"userId": "user-2"},
{"userId": "user-3"},
})
fmt.Printf(" Batch get operation created for 3 users\n")
// Batch Write
batchWriteOp := entity.BatchWrite().
Put([]electrodb.Item{
{"userId": "user-4", "email": "user4@example.com"},
{"userId": "user-5", "email": "user5@example.com"},
}).
Delete([]electrodb.Keys{
{"userId": "user-6"},
})
fmt.Printf(" Batch write operation created (2 puts, 1 delete)\n")
_, _ = batchGetOp, batchWriteOp // Use variables to avoid warnings
fmt.Println("\n✨ Quick start complete!")
fmt.Println("\nNext steps:")
fmt.Println("- See examples/comprehensive for all features")
fmt.Println("- Read README_GO.md for detailed documentation")
fmt.Println("- Add AWS DynamoDB client to actually execute operations")
fmt.Println("- Check out validation, transformations, and advanced features")
}
func stringPtr(s string) *string {
return &s
}