core

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2025 License: Apache-2.0 Imports: 6 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 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 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"

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

	// 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
}

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

	// 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
	BatchGet(keys []any, dest any) error

	// 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 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) 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