Documentation
¶
Overview ¶
Package dynamodb provides a typed DynamoDB integration block. It wraps the AWS SDK v2 DynamoDB client and exposes a minimal, strongly-typed CRUD API. The block is generic over T, the item model struct.
Index ¶
- type Block
- func (b *Block[T]) DeleteItem(ctx context.Context, partitionKeyValue, sortKeyValue any) error
- func (b *Block[T]) GetItem(ctx context.Context, partitionKeyValue, sortKeyValue any) (T, error)
- func (b *Block[T]) Init(ctx context.Context) error
- func (b *Block[T]) Name() string
- func (b *Block[T]) PutItem(ctx context.Context, item T) error
- func (b *Block[T]) QueryItems(ctx context.Context, in QueryInput) (Page[T], error)
- func (b *Block[T]) ScanItems(ctx context.Context, limit *int32, lastKey map[string]types.AttributeValue) (Page[T], error)
- func (b *Block[T]) Shutdown(_ context.Context) error
- type Option
- type Page
- type QueryInput
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Block ¶
type Block[T any] struct { // contains filtered or unexported fields }
Block is a typed DynamoDB integration block.
T is the Go struct that maps to a DynamoDB item via the `dynamodbav` struct tag. All CRUD operations are strongly typed over T, eliminating manual marshalling.
func New ¶
New creates a new DynamoDB Block with the given name and options.
block := dynamodb.New[User]("users",
dynamodb.WithRegion("us-east-1"),
dynamodb.WithTable("users-prod"),
dynamodb.WithPartitionKey("id"),
)
func (*Block[T]) DeleteItem ¶
DeleteItem removes a single item by its primary key. sortKeyValue is ignored when the table has no sort key.
func (*Block[T]) GetItem ¶
GetItem retrieves a single item by its primary key. sortKeyValue is ignored when the table has no sort key (WithSortKey was not set). Returns core.ErrItemNotFound when the item does not exist.
func (*Block[T]) Init ¶
Init implements core.Block. It resolves the AWS config and creates the DynamoDB client. A custom endpoint is applied here if WithEndpoint was set.
func (*Block[T]) PutItem ¶
PutItem marshals item into a DynamoDB AttributeValue map and writes it to the configured table. It performs a full replace if the key already exists.
func (*Block[T]) QueryItems ¶
QueryItems executes a DynamoDB Query and returns a paginated Page[T]. Use QueryInput.LastEvaluatedKey from a previous Page to fetch the next page.
type Option ¶
type Option func(*blockConfig)
Option configures a DynamoDB Block.
func WithAWSConfig ¶
WithAWSConfig injects a pre-built aws.Config, bypassing automatic resolution.
func WithEndpoint ¶
WithEndpoint overrides the DynamoDB endpoint (e.g. "http://localhost:8000" for DynamoDB Local or LocalStack).
func WithPartitionKey ¶
WithPartitionKey sets the attribute name of the table's partition key.
func WithProfile ¶
WithProfile selects a named AWS credentials profile.
func WithRegion ¶
WithRegion sets the AWS region used to resolve credentials and endpoints.
func WithSortKey ¶
WithSortKey sets the attribute name of the table's sort key. Omit this option for single-key tables.
type Page ¶
type Page[T any] struct { Items []T LastEvaluatedKey map[string]types.AttributeValue Count int32 ScannedCount int32 }
Page is a paginated result set.
type QueryInput ¶
type QueryInput struct {
// KeyConditionExpression is required (e.g. "id = :pk AND begins_with(sk, :prefix)").
KeyConditionExpression string
// ExpressionAttributeNames maps placeholder tokens to actual attribute names.
ExpressionAttributeNames map[string]string
// ExpressionAttributeValues maps value placeholders to AttributeValues.
ExpressionAttributeValues map[string]types.AttributeValue
// FilterExpression is an optional server-side filter applied after the query.
FilterExpression string
// IndexName targets a Global or Local Secondary Index instead of the base table.
IndexName string
// Limit caps the number of items evaluated (not returned) per call.
Limit *int32
// ScanIndexForward controls sort order; nil defaults to ascending.
ScanIndexForward *bool
// LastEvaluatedKey is the pagination token returned by the previous call.
LastEvaluatedKey map[string]types.AttributeValue
}
QueryInput parametrises a DynamoDB Query operation.