-
-
Notifications
You must be signed in to change notification settings - Fork 181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Could we get some more examples, specifically for updateItem? #148
Comments
Hello, thanks. I am slowly working on adding more docs and examples and hope to update the readme soonish. Right now the only official stuff is in the godoc. Unfortunately my stuff in production is closed source. |
this work by @xralf is excellent - very simple to get up and running - has a sample to create table by cli - I returned to this codebase because I'm seeing this sess := session.Must(session.NewSession())
db := dynamo.New(sess, &aws.Config{Region: aws.String("us-west-2")})
table := db.Table("Widgets")
// put item
w := widget{UserID: 613, Time: time.Now(), Msg: "hello"}
err := table.Put(w).Run() need the json / yaml definition.... UPDATE 2. Creating tables
You can use struct tags to specify hash keys, range keys, and indexes when creating a table.
For example:
type UserAction struct {
UserID string `dynamo:"ID,hash" index:"Seq-ID-index,range"`
Time time.Time `dynamo:",range"`
Seq int64 `localIndex:"ID-Seq-index,range" index:"Seq-ID-index,hash"`
UUID string `index:"UUID-index,hash"`
}
This creates a table with the primary hash key ID and range key Time. It creates two global secondary indices called UUID-index and Seq-ID-index, and a local secondary index called ID-Seq-index.
this talks about creating a table - but it's missing the actual create statement??? |
@johndpope Sorry, what's your question? For this library, the example is not meant to be a tutorial. If you scroll down to the Integration Tests section there is an example of how to use the AWS CLI to create the tables for the integration tests, which is the same as the example in the readme, added in #173. In the code you linked, it uses dynamo's struct tags to create a new table here: https://github.com/xralf/dynamo/blob/2e319168cbe0965139c6f0d85bae0e2788e455bc/go/guregu/example.go#L56 which is much different than the schema you posted a screenshot of. You can still use JSON schemas and the AWS CLI if you want. You could even go into the control panel and click around. How you create your tables is up to you, this library does not prescribe any particular method. If you're getting a "request context canceled" error that means your request timed out. I'm guessing you're using DynamoDB local. IIRC, there is a bug in DynamoDB local that causes it to hang forever when you try to access a table that does not exist. That could be the cause. It's possible that the code you linked does not work with DynamoDB local because it tries to delete a table to check whether it exists. It could be a configuration error. I can't say for sure. I would recommend using real DynamoDB tables if you're new to DynamoDB because it eliminates many potential things that could go wrong. |
type Widget struct {
Id int `json:"id" dynamo:"id,hash" index:"time-id-index,range"` // Hash key, a.k.a. partition key
Time time.Time `json:"time" dynamo:"time,range" index:"time-id-index,hash" index:"msg-time-index,range"` // Range key, a.k.a. sort key
Msg string `json:"msg,omitempty" dynamo:"msg,omitempty" index:"msg-time-index,hash"` // Change name in the database
Count int `json:"count,omitempty" dynamo:"count,omitempty"` // Omits if zero value
Children []Widget `json:"children,omitempty" dynamo:"children,omitempty"` // Lists
Friends []string `json:"friends,omitempty" dynamo:"friends,set"` // Sets
Set map[string]struct{} `json:"set,omitempty" dynamo:"set,set"` // Map sets, too!
SecretKey string `json:"secretKey,omitempty" dynamo:"-"` // Ignored
SecretKey2 string `json:"secretKey2,omitempty" dynamo:"secretKey2"` // Ignored
}
Thanks for quick feedback - I think all I'm wanting is this line you've pointed out to create the table - in the readme I'm going to test this and see if I can't get some data back. |
Good idea. I'll add it when I have some time.
The first line on the readme uses AWS's official SDK to establish a session sess := session.Must(session.NewSession()) You can read about it here: https://docs.aws.amazon.com/sdk-for-go/api/aws/session/ If you're using the cloud, make sure you don't set the Endpoint field: |
Good luck! One final thing to keep in mind: creating tables takes time, sometimes a few seconds. I'll be adding #52 in the next couple months. There's some example code there if you want to wait for new tables to finish creating. |
The lib seems great and we plan to use this in production but having some difficulty translating some of the tests to functional code for our constraints.
Are there some more examples/docs somewhere I can reference?
The text was updated successfully, but these errors were encountered: