Skip to content

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-:

  • User entity → prod-users table
  • CreditCard entity → prod-credit_cards table

Programmatic Configuration

Set Table Prefix

import "github.com/go-gamma/dynorm"

func init() {
    dynorm.SetTablePrefix("myapp-")
}

Get Current Prefix

prefix := dynorm.GetTablePrefix()
fmt.Println(prefix) // "myapp-"

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

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:

Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: main
      Runtime: provided.al2023
      Environment:
        Variables:
          DYNORM_TABLE_PREFIX: !Sub "${Environment}-"
          AWS_REGION: !Ref AWS::Region
lambda.NewFunction(stack, jsii.String("MyFunction"), &lambda.FunctionProps{
    Runtime: lambda.Runtime_PROVIDED_AL2023(),
    Handler: jsii.String("main"),
    Environment: &map[string]*string{
        "DYNORM_TABLE_PREFIX": jsii.String(fmt.Sprintf("%s-", env)),
    },
})

Table Name Resolution

Table names are resolved in this order:

  1. Explicit tag: table:"custom_name" on repository struct
  2. Type name: Entity type name → pluralized snake_case
  3. Prefix: DYNORM_TABLE_PREFIX prepended
// 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