Configuration¶
Dynorm uses sensible defaults but can be configured for different environments.
Environment Variables¶
| Variable | Description | Default |
|---|---|---|
DYNORM_TABLE_PREFIX | Prefix added to all table names | "" |
AWS_REGION | AWS region for DynamoDB | Required |
Table Prefix¶
Use the table prefix to separate environments:
# Development
export DYNORM_TABLE_PREFIX=dev-
# Staging
export DYNORM_TABLE_PREFIX=staging-
# Production
export DYNORM_TABLE_PREFIX=prod-
With DYNORM_TABLE_PREFIX=prod-:
Userentity →prod-userstableCreditCardentity →prod-credit_cardstable
Programmatic Configuration¶
Set Table Prefix¶
Get Current Prefix¶
Set Custom DynamoDB Client¶
For testing or custom configuration:
import (
"context"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/go-gamma/dynorm"
)
func init() {
cfg, _ := config.LoadDefaultConfig(context.Background(),
config.WithRegion("us-east-1"),
)
client := dynamodb.NewFromConfig(cfg)
dynorm.SetClient(client)
}
Set Context¶
For Lambda handlers or request-scoped context:
func handler(ctx context.Context, event events.APIGatewayProxyRequest) {
dynorm.SetContext(ctx)
// Now all operations use this context
user, err := UserRepo.Find("...")
}
Context is auto-captured
In most cases, Dynorm automatically captures context from the first operation. Manual context setting is rarely needed.
Reset Configuration¶
For testing, reset all configuration to defaults:
func TestSomething(t *testing.T) {
defer dynorm.Reset()
dynorm.SetTablePrefix("test-")
// ... test code
}
Not thread-safe
Reset() is not safe for concurrent use. Only call it in test setup/teardown when no other goroutines are accessing Dynorm.
Lambda Configuration¶
Recommended Setup¶
package main
import (
"context"
"os"
"github.com/aws/aws-lambda-go/lambda"
"github.com/go-gamma/dynorm"
)
func init() {
// Set prefix from environment (optional)
if prefix := os.Getenv("TABLE_PREFIX"); prefix != "" {
dynorm.SetTablePrefix(prefix)
}
}
func handler(ctx context.Context, event MyEvent) (Response, error) {
dynorm.SetContext(ctx)
// Your handler logic
user, err := UserRepo.Find(event.UserID)
// ...
}
func main() {
lambda.Start(handler)
}
Environment Variables in Lambda¶
Configure via AWS Lambda console or SAM/CDK:
Table Name Resolution¶
Table names are resolved in this order:
- Explicit tag:
table:"custom_name"on repository struct - Type name: Entity type name → pluralized snake_case
- Prefix:
DYNORM_TABLE_PREFIXprepended
// 1. Explicit tag overrides everything
type MyRepo struct {
dynorm.Repository[User] `table:"my_users"`
}
// → "prod-my_users" (with prefix)
// 2. Type name conversion
type UserRepository struct {
dynorm.Repository[User]
}
// User → users → "prod-users" (with prefix)
type CreditCardRepository struct {
dynorm.Repository[CreditCard]
}
// CreditCard → credit_cards → "prod-credit_cards" (with prefix)
Next Steps¶
- Entity Pattern - Learn about entity structure
- Repository Pattern - Repository operations