dynamo

package
v1.8.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(tableName, key string, sortKey ...string) error

Delete is a utility function that removes an item from a DynamoDB table by its key. This function provides a convenient wrapper around the DynamoDB Delete operation for simple deletion scenarios.

Parameters:

  • tableName: Name of the DynamoDB table to delete the item from
  • key: The partition key value that uniquely identifies the item to delete
  • sortKey: Optional variadic parameter for the sort key value when the table uses composite keys

Returns:

  • error: Returns an error if the table doesn't exist or the delete operation fails

This function will succeed even if the item doesn't exist (DynamoDB doesn't return an error for deleting non-existent items). It automatically handles the table lookup and delegates to the table's Delete method.

func Get

func Get[T any](tableName string, key string, sortKey ...string) (*T, *time.Time, error)

Get is a generic utility function that retrieves an item from a DynamoDB table and returns it as a strongly-typed pointer. This function provides a convenient wrapper around the DynamoDB Get operation with automatic type conversion.

Type Parameter:

  • T: The type to unmarshal the retrieved item into. Must be compatible with the stored data format.

Parameters:

  • tableName: Name of the DynamoDB table to retrieve from
  • key: The partition key value that uniquely identifies the item
  • sortKey: Optional variadic parameter for the sort key value when the table uses composite keys

Returns:

  • *T: A pointer to the retrieved and unmarshaled item, or nil if the item doesn't exist
  • *expirationTime: A pointer to the time the item will expire, or nil if the item doesn't have a TTL
  • error: Returns an error if the retrieval fails, table doesn't exist, or unmarshaling fails

The function automatically handles different storage modes (JSON vs native DynamoDB types) and returns nil without error if the requested item doesn't exist in the table.

func GetDynamoClient

func GetDynamoClient(region string) (*dynamodb.Client, error)

GetDynamoClient returns a singleton DynamoDB client for the specified AWS region. This function implements lazy initialization and reuses the same client instance for subsequent calls to improve performance and resource utilization.

Parameters:

  • region: The AWS region identifier (e.g., "us-east-1", "af-south-1") where the DynamoDB service should be accessed

Returns:

  • *dynamodb.Client: A configured DynamoDB client ready for use
  • error: Returns an error if the AWS configuration cannot be loaded or the client cannot be created

func GetInt

func GetInt(tableName, key string) (int, *time.Time, error)

GetInt is a utility function that retrieves an integer value from a DynamoDB table. This function expects the stored value to be a string representation of an integer and automatically converts it using strconv.Atoi.

Parameters:

  • tableName: Name of the DynamoDB table to retrieve from
  • key: The partition key value that uniquely identifies the item

Returns:

  • int: The integer value converted from the stored string, or 0 if not found
  • *expirationTime: A pointer to the time the item will expire, or nil if the item doesn't have a TTL
  • error: Returns an error if retrieval fails or the value cannot be converted to integer

func GetString

func GetString(tableName, key string, sortKey ...string) (string, *time.Time, error)

GetString is a utility function that retrieves a string value from a DynamoDB table. This is a convenience function for cases where you know the stored value is a string and want to avoid the overhead of generic type parameters.

Parameters:

  • tableName: Name of the DynamoDB table to retrieve from
  • key: The partition key value that uniquely identifies the item

Returns:

  • string: The string value stored in the table, or empty string if not found
  • *expirationTime: A pointer to the time the item will expire, or nil if the item doesn't have a TTL
  • error: Returns an error if the retrieval fails or the value cannot be converted to string

func Put

func Put(tableName, key string, value any, options ...PutOptions) error

Put is a utility function that stores an item in a DynamoDB table using the specified key. This function provides a convenient wrapper around the DynamoDB Put operation for simple key-value storage scenarios.

Parameters:

  • tableName: Name of the DynamoDB table to store the item in
  • key: The partition key value that will uniquely identify the stored item
  • value: The data to store - can be any type that can be marshaled to DynamoDB format
  • options: Optional SetOptions to configure TTL, sort keys, or other storage settings

Returns:

  • error: Returns an error if the table doesn't exist or the put operation fails

This function automatically handles the table lookup and delegates to the table's Put method.

Types

type DbOptions

type DbOptions struct {
	Region                string         // AWS region for the DynamoDB service
	TableName             string         // Name of the DynamoDB table
	PartitionKeyAttribute string         // Name of the partition key attribute
	TtlAttribute          string         // Name of the TTL attribute for automatic item expiration
	SortKeyAttribute      string         // Name of the sort key attribute (optional)
	ValueStoreMode        ValueStoreMode // Storage mode for values (JSON or attributes)
	ValueAttribute        string         // Name of the attribute that stores the value
	Ttl                   time.Duration  // Default TTL for items
}

DbOptions contains configuration options for creating a new DynamoDB connection

type DynamoDB

type DynamoDB struct {
	// contains filtered or unexported fields
}

DynamoDB represents a configured DynamoDB table connection with all necessary settings for performing operations on a specific table

func New

func New(options ...DbOptions) (*DynamoDB, error)

New creates a new DynamoDB instance with the provided configuration options. It initializes the AWS DynamoDB client and stores the instance in the global tableMap for reuse.

Parameters:

  • options: Variable number of DbOptions to configure the DynamoDB instance

Returns:

  • *DynamoDB: Configured DynamoDB instance
  • error: Error if table name is missing or client creation fails

Example:

db, err := New(DbOptions{
    TableName:        "my-table",
    Region:           "us-east-1",
    ValueStoreMode:   ValueStoreModeAttributes,
    SortKeyAttribute: "sk",
})

func (*DynamoDB) Delete

func (d *DynamoDB) Delete(key string, sortKey ...string) error

Delete removes an item from the DynamoDB table by its partition key and optional sort key. It uses the DeleteItem operation to permanently remove the item from the table.

Parameters:

  • key: The partition key value that uniquely identifies the item (or combined with sort key)
  • sortKey: Optional variadic parameter for the sort key value. If the table has a sort key attribute configured, the first sortKey value will be used. If no sortKey is provided but the table requires one, "null" will be used as the default value.

Returns:

  • error: Returns an error if the delete operation fails due to network issues, permission problems, or other DynamoDB service errors. Returns nil on success.

Note: This operation will succeed even if the item doesn't exist (DynamoDB doesn't return an error for deleting non-existent items).

func (*DynamoDB) Get

func (d *DynamoDB) Get(key string, options ...GetOptions) (any, *time.Time, error)

Get retrieves a single item from DynamoDB using the partition key and optional sort key. The function supports TTL (Time To Live) checking and returns nil for expired items. It supports both JSON and attribute value store modes.

Parameters:

  • key: The partition key value to retrieve
  • options: Optional GetOptions containing sort key and result type information

Returns:

  • any: The retrieved item (type depends on value store mode)
  • *time.Time: The expiration time of the item
  • error: Error if retrieval fails

Behavior:

  • Returns nil if item doesn't exist
  • Returns empty string (JSON mode) or nil (attribute mode) for expired items
  • JSON mode: Returns the value from the configured value attribute
  • Attribute mode: Returns the entire item unmarshaled into the provided result type

Example:

// Simple get
item, expiry, err := db.Get("user123")

// Get with sort key and typed result
result, expiry, err := db.Get("user123", GetOptions{
    SortKey: "profile",
    Result:  &Person{},
})

func (*DynamoDB) Put

func (d *DynamoDB) Put(key string, value any, options ...PutOptions) error

Put stores a complete item in DynamoDB, replacing any existing item with the same key. It supports both JSON and attribute value store modes and automatically handles type conversion. The function uses DynamoDB's PutItem operation which performs a complete item replacement.

Parameters:

  • key: The partition key value for the item
  • value: The data to store (can be any serializable type)
  • options: Optional SetOptions containing sort key and TTL information

Returns:

  • error: Error if storage fails

Storage Modes:

  • JSON Mode: Serializes value to JSON and stores in the configured value attribute
  • Attribute Mode: Maps struct fields directly to DynamoDB attributes using dynamodbav tags

Type Support (JSON Mode):

  • Structs, pointers, interfaces, maps, slices, arrays: JSON serialized
  • Strings: Stored directly
  • Integers: Converted to string representation
  • Floats: Converted to string representation
  • Booleans: Converted to string representation

Features:

  • Complete item replacement (unlike Update which does partial updates)
  • Automatic type conversion based on reflection
  • TTL support for automatic item expiration
  • Sort key support for composite primary keys
  • Handles both simple and complex data types

Example:

// Store a struct (attribute mode)
person := Person{Name: "John", Email: "john@example.com"}
err := db.Put("user123", person, SetOptions{
    SortKey: "profile",
    Ttl:     24 * time.Hour,
})

// Store simple string (JSON mode)
err := db.Put("cache_key", "cached_value")

// Store complex data with expiration
data := map[string]interface{}{
    "settings": map[string]string{"theme": "dark"},
    "lastLogin": time.Now(),
}
err := db.Put("user123", data, SetOptions{
    SortKey: "settings",
    Ttl:     7 * 24 * time.Hour,
})

func (*DynamoDB) Query

func (d *DynamoDB) Query(key string, options ...QueryOptions) ([]QueryResult[any], error)

Query retrieves multiple items from DynamoDB using the partition key and optional sort key conditions. It performs efficient queries using DynamoDB's Query operation (not Scan) and supports various sort key conditions like begins_with, equals, greater_than, etc.

Parameters:

  • key: The partition key value to query
  • options: Optional QueryOptions containing sort key conditions and result type information

Returns:

  • []QueryResult[any]: Slice of retrieved items (type depends on value store mode)
  • error: Error if query fails

Features:

  • Automatic TTL filtering (expired items are excluded)
  • Support for multiple sort key conditions (begins_with, equals, comparisons)
  • Both JSON and attribute value store modes supported
  • Efficient DynamoDB Query operation (not table scan)
  • Configurable result limits

Sort Key Conditions:

  • QueryConditionEquals: Exact match
  • QueryConditionBeginsWith: Prefix match
  • QueryConditionGreaterThan/LessThan: Comparison operators
  • QueryConditionGreaterThanOrEqualTo/LessThanOrEqualTo: Inclusive comparisons

Example:

// Query all items for a partition key
items, err := db.Query("user123")

// Query with sort key condition
sessions, err := db.Query("user123", QueryOptions{
    SortKeyCondition: QueryConditionBeginsWith,
    SortKey:          "session_",
})

// Query with typed result for attribute mode
people, err := db.Query("company1", QueryOptions{
    Result: &Person{},
})

func (*DynamoDB) Update

func (d *DynamoDB) Update(key string, value any, options ...PutOptions) error

Update performs partial updates to existing DynamoDB items using the efficient UpdateItem operation. It only updates the fields provided in the value parameter, leaving other fields unchanged. The function uses DynamoDB's UpdateExpression with SET operations for optimal performance.

Parameters:

  • key: The partition key of the item to update
  • value: Struct containing the fields to update (uses dynamodbav tags for field mapping)
  • options: Optional SetOptions containing sort key and TTL information

Returns:

  • error: Error if update fails

Features:

  • Partial updates: Only modifies specified fields
  • Automatic key protection: Partition/sort keys cannot be updated
  • TTL support: Can set/update expiration times
  • Type safety: Uses struct tags for field mapping
  • Efficient: Uses DynamoDB's native UpdateItem operation
  • Upsert behavior: Creates item if it doesn't exist (DynamoDB default behavior)

Field Mapping:

The function uses Go struct tags to map fields to DynamoDB attributes:
- `dynamodbav:"field_name"` - Maps struct field to DynamoDB attribute
- Fields without tags use the struct field name
- Partition/sort key fields are automatically skipped

Example:

// Partial update - only email field
update := struct {
    Email string `dynamodbav:"email"`
}{
    Email: "new@email.com",
}
err := db.Update("user123", update, SetOptions{SortKey: "profile"})

// Multiple field update with TTL
person := Person{
    Name:  "New Name",
    Email: "new@email.com",
}
err := db.Update("user123", person, SetOptions{
    SortKey: "profile",
    Ttl:     1 * time.Hour,
})

type GetOptions

type GetOptions struct {
	SortKey string // Sort key value for tables with composite keys
	Result  any    // Pointer to struct where the result will be unmarshaled
}

GetOptions contains options for DynamoDB Get operations

type PutOptions added in v1.3.6

type PutOptions struct {
	Ttl     time.Duration // TTL for the item (overrides default table TTL)
	SortKey string        // Sort key value for tables with composite keys
}

PutOptions contains options for DynamoDB Put and Update operations

type QueryCondition

type QueryCondition string

QueryCondition defines the types of conditions that can be applied to sort keys in DynamoDB queries

const (
	// QueryConditionNone indicates no condition should be applied to the sort key
	QueryConditionNone QueryCondition = "none"
	// QueryConditionBeginsWith checks if the sort key begins with a specific value
	QueryConditionBeginsWith QueryCondition = "beginsWith"
	// QueryConditionEndsWith checks if the sort key ends with a specific value
	QueryConditionEndsWith QueryCondition = "endsWith"
	// QueryConditionContains checks if the sort key contains a specific value
	QueryConditionContains QueryCondition = "contains"
	// QueryConditionEquals checks if the sort key equals a specific value
	QueryConditionEquals QueryCondition = "equals"
	// QueryConditionNotEquals checks if the sort key does not equal a specific value
	QueryConditionNotEquals QueryCondition = "notEquals"
	// QueryConditionGreaterThan checks if the sort key is greater than a specific value
	QueryConditionGreaterThan QueryCondition = "greaterThan"
	// QueryConditionLessThan checks if the sort key is less than a specific value
	QueryConditionLessThan QueryCondition = "lessThan"
	// QueryConditionGreaterThanOrEqualTo checks if the sort key is greater than or equal to a specific value
	QueryConditionGreaterThanOrEqualTo QueryCondition = "greaterThanOrEqualTo"
	// QueryConditionLessThanOrEqualTo checks if the sort key is less than or equal to a specific value
	QueryConditionLessThanOrEqualTo QueryCondition = "lessThanOrEqualTo"
)

type QueryOptions

type QueryOptions struct {
	Result                any            // Pointer to struct where the results will be unmarshaled
	SortKeyValue          string         // Sort key value to apply the condition against
	PartitionKeyCondition QueryCondition // Condition to apply to the partition key (usually equals)
	SortKeyCondition      QueryCondition // Condition to apply to the sort key
	Limit                 int            // Maximum number of items to return (0 = no limit)
}

QueryOptions contains options for DynamoDB Query operations

type QueryResult added in v1.6.0

type QueryResult[T interface{}] struct {
	Value   T
	Expiry  *time.Time
	SortKey string
}

func Query

func Query[T any](tableName string, key string, options ...QueryOptions) ([]QueryResult[T], error)

Query is a generic utility function that performs a DynamoDB Query operation and returns a slice of strongly-typed items. This function provides a convenient wrapper around the DynamoDB Query operation with automatic type conversion for multiple results.

Type Parameter:

  • T: The type to unmarshal each retrieved item into. Must be compatible with the stored data format.

Parameters:

  • tableName: Name of the DynamoDB table to query
  • key: The partition key value to query for
  • options: Optional QueryOptions to specify sort key conditions, filters, limits, etc.

Returns:

  • []QueryResult[T]: A slice of retrieved and unmarshaled items matching the query criteria
  • error: Returns an error if the query fails, table doesn't exist, or unmarshaling fails

The function automatically handles type conversion and returns an empty slice if no items match the query criteria.

type ValueStoreMode

type ValueStoreMode string

ValueStoreMode defines how values are stored in DynamoDB tables

const (
	// ValueStoreModeJson stores values as JSON strings in a single attribute
	ValueStoreModeJson ValueStoreMode = "json"
	// ValueStoreModeAttributes stores values as native DynamoDB attributes (maps, lists, etc.)
	ValueStoreModeAttributes ValueStoreMode = "attributes"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL