dynamo icon indicating copy to clipboard operation
dynamo copied to clipboard

Feature Request: Could we get some more examples, specifically for updateItem?

Open RichDavis1 opened this issue 4 years ago • 7 comments

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?

RichDavis1 avatar Oct 30 '20 17:10 RichDavis1

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. Can you be more specific about what you're trying to do? I can help explain, and it will be useful to know what people are looking for so I can add it to the docs.

guregu avatar Oct 30 '20 17:10 guregu

this work by @xralf is excellent - very simple to get up and running - has a sample to create table by cli - https://github.com/xralf/dynamo

I returned to this codebase because I'm seeing this 2021/09/21 13:27:20 RequestCanceled: request context canceled I think the tables are wrong.... the simple example on readme - seems to depend on table being created....


	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.... Screen Shot 2021-09-21 at 1 40 51 pm

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 avatar Sep 21 '21 03:09 johndpope

@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.

guregu avatar Sep 21 '21 04:09 guregu

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 err := dynamoClient.CreateTable(tableName, Widget{}).Run()

I'm going to test this and see if I can't get some data back. I'm using the cloud - not local. (Presumably the library takes the aws credentials from ~/.aws ??? there's no token / access key passed into the init... )

johndpope avatar Sep 21 '21 04:09 johndpope

Thanks for quick feedback - I think all I'm wanting is this line you've pointed out to create the table - in the readme

Good idea. I'll add it when I have some time.

Presumably the library takes the aws credentials from ~/.aws

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/ It looks at ~/.aws, environment variables, etc.

If you're using the cloud, make sure you don't set the Endpoint field: https://github.com/xralf/dynamo/blob/2e319168cbe0965139c6f0d85bae0e2788e455bc/go/guregu/example.go#L138 This is setting up the client to connect to DynamoDB Local. Delete this line to connect to the cloud instead.

guregu avatar Sep 21 '21 04:09 guregu

Screen Shot 2021-09-21 at 2 53 47 pm found my bug (little snitch firewall rule) - yes. I'm getting somewhere now. Thanks for your help.

johndpope avatar Sep 21 '21 04:09 johndpope

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.

guregu avatar Sep 21 '21 05:09 guregu