Skip to content

Latest commit

 

History

History
157 lines (113 loc) · 3.26 KB

File metadata and controls

157 lines (113 loc) · 3.26 KB

Getting Started with DynamORM

This guide walks you through installing, configuring, and deploying the Go implementation of DynamORM.

For the multi-language monorepo:

  • TypeScript: ts/docs/getting-started.md
  • Python: py/docs/getting-started.md

Prerequisites

Required:

  • Go 1.25 or higher
  • AWS Credentials configured (or IAM role in Lambda)
  • Basic understanding of DynamoDB (Primary Keys, Tables)

Recommended:

  • AWS CLI installed for verification
  • Docker (if using DynamoDB Local)

Installation

Step 1: Add Dependency

# Add DynamORM to your project
go get github.com/pay-theory/dynamorm

What this does:

  • Downloads the library and its dependencies (including AWS SDK v2)
  • Updates your go.mod and go.sum files

Step 2: Define Your Model

Create a struct that represents your DynamoDB item.

package models

import "time"

// CORRECT: Use dynamorm tags for keys
type User struct {
    ID        string    `dynamorm:"pk" json:"id"`           // Partition Key
    Email     string    `dynamorm:"sk" json:"email"`        // Sort Key
    Name      string    `json:"name"`
    CreatedAt time.Time `json:"created_at"`
}

What this does:

  • Defines the data structure
  • Tells DynamORM which fields are Primary Keys (pk, sk)

First Deployment

Option A: Lambda Function (Recommended)

Use this for serverless applications to get sub-15ms cold starts.

package main

import (
    "github.com/aws/aws-lambda-go/lambda"
    "github.com/pay-theory/dynamorm"
    "log"
)

// Global variable for connection reuse
var db *dynamorm.LambdaDB

func init() {
    var err error
    // Initialize once during cold start
    db, err = dynamorm.NewLambdaOptimized()
    if err != nil {
        log.Fatal(err)
    }
}

func handler() (string, error) {
    // Use the pre-warmed connection
    return "Connected!", nil
}

func main() {
    lambda.Start(handler)
}

Option B: Standard Application / Local Dev

Use this for containers, CLI tools, or local testing.

package main

import (
    "github.com/pay-theory/dynamorm"
    "github.com/pay-theory/dynamorm/pkg/session"
    "log"
)

func main() {
    // Standard initialization
    db, err := dynamorm.New(session.Config{
        Region: "us-east-1",
        // Uncomment for local DynamoDB:
        // Endpoint: "http://localhost:8000",
    })
    if err != nil {
        log.Fatal(err)
    }
}

Verification

Test your setup by creating an item.

// Create a user
user := &User{
    ID:        "user_123",
    Email:     "test@example.com",
    Name:      "Test User",
    CreatedAt: time.Now(),
}

err := db.Model(user).Create()
if err != nil {
    log.Printf("Error creating user: %v", err)
} else {
    log.Println("User created successfully!")
}

Next Steps

Troubleshooting

Issue: "ResourceNotFoundException"

  • Cause: The table "users" (derived from User struct) does not exist in AWS.
  • Solution: Create the table in DynamoDB or use db.CreateTable(&User{}) for development.

See full troubleshooting guide