From 35807f650720ce804f86d41bad22f432cd1f2a4c Mon Sep 17 00:00:00 2001 From: Greg Date: Sun, 16 Jun 2024 05:44:58 +0900 Subject: [PATCH] add some examples --- example_test.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 example_test.go diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..331de5d --- /dev/null +++ b/example_test.go @@ -0,0 +1,67 @@ +package dynamo_test + +import ( + "context" + "log" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/aws/retry" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/credentials" + "github.com/guregu/dynamo/v2" +) + +func ExampleNew() { + // Basic setup example. + // See: https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/config for more on configuration options. + const region = "us-west-2" + cfg, err := config.LoadDefaultConfig( + context.Background(), + config.WithRegion(region), + ) + if err != nil { + log.Fatal(err) + } + db := dynamo.New(cfg) + // use the db + _ = db +} + +func ExampleNew_local_endpoint() { + // Example of connecting to a DynamoDB local instance. + // See: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html + const endpoint = "http://localhost:8000" + resolver := aws.EndpointResolverWithOptionsFunc( + func(service, region string, options ...interface{}) (aws.Endpoint, error) { + return aws.Endpoint{URL: endpoint}, nil + }, + ) + // credentials can be anything, but must be set + creds := credentials.NewStaticCredentialsProvider("dummy", "dummy", "") + cfg, err := config.LoadDefaultConfig( + context.Background(), + config.WithRegion("local"), // region can also be anything + config.WithEndpointResolverWithOptions(resolver), + config.WithCredentialsProvider(creds), + ) + if err != nil { + log.Fatal(err) + } + db := dynamo.New(cfg) + // use the db + _ = db +} + +func ExampleRetryTx() { + // `dynamo.RetryTx` is an option you can pass to retry.NewStandard. + // It will automatically retry canceled transactions. + cfg, err := config.LoadDefaultConfig(context.Background(), config.WithRetryer(func() aws.Retryer { + return retry.NewStandard(dynamo.RetryTx) + })) + if err != nil { + log.Fatal(err) + } + db := dynamo.New(cfg) + // use the db + _ = db +}