core

package
v1.0.37 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: Apache-2.0 Imports: 8 Imported by: 4

Documentation

Overview

Package core defines the core interfaces and types for DynamORM

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AttributeMetadata

type AttributeMetadata struct {
	Name         string
	Type         string
	DynamoDBName string
	Tags         map[string]string
}

AttributeMetadata provides metadata about a model attribute

type BatchChunkErrorHandler added in v1.0.37

type BatchChunkErrorHandler func(chunk []any, err error) error

BatchChunkErrorHandler can intercept per-chunk failures. Return nil to swallow the error and continue.

type BatchDeleteResult

type BatchDeleteResult struct {
	Succeeded       int
	Failed          int
	UnprocessedKeys []map[string]types.AttributeValue
	Errors          []error
}

BatchDeleteResult represents the result of a batch delete operation

type BatchGetBuilder added in v1.0.37

type BatchGetBuilder interface {
	Keys(keys []any) BatchGetBuilder
	ChunkSize(size int) BatchGetBuilder
	ConsistentRead() BatchGetBuilder
	Parallel(maxConcurrency int) BatchGetBuilder
	WithRetry(policy *RetryPolicy) BatchGetBuilder
	Select(fields ...string) BatchGetBuilder
	OnProgress(callback BatchProgressCallback) BatchGetBuilder
	OnError(handler BatchChunkErrorHandler) BatchGetBuilder
	Execute(dest any) error
}

BatchGetBuilder exposes a fluent API for composing advanced BatchGet operations.

type BatchGetOptions added in v1.0.37

type BatchGetOptions struct {
	// ChunkSize limits how many keys are sent per BatchGetItem request (max 100).
	ChunkSize int
	// ConsistentRead enables strongly consistent reads.
	ConsistentRead bool
	// Parallel toggles concurrent chunk execution.
	Parallel bool
	// MaxConcurrency caps the number of concurrent BatchGetItem requests when Parallel is true.
	MaxConcurrency int
	// RetryPolicy controls handling of UnprocessedKeys and throttling responses.
	RetryPolicy *RetryPolicy
	// ProgressCallback receives updates after each chunk finishes.
	ProgressCallback BatchProgressCallback
	// OnChunkError is invoked when a chunk fails permanently.
	OnChunkError BatchChunkErrorHandler
}

BatchGetOptions tune the behavior of BatchGet operations.

func DefaultBatchGetOptions added in v1.0.37

func DefaultBatchGetOptions() *BatchGetOptions

DefaultBatchGetOptions returns a sensible baseline configuration.

func (*BatchGetOptions) Clone added in v1.0.37

func (o *BatchGetOptions) Clone() *BatchGetOptions

Clone returns a shallow copy of the options to decouple caller modifications from shared defaults.

type BatchProgressCallback added in v1.0.37

type BatchProgressCallback func(retrieved, total int)

BatchProgressCallback is invoked after each chunk completes with the total number of items retrieved so far.

type BatchWriteExecutor

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

BatchWriteExecutor implements batch write operations for DynamoDB

func NewBatchWriteExecutor

func NewBatchWriteExecutor(client *dynamodb.Client, ctx context.Context) *BatchWriteExecutor

NewBatchWriteExecutor creates a new batch write executor

func (*BatchWriteExecutor) BatchDeleteWithResult

func (e *BatchWriteExecutor) BatchDeleteWithResult(tableName string, keys []map[string]types.AttributeValue) (*BatchDeleteResult, error)

BatchDeleteWithResult performs batch delete and returns detailed results

func (*BatchWriteExecutor) ExecuteBatchWriteItem

func (e *BatchWriteExecutor) ExecuteBatchWriteItem(tableName string, writeRequests []types.WriteRequest) (*BatchWriteResult, error)

ExecuteBatchWriteItem executes a batch write operation

func (*BatchWriteExecutor) ExecuteQuery

func (e *BatchWriteExecutor) ExecuteQuery(input *CompiledQuery, dest any) error

ExecuteQuery implements the QueryExecutor interface

func (*BatchWriteExecutor) ExecuteScan

func (e *BatchWriteExecutor) ExecuteScan(input *CompiledQuery, dest any) error

ExecuteScan implements the QueryExecutor interface

type BatchWriteResult

type BatchWriteResult struct {
	UnprocessedItems map[string][]types.WriteRequest
	ConsumedCapacity []types.ConsumedCapacity
}

BatchWriteResult contains the result of a batch write operation

type CompiledQuery

type CompiledQuery struct {
	Operation string // "Query", "Scan", "GetItem", etc.
	TableName string
	IndexName string

	// Expression components
	KeyConditionExpression string
	FilterExpression       string
	ProjectionExpression   string
	UpdateExpression       string
	ConditionExpression    string

	// Expression mappings
	ExpressionAttributeNames  map[string]string
	ExpressionAttributeValues map[string]types.AttributeValue

	// Other query parameters
	Limit             *int32
	ExclusiveStartKey map[string]types.AttributeValue
	ScanIndexForward  *bool
	Select            string // "ALL_ATTRIBUTES", "COUNT", etc.
	Offset            *int   // For pagination handling
	ReturnValues      string // "NONE", "ALL_OLD", "UPDATED_OLD", "ALL_NEW", "UPDATED_NEW"
	ConsistentRead    *bool  // For strongly consistent reads

	// Parallel scan parameters
	Segment       *int32 // The segment number for parallel scan
	TotalSegments *int32 // Total number of segments for parallel scan
}

CompiledQuery represents a compiled query ready for execution

type ConsumedCapacity

type ConsumedCapacity struct {
	TableName          string
	CapacityUnits      float64
	ReadCapacityUnits  float64
	WriteCapacityUnits float64
}

ConsumedCapacity represents consumed capacity information

type DB

type DB interface {
	// Model returns a new query builder for the given model
	Model(model any) Query

	// Transaction executes a function within a database transaction
	Transaction(fn func(tx *Tx) error) error

	// Migrate runs all pending migrations
	Migrate() error

	// AutoMigrate creates or updates tables based on the given models
	AutoMigrate(models ...any) error

	// Close closes the database connection
	Close() error

	// WithContext returns a new DB instance with the given context
	WithContext(ctx context.Context) DB
}

DB represents the main database connection interface

type DynamoDBUpdateAPI

type DynamoDBUpdateAPI interface {
	UpdateItem(ctx context.Context, params *dynamodb.UpdateItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.UpdateItemOutput, error)
}

DynamoDBUpdateAPI defines the interface for DynamoDB update operations

type ExecutorWithBatchSupport

type ExecutorWithBatchSupport struct {
	*UpdateExecutor
	*BatchWriteExecutor
	// contains filtered or unexported fields
}

ExecutorWithBatchSupport wraps an executor to add batch write support

func NewExecutorWithBatchSupport

func NewExecutorWithBatchSupport(client *dynamodb.Client, ctx context.Context) *ExecutorWithBatchSupport

NewExecutorWithBatchSupport creates a new executor with batch support

func (*ExecutorWithBatchSupport) ExecuteDeleteItem

func (e *ExecutorWithBatchSupport) ExecuteDeleteItem(input *CompiledQuery, key map[string]types.AttributeValue) error

ExecuteDeleteItem implements DeleteItemExecutor interface

func (*ExecutorWithBatchSupport) ExecutePutItem

func (e *ExecutorWithBatchSupport) ExecutePutItem(input *CompiledQuery, item map[string]types.AttributeValue) error

ExecutePutItem implements PutItemExecutor interface

type ExtendedDB added in v1.0.1

type ExtendedDB interface {
	DB

	// AutoMigrateWithOptions performs enhanced auto-migration with data copy support
	// opts should be of type schema.AutoMigrateOption
	AutoMigrateWithOptions(model any, opts ...any) error

	// RegisterTypeConverter registers a custom converter for a specific Go type, allowing
	// callers to override how values are marshaled to and unmarshaled from DynamoDB.
	RegisterTypeConverter(typ reflect.Type, converter pkgTypes.CustomConverter) error

	// CreateTable creates a DynamoDB table for the given model
	// opts should be of type schema.TableOption
	CreateTable(model any, opts ...any) error

	// EnsureTable checks if a table exists for the model and creates it if not
	EnsureTable(model any) error

	// DeleteTable deletes the DynamoDB table for the given model
	DeleteTable(model any) error

	// DescribeTable returns the table description for the given model
	// Returns *types.TableDescription
	DescribeTable(model any) (any, error)

	// WithLambdaTimeout sets a deadline based on Lambda context
	WithLambdaTimeout(ctx context.Context) DB

	// WithLambdaTimeoutBuffer sets a custom timeout buffer for Lambda execution
	WithLambdaTimeoutBuffer(buffer time.Duration) DB

	// TransactionFunc executes a function within a full transaction context
	// tx should be of type *transaction.Transaction
	TransactionFunc(fn func(tx any) error) error

	// Transact returns a fluent transaction builder for composing TransactWriteItems
	Transact() TransactionBuilder

	// TransactWrite executes the provided function within a transaction builder context
	// and automatically commits the accumulated operations.
	TransactWrite(ctx context.Context, fn func(TransactionBuilder) error) error
}

ExtendedDB represents the full database interface with all available methods This interface includes schema management and Lambda-specific features

type IndexSchema

type IndexSchema struct {
	Name            string
	Type            string // "GSI" or "LSI"
	PartitionKey    string
	SortKey         string
	ProjectionType  string
	ProjectedFields []string
}

IndexSchema represents a GSI or LSI schema

type KeyPair added in v1.0.37

type KeyPair struct {
	PartitionKey any
	SortKey      any
}

KeyPair lets callers supply composite keys without defining ad-hoc structs.

func NewKeyPair added in v1.0.37

func NewKeyPair(partitionKey any, sortKey ...any) KeyPair

NewKeyPair constructs a KeyPair with the optional sort key.

type KeySchema

type KeySchema struct {
	PartitionKey string
	SortKey      string // optional
}

KeySchema represents a primary key or index key schema

type ModelMetadata

type ModelMetadata interface {
	TableName() string
	PrimaryKey() KeySchema
	Indexes() []IndexSchema
	AttributeMetadata(field string) *AttributeMetadata
}

ModelMetadata provides metadata about a model

type PaginatedResult

type PaginatedResult struct {
	// Items contains the retrieved items
	Items any

	// Count is the number of items returned
	Count int

	// ScannedCount is the number of items examined
	ScannedCount int

	// LastEvaluatedKey is the key of the last item evaluated
	LastEvaluatedKey map[string]types.AttributeValue

	// NextCursor is a base64-encoded cursor for the next page
	NextCursor string

	// HasMore indicates if there are more results
	HasMore bool
}

PaginatedResult contains the results and pagination metadata

type Param

type Param struct {
	Name  string
	Value any
}

Param represents a parameter for expressions

type Query

type Query interface {
	// Query construction
	Where(field string, op string, value any) Query
	Index(indexName string) Query
	Filter(field string, op string, value any) Query
	OrFilter(field string, op string, value any) Query
	FilterGroup(func(Query)) Query
	OrFilterGroup(func(Query)) Query
	// IfNotExists ensures the target item does not already exist before a write
	IfNotExists() Query
	// IfExists ensures the target item exists before executing a write
	IfExists() Query
	// WithCondition appends a simple condition expression for write operations
	WithCondition(field, operator string, value any) Query
	// WithConditionExpression adds a raw condition expression with placeholder values
	WithConditionExpression(expr string, values map[string]any) Query
	OrderBy(field string, order string) Query
	Limit(limit int) Query

	// Offset sets the starting position for the query
	Offset(offset int) Query

	// Select specifies which fields to retrieve
	Select(fields ...string) Query

	// ConsistentRead enables strongly consistent reads for Query operations
	// Note: This only works on main table queries, not GSI queries
	ConsistentRead() Query

	// WithRetry configures retry behavior for eventually consistent reads
	// Useful for GSI queries where you need read-after-write consistency
	WithRetry(maxRetries int, initialDelay time.Duration) Query

	// First retrieves the first matching item
	First(dest any) error

	// All retrieves all matching items
	All(dest any) error

	// AllPaginated retrieves all matching items with pagination metadata
	AllPaginated(dest any) (*PaginatedResult, error)

	// Count returns the number of matching items
	Count() (int64, error)

	// Create creates a new item
	Create() error

	// CreateOrUpdate creates a new item or updates an existing one (upsert)
	CreateOrUpdate() error

	// Update updates the matching items
	Update(fields ...string) error

	// UpdateBuilder returns a builder for complex update operations
	UpdateBuilder() UpdateBuilder

	// Delete deletes the matching items
	Delete() error

	// Scan performs a table scan
	Scan(dest any) error

	// ParallelScan configures parallel scanning with segment and total segments
	ParallelScan(segment int32, totalSegments int32) Query

	// ScanAllSegments performs parallel scan across all segments automatically
	ScanAllSegments(dest any, totalSegments int32) error

	// BatchGet retrieves multiple items by their primary keys.
	// Keys may be primitives, structs matching the model schema, or core.KeyPair values.
	BatchGet(keys []any, dest any) error

	// BatchGetWithOptions retrieves items with fine-grained control over chunking, retries, and callbacks.
	BatchGetWithOptions(keys []any, dest any, opts *BatchGetOptions) error

	// BatchGetBuilder returns a fluent builder for complex batch get workflows.
	BatchGetBuilder() BatchGetBuilder

	// BatchCreate creates multiple items
	BatchCreate(items any) error

	// BatchDelete deletes multiple items by their primary keys
	BatchDelete(keys []any) error

	// BatchWrite performs mixed batch write operations (puts and deletes)
	BatchWrite(putItems []any, deleteKeys []any) error

	// BatchUpdateWithOptions performs batch update operations with custom options
	BatchUpdateWithOptions(items []any, fields []string, options ...any) error

	// Cursor sets the pagination cursor for the query
	Cursor(cursor string) Query

	// SetCursor sets the cursor from a string (alternative to Cursor)
	SetCursor(cursor string) error

	// WithContext sets the context for the query
	WithContext(ctx context.Context) Query
}

Query represents a chainable query builder interface

type RetryPolicy added in v1.0.37

type RetryPolicy struct {
	// MaxRetries is the maximum number of retry attempts before giving up.
	MaxRetries int
	// InitialDelay is the base delay between attempts.
	InitialDelay time.Duration
	// MaxDelay caps the exponential backoff delay.
	MaxDelay time.Duration
	// BackoffFactor controls how quickly the delay grows between attempts.
	BackoffFactor float64
	// Jitter adds randomness (as a percentage between 0 and 1) to each delay to avoid thundering herds.
	Jitter float64
}

RetryPolicy defines exponential backoff settings for retryable DynamoDB operations.

func DefaultRetryPolicy added in v1.0.37

func DefaultRetryPolicy() *RetryPolicy

DefaultRetryPolicy returns a conservative retry policy suitable for most batch operations.

func (*RetryPolicy) Clone added in v1.0.37

func (p *RetryPolicy) Clone() *RetryPolicy

Clone returns a deep copy of the policy so callers can modify it without affecting the original.

type TransactCondition added in v1.0.37

type TransactCondition struct {
	Kind       TransactConditionKind
	Field      string
	Operator   string
	Value      any
	Expression string
	Values     map[string]any
}

TransactCondition represents a condition attached to a transactional operation

type TransactConditionKind added in v1.0.37

type TransactConditionKind string

TransactConditionKind identifies the type of transactional condition

const (
	// TransactConditionKindField represents a simple field comparison (Field Operator Value)
	TransactConditionKindField TransactConditionKind = "field"
	// TransactConditionKindExpression represents a raw condition expression supplied by the caller
	TransactConditionKindExpression TransactConditionKind = "expression"
	// TransactConditionKindPrimaryKeyExists enforces that the primary key exists (attribute_exists)
	TransactConditionKindPrimaryKeyExists TransactConditionKind = "pk_exists"
	// TransactConditionKindPrimaryKeyNotExists enforces that the primary key does not exist (attribute_not_exists)
	TransactConditionKindPrimaryKeyNotExists TransactConditionKind = "pk_not_exists"
	// TransactConditionKindVersionEquals enforces that the optimistic lock/version field matches Value
	TransactConditionKindVersionEquals TransactConditionKind = "version"
)

type TransactionBuilder added in v1.0.37

type TransactionBuilder interface {
	// Put adds a put (upsert) operation
	Put(model any, conditions ...TransactCondition) TransactionBuilder
	// Create adds a put operation guarded by attribute_not_exists on the primary key
	Create(model any, conditions ...TransactCondition) TransactionBuilder
	// Update updates selected fields on the provided model
	Update(model any, fields []string, conditions ...TransactCondition) TransactionBuilder
	// UpdateWithBuilder allows complex expression-based updates
	UpdateWithBuilder(model any, updateFn func(UpdateBuilder) error, conditions ...TransactCondition) TransactionBuilder
	// Delete removes the provided model by primary key
	Delete(model any, conditions ...TransactCondition) TransactionBuilder
	// ConditionCheck adds a pure condition check without mutating data
	ConditionCheck(model any, conditions ...TransactCondition) TransactionBuilder
	// WithContext sets the context used for DynamoDB calls
	WithContext(ctx context.Context) TransactionBuilder
	// Execute commits the transaction using the currently configured context
	Execute() error
	// ExecuteWithContext commits the transaction with an explicit context override
	ExecuteWithContext(ctx context.Context) error
}

TransactionBuilder defines the fluent DSL for composing DynamoDB transactions

type Tx

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

Tx represents a database transaction

func (*Tx) Create

func (tx *Tx) Create(model any) error

Create creates a new item within the transaction

func (*Tx) Delete

func (tx *Tx) Delete(model any) error

Delete deletes an item within the transaction

func (*Tx) Model

func (tx *Tx) Model(model any) Query

Model returns a new query builder for the given model within the transaction

func (*Tx) SetDB added in v1.0.18

func (tx *Tx) SetDB(db DB)

SetDB sets the database reference for the transaction

func (*Tx) Update

func (tx *Tx) Update(model any, fields ...string) error

Update updates an item within the transaction

type UpdateBuilder

type UpdateBuilder interface {
	// Set updates a field to a new value
	Set(field string, value any) UpdateBuilder

	// SetIfNotExists sets a field value only if it doesn't already exist
	SetIfNotExists(field string, value any, defaultValue any) UpdateBuilder

	// Add performs atomic addition (for numbers) or adds to a set
	Add(field string, value any) UpdateBuilder

	// Increment increments a numeric field by 1
	Increment(field string) UpdateBuilder

	// Decrement decrements a numeric field by 1
	Decrement(field string) UpdateBuilder

	// Remove removes an attribute from the item
	Remove(field string) UpdateBuilder

	// Delete removes values from a set
	Delete(field string, value any) UpdateBuilder

	// AppendToList appends values to the end of a list
	AppendToList(field string, values any) UpdateBuilder

	// PrependToList prepends values to the beginning of a list
	PrependToList(field string, values any) UpdateBuilder

	// RemoveFromListAt removes an element at a specific index from a list
	RemoveFromListAt(field string, index int) UpdateBuilder

	// SetListElement sets a specific element in a list
	SetListElement(field string, index int, value any) UpdateBuilder

	// Condition adds a condition that must be met for the update to succeed
	Condition(field string, operator string, value any) UpdateBuilder

	// OrCondition adds a condition with OR logic
	OrCondition(field string, operator string, value any) UpdateBuilder

	// ConditionExists adds a condition that the field must exist
	ConditionExists(field string) UpdateBuilder

	// ConditionNotExists adds a condition that the field must not exist
	ConditionNotExists(field string) UpdateBuilder

	// ConditionVersion adds optimistic locking based on version
	ConditionVersion(currentVersion int64) UpdateBuilder

	// ReturnValues specifies what values to return after the update
	ReturnValues(option string) UpdateBuilder

	// Execute performs the update operation
	Execute() error

	// ExecuteWithResult performs the update and returns the result
	ExecuteWithResult(result any) error
}

UpdateBuilder represents a fluent interface for building update operations

type UpdateExecutor

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

UpdateExecutor implements UpdateItemExecutor interface for DynamoDB update operations

func NewUpdateExecutor

func NewUpdateExecutor(client DynamoDBUpdateAPI, ctx context.Context) *UpdateExecutor

NewUpdateExecutor creates a new UpdateExecutor instance

func (*UpdateExecutor) ExecuteUpdateItem

func (e *UpdateExecutor) ExecuteUpdateItem(input *CompiledQuery, key map[string]types.AttributeValue) error

ExecuteUpdateItem performs a DynamoDB UpdateItem operation

func (*UpdateExecutor) ExecuteUpdateItemWithResult

func (e *UpdateExecutor) ExecuteUpdateItemWithResult(input *CompiledQuery, key map[string]types.AttributeValue) (*UpdateResult, error)

ExecuteUpdateItemWithResult performs UpdateItem and returns the result

type UpdateResult

type UpdateResult struct {
	Attributes       map[string]types.AttributeValue
	ConsumedCapacity *ConsumedCapacity
}

UpdateResult represents the result of an UpdateItem operation

Jump to

Keyboard shortcuts

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