dynamodb

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AttributeDefinition

type AttributeDefinition struct {
	AttributeName string `json:"AttributeName"`
	AttributeType string `json:"AttributeType"` // S, N, or B
}

AttributeDefinition describes the type of a key attribute.

type AttributeValue

type AttributeValue = map[string]any

AttributeValue is the DynamoDB typed value format. We store it as map[string]any matching the JSON wire format. e.g., {"S": "hello"}, {"N": "42"}, {"BOOL": true}

type Backup

type Backup struct {
	BackupArn              string  `json:"BackupArn"`
	BackupName             string  `json:"BackupName"`
	BackupStatus           string  `json:"BackupStatus"`
	TableName              string  `json:"TableName"`
	TableArn               string  `json:"TableArn"`
	BackupCreationDateTime float64 `json:"BackupCreationDateTime"`
}

type ConditionExpr

type ConditionExpr interface {
	Evaluate(item Item) bool
}

ConditionExpr is a compiled condition expression that can be evaluated against a DynamoDB item with zero string parsing in the hot path.

func CompileCondition

func CompileCondition(expr string, names map[string]string, values map[string]AttributeValue) ConditionExpr

CompileCondition parses a condition expression string once, resolving #name placeholders from names and binding :value placeholders from values, returning a ConditionExpr whose Evaluate method does no string parsing.

type DynamoDBService

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

DynamoDBService is the cloudmock implementation of the AWS DynamoDB API.

func New

func New(accountID, region string) *DynamoDBService

New returns a new DynamoDBService for the given AWS account ID and region.

func (*DynamoDBService) Actions

func (s *DynamoDBService) Actions() []service.Action

Actions returns the list of DynamoDB API actions supported by this service.

func (*DynamoDBService) Close

func (s *DynamoDBService) Close()

Close stops the background TTL reaper goroutine started in New. It is safe to call multiple times. Long-lived servers never need this, but in-process embeddings (e.g. the SDK) call it on teardown to avoid leaking the goroutine.

func (*DynamoDBService) ExportState

func (s *DynamoDBService) ExportState() (json.RawMessage, error)

ExportState returns a JSON snapshot of all DynamoDB tables and items.

func (*DynamoDBService) GetTableNames

func (s *DynamoDBService) GetTableNames() []string

GetTableNames returns all table names for topology queries.

func (*DynamoDBService) HandleRequest

func (s *DynamoDBService) HandleRequest(ctx *service.RequestContext) (*service.Response, error)

HandleRequest routes an incoming DynamoDB request to the appropriate handler.

func (*DynamoDBService) HealthCheck

func (s *DynamoDBService) HealthCheck() error

HealthCheck always returns nil (no external dependencies).

func (*DynamoDBService) ImportState

func (s *DynamoDBService) ImportState(data json.RawMessage) error

ImportState restores DynamoDB state from a JSON snapshot.

func (*DynamoDBService) Name

func (s *DynamoDBService) Name() string

Name returns the AWS service name used for routing.

func (*DynamoDBService) ResourceSchemas

func (s *DynamoDBService) ResourceSchemas() []schema.ResourceSchema

ResourceSchemas returns the schema for DynamoDB table resources.

func (*DynamoDBService) TableKeySchema

func (s *DynamoDBService) TableKeySchema(name string) (hashKey, rangeKey string, gsis map[string][2]string, ok bool)

TableKeySchema reports a running table's key schema and GSIs for IaC drift detection. See (*TableStore).TableKeySchema.

type Export

type Export struct {
	ExportArn    string  `json:"ExportArn"`
	ExportStatus string  `json:"ExportStatus"`
	TableArn     string  `json:"TableArn"`
	S3Bucket     string  `json:"S3Bucket"`
	ExportFormat string  `json:"ExportFormat"`
	ExportTime   float64 `json:"ExportTime"`
}

type ExprCache

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

ExprCache caches compiled condition expressions keyed by the raw expression string. It is safe for concurrent use.

func NewExprCache

func NewExprCache() *ExprCache

NewExprCache creates a new expression cache.

func (*ExprCache) GetOrCompile

func (c *ExprCache) GetOrCompile(expr string, names map[string]string, values map[string]AttributeValue) ConditionExpr

GetOrCompile returns a cached ConditionExpr or compiles and caches a new one. NOTE: The cache key is the raw expression string. Because literal values are bound at compile time, different values maps with the same expression string will return the first-compiled version. This is correct when the same expression+values pair is always used together (the common DynamoDB pattern).

type GSI

type GSI struct {
	IndexName             string                 `json:"IndexName"`
	KeySchema             []KeySchemaElement     `json:"KeySchema"`
	Projection            map[string]any         `json:"Projection"`
	ProvisionedThroughput *ProvisionedThroughput `json:"ProvisionedThroughput,omitempty"`
}

GSI represents a Global Secondary Index definition.

type GlobalTable

type GlobalTable struct {
	GlobalTableName   string              `json:"GlobalTableName"`
	ReplicationGroup  []map[string]string `json:"ReplicationGroup"`
	GlobalTableArn    string              `json:"GlobalTableArn"`
	GlobalTableStatus string              `json:"GlobalTableStatus"`
	CreationDateTime  float64             `json:"CreationDateTime"`
}

type IndexStore

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

IndexStore mirrors the main table's partition structure for a secondary index.

type Item

type Item = map[string]AttributeValue

Item is a DynamoDB item: a map of attribute names to typed values.

func ApplyProjection

func ApplyProjection(item Item, projExpr string, names map[string]string) Item

ApplyProjection applies a projection expression to an item.

func ApplyUpdate

func ApplyUpdate(item Item, expr string, names map[string]string, values map[string]AttributeValue) Item

ApplyUpdate applies an update expression to an item, resolving names upfront. It delegates to the existing applySet/applyRemove logic.

type KeySchemaElement

type KeySchemaElement struct {
	AttributeName string `json:"AttributeName"`
	KeyType       string `json:"KeyType"` // HASH or RANGE
}

KeySchemaElement describes a single element of the table's key schema.

type LSI

type LSI struct {
	IndexName  string             `json:"IndexName"`
	KeySchema  []KeySchemaElement `json:"KeySchema"`
	Projection map[string]any     `json:"Projection"`
}

LSI represents a Local Secondary Index definition.

type Partition

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

Partition holds items sharing the same partition key value. For tables without a sort key, it holds at most one item. For tables with a sort key, items are stored in a B-tree sorted by sort key.

Frozen JSON cache: each item's GetItem JSON response is pre-serialized at write time and stored in frozenJSON. GetItemRaw returns the cached bytes with zero marshaling, eliminating the #1 CPU bottleneck (28% of CPU was spent in gojson.Marshal on reads).

type ProvisionedThroughput

type ProvisionedThroughput struct {
	ReadCapacityUnits  int64 `json:"ReadCapacityUnits"`
	WriteCapacityUnits int64 `json:"WriteCapacityUnits"`
}

ProvisionedThroughput holds the read/write capacity for a table.

type Shard

type Shard struct {
	ShardId string `json:"ShardId"`
}

Shard represents a shard in a DynamoDB Stream.

type Stream

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

Stream holds the in-memory state for a table's DynamoDB Stream.

type StreamDescription

type StreamDescription struct {
	StreamARN      string  `json:"StreamArn"`
	StreamLabel    string  `json:"StreamLabel"`
	StreamStatus   string  `json:"StreamStatus"` // ENABLED, DISABLED
	StreamViewType string  `json:"StreamViewType"`
	TableName      string  `json:"TableName"`
	Shards         []Shard `json:"Shards"`
}

StreamDescription holds metadata about a table's stream.

type StreamRecord

type StreamRecord struct {
	EventID        string `json:"eventID"`
	EventName      string `json:"eventName"` // INSERT, MODIFY, REMOVE
	NewImage       Item   `json:"NewImage,omitempty"`
	OldImage       Item   `json:"OldImage,omitempty"`
	SequenceNumber string `json:"SequenceNumber"`
	StreamViewType string `json:"StreamViewType"`
}

StreamRecord represents a single change event in a DynamoDB Stream.

type StreamSpecification

type StreamSpecification struct {
	StreamEnabled  bool   `json:"StreamEnabled"`
	StreamViewType string `json:"StreamViewType,omitempty"` // KEYS_ONLY, NEW_IMAGE, OLD_IMAGE, NEW_AND_OLD_IMAGES
}

StreamSpecification describes whether streams are enabled and the view type.

type TTLSpecification

type TTLSpecification struct {
	AttributeName string `json:"AttributeName"`
	Enabled       bool   `json:"Enabled"`
}

TTLSpecification describes the TTL configuration for a table.

type Table

type Table struct {
	Name                  string
	KeySchema             []KeySchemaElement
	AttributeDefinitions  []AttributeDefinition
	Status                string  // ACTIVE, CREATING, DELETING
	CreationDateTime      float64 // Unix timestamp
	BillingMode           string
	ProvisionedThroughput *ProvisionedThroughput
	GSIs                  []GSI
	LSIs                  []LSI
	Stream                *Stream           // nil if streams not enabled
	TTL                   *TTLSpecification // nil if TTL not configured

	// ResourcePolicy holds the table's resource-based policy JSON (set via PutResourcePolicy).
	ResourcePolicy           string
	ResourcePolicyRevisionID string

	// Tags holds the table's resource tags (key/value pairs).
	Tags map[string]string
	// contains filtered or unexported fields
}

Table is the in-memory representation of a DynamoDB table.

type TableStore

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

TableStore manages all DynamoDB tables in memory.

func NewTableStore

func NewTableStore(accountID, region string) *TableStore

NewTableStore creates an empty TableStore.

func (*TableStore) CreateTable

func (s *TableStore) CreateTable(name string, keySchema []KeySchemaElement, attrDefs []AttributeDefinition, billingMode string, pt *ProvisionedThroughput, gsis []GSI, lsis []LSI, streamSpec *StreamSpecification) (*Table, *service.AWSError)

CreateTable creates a new table. Returns ResourceInUseException if it already exists.

func (*TableStore) DeleteItem

func (s *TableStore) DeleteItem(tableName string, key Item, condExpr ...string) *service.AWSError

DeleteItem removes an item by key from the specified table.

func (*TableStore) DeleteResourcePolicy

func (s *TableStore) DeleteResourcePolicy(resourceARN string) *service.AWSError

DeleteResourcePolicy removes the resource policy from a table.

func (*TableStore) DeleteTable

func (s *TableStore) DeleteTable(name string) (*Table, *service.AWSError)

DeleteTable removes a table. Returns ResourceNotFoundException if not found.

func (*TableStore) DescribeTable

func (s *TableStore) DescribeTable(name string) (*Table, *service.AWSError)

DescribeTable returns table metadata. Returns ResourceNotFoundException if not found.

func (*TableStore) DescribeTimeToLive

func (s *TableStore) DescribeTimeToLive(tableName string) (*TTLSpecification, *service.AWSError)

DescribeTimeToLive returns the TTL configuration for a table.

func (*TableStore) GetItem

func (s *TableStore) GetItem(tableName string, key Item, projExpr string, exprNames map[string]string) (Item, *service.AWSError)

GetItem retrieves an item by key from the specified table.

func (*TableStore) GetItemRaw

func (s *TableStore) GetItemRaw(tableName string, key Item) ([]byte, *service.AWSError)

GetItemRaw retrieves an item and marshals it to JSON while holding the partition lock, eliminating the need for copyItem. Returns nil for not found.

func (*TableStore) GetResourcePolicy

func (s *TableStore) GetResourcePolicy(resourceARN string) (*Table, *service.AWSError)

GetResourcePolicy returns the resource policy for a table.

func (*TableStore) GetStream

func (s *TableStore) GetStream(tableName string) (*Stream, *service.AWSError)

GetStream returns the stream for a table, or nil if streams are not enabled.

func (*TableStore) GetStreamByARN

func (s *TableStore) GetStreamByARN(arn string) *Stream

GetStreamByARN returns the stream matching the given ARN, or nil.

func (*TableStore) ListTables

func (s *TableStore) ListTables() []string

ListTables returns the names of all tables.

func (*TableStore) PutItem

func (s *TableStore) PutItem(tableName string, item Item, condExpr ...string) *service.AWSError

PutItem adds or replaces an item in the specified table.

func (*TableStore) PutResourcePolicy

func (s *TableStore) PutResourcePolicy(resourceARN, policy string) (*Table, *service.AWSError)

PutResourcePolicy sets a resource-based policy on a table.

func (*TableStore) Query

func (s *TableStore) Query(tableName string, indexName string, keyCondExpr string, filterExpr string, projExpr string, exprNames map[string]string, exprValues map[string]AttributeValue, scanForward *bool, limit int) ([]Item, int, int, *service.AWSError)

Query finds items matching a key condition expression, applies filter and projection.

func (*TableStore) Scan

func (s *TableStore) Scan(tableName string, filterExpr string, projExpr string, exprNames map[string]string, exprValues map[string]AttributeValue, limit int) ([]Item, int, int, *service.AWSError)

Scan iterates all items, applies filter and projection.

func (*TableStore) TableKeySchema

func (s *TableStore) TableKeySchema(name string) (hashKey, rangeKey string, gsis map[string][2]string, ok bool)

TableKeySchema reports a running table's hash key, optional range key ("" when the table has no sort key), and a map of GSI name → [hashKey, rangeKey], for IaC drift detection. ok is false if no table with the given name exists.

func (*TableStore) TransactGetItems

func (s *TableStore) TransactGetItems(items []transactGetItem) ([]transactGetResponse, *service.AWSError)

TransactGetItems retrieves items transactionally.

func (*TableStore) TransactWriteItems

func (s *TableStore) TransactWriteItems(items []transactWriteItem) *service.AWSError

TransactWriteItems executes a transactional write across multiple tables.

func (*TableStore) UpdateItem

func (s *TableStore) UpdateItem(tableName string, key Item, updateExpr string, exprNames map[string]string, exprValues map[string]AttributeValue, returnValues string) (Item, *service.AWSError)

UpdateItem updates an item using an UpdateExpression. Creates the item if it doesn't exist.

func (*TableStore) UpdateTable

func (s *TableStore) UpdateTable(name, billingMode string, pt *ProvisionedThroughput, attrDefs []AttributeDefinition) (*Table, *service.AWSError)

UpdateTable updates mutable table properties: BillingMode, ProvisionedThroughput, AttributeDefinitions (for new GSIs), and GlobalSecondaryIndexUpdates. All fields are optional; only non-zero values are applied. The table stays ACTIVE throughout (no async state transition in CloudMock).

func (*TableStore) UpdateTimeToLive

func (s *TableStore) UpdateTimeToLive(tableName string, spec *TTLSpecification) *service.AWSError

UpdateTimeToLive sets or disables TTL for a table.

Jump to

Keyboard shortcuts

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