Documentation
¶
Overview ¶
Package dynamorm is a lightweight, flexible Object-Relational Mapping (ORM) library for Amazon DynamoDB in Go. It provides a simple, type-safe way to interact with DynamoDB tables using Go structs.
DynamORM offers features like:
- Simple, intuitive API for common DynamoDB operations
- Type-safe mapping between Go structs and DynamoDB items
- Support for primary keys and global secondary indexes
- Composite primary key support and integrity
- Flexible query and filtering capabilities
- Lifecycle hooks for entities
Index ¶
- Constants
- Variables
- func WithBaseEndpoint(endpoint string) func(*dynamodb.Options)
- type Builder
- func (b *Builder) Build() (Expression, error)
- func (b *Builder) WithCondition(condition expression.ConditionBuilder) BuilderInterface
- func (b *Builder) WithFilter(filter expression.ConditionBuilder) BuilderInterface
- func (b *Builder) WithKeyCondition(keyCond expression.KeyConditionBuilder) BuilderInterface
- func (b *Builder) WithProjection(projection expression.ProjectionBuilder) BuilderInterface
- func (b *Builder) WithUpdate(update expression.UpdateBuilder) BuilderInterface
- type BuilderInterface
- type ClientError
- type CreateBuilder
- type Decoder
- type DecoderInterface
- type DynamoDB
- type Encoder
- type EncoderInterface
- type Entity
- type Expression
- type GetOption
- type Option
- type Options
- type Output
- type Query
- func (q *Query) Count() int32
- func (q *Query) Decode(e Entity) error
- func (q *Query) Error() error
- func (q *Query) First(e Entity) error
- func (q *Query) Last(e Entity) error
- func (q *Query) Next() bool
- func (q *Query) NextPage(ctx context.Context) bool
- func (q *Query) Reset()
- func (q *Query) ScannedCount() int32
- type QueryInterface
- type QueryOption
- type RemoveOption
- type ReturnValue
- type SaveOption
- type ScanOption
- type SkCondition
- type Storage
- func (s *Storage) BatchRemove(ctx context.Context, entities ...Entity) error
- func (s *Storage) BatchSave(ctx context.Context, entities ...Entity) error
- func (s *Storage) Get(ctx context.Context, e Entity, opts ...GetOption) error
- func (s *Storage) Query(ctx context.Context, pk string, cond SkCondition, opts ...QueryOption) (QueryInterface, error)
- func (s *Storage) QueryGSI1(ctx context.Context, pk string, cond SkCondition, opts ...QueryOption) (QueryInterface, error)
- func (s *Storage) QueryGSI2(ctx context.Context, pk string, cond SkCondition, opts ...QueryOption) (QueryInterface, error)
- func (s *Storage) Remove(ctx context.Context, e Entity, opts ...RemoveOption) error
- func (s *Storage) Save(ctx context.Context, e Entity, opts ...SaveOption) error
- func (s *Storage) Scan(ctx context.Context, opts ...ScanOption) (QueryInterface, error)
- func (s *Storage) ScanGSI1(ctx context.Context, opts ...ScanOption) (QueryInterface, error)
- func (s *Storage) ScanGSI2(ctx context.Context, opts ...ScanOption) (QueryInterface, error)
- func (s *Storage) Transaction() TransactionInterface
- func (s *Storage) Update(ctx context.Context, e Entity, update expression.UpdateBuilder, ...) error
- type StorageInterface
- type Transaction
- func (tx *Transaction) AddConditionCheck(e Entity, cond expression.ConditionBuilder) error
- func (tx *Transaction) AddRemove(e Entity, opts ...RemoveOption) error
- func (tx *Transaction) AddSave(e Entity, opts ...SaveOption) error
- func (tx *Transaction) AddUpdate(e Entity, update expression.UpdateBuilder, opts ...UpdateOption) error
- func (tx *Transaction) Execute(ctx context.Context) error
- type TransactionInterface
- type UpdateOption
Constants ¶
const ( NONE types.ReturnValue = "NONE" ALL_OLD types.ReturnValue = "ALL_OLD" UPDATED_OLD types.ReturnValue = "UPDATED_OLD" ALL_NEW types.ReturnValue = "ALL_NEW" UPDATED_NEW types.ReturnValue = "UPDATED_NEW" )
Common ReturnValue constants for UpdateItem.
Variables ¶
var ErrBatch = errors.New("failed to process all items in batch")
ErrBatch is returned by batch write operations when DynamoDB responds with unprocessed items that could not be written (affecting BatchSave/BatchRemove).
var ErrClient = errors.New("client error")
ErrClient is used to wrap errors returned by the underlying DynamoDB client.
var ErrEntityBeforeSave = errors.New("failed to execute entity.BeforeSave")
ErrEntityBeforeSave is returned when Entity.BeforeSave returns an error during a save operation (e.g., Storage.Save).
var ErrEntityDecode = errors.New("failed to decode entity")
ErrEntityDecode is returned when the Decoder fails to convert a DynamoDB item into the target entity (e.g., during Storage.Get).
var ErrEntityEncode = errors.New("failed to encode entity")
ErrEntityEncode is returned when the Encoder fails to convert an entity into a DynamoDB attribute map (e.g., during Storage.Save).
var ErrEntityNotFound = errors.New("entity not found")
ErrEntityNotFound is returned by Storage.Get when an entity with the given PK/SK does not exist in DynamoDB.
var ErrEntityPkNotSet = errors.New("entity PK not set")
ErrEntityPkNotSet is returned when an entity's partition key (PK) is empty while an operation requires it (e.g., Storage.Save/Get/Update/Remove).
var ErrEntitySkNotSet = errors.New("entity SK not set")
ErrEntitySkNotSet is returned when an entity's sort key (SK) is empty while an operation requires it (e.g., Storage.Save/Get/Update/Remove).
var ErrIndexOutOfRange = errors.New("index out of range")
ErrIndexOutOfRange is returned by Query.Decode, Query.First, and Query.Last when the requested item is outside the bounds of the current result set, including when there are no items.
Functions ¶
func WithBaseEndpoint ¶
WithBaseEndpoint returns a function that configures the DynamoDB client with a custom base endpoint. This is particularly useful for local test integration, allowing you to point the client to a local DynamoDB instance or alternative endpoint.
Types ¶
type Builder ¶ added in v0.0.5
type Builder struct {
// contains filtered or unexported fields
}
func (*Builder) Build ¶ added in v0.0.5
func (b *Builder) Build() (Expression, error)
func (*Builder) WithCondition ¶ added in v0.0.5
func (b *Builder) WithCondition(condition expression.ConditionBuilder) BuilderInterface
func (*Builder) WithFilter ¶ added in v0.0.5
func (b *Builder) WithFilter(filter expression.ConditionBuilder) BuilderInterface
func (*Builder) WithKeyCondition ¶ added in v0.0.5
func (b *Builder) WithKeyCondition(keyCond expression.KeyConditionBuilder) BuilderInterface
func (*Builder) WithProjection ¶ added in v0.0.5
func (b *Builder) WithProjection(projection expression.ProjectionBuilder) BuilderInterface
func (*Builder) WithUpdate ¶ added in v0.0.5
func (b *Builder) WithUpdate(update expression.UpdateBuilder) BuilderInterface
type BuilderInterface ¶ added in v0.0.5
type BuilderInterface interface {
WithFilter(expression.ConditionBuilder) BuilderInterface
WithProjection(expression.ProjectionBuilder) BuilderInterface
WithUpdate(expression.UpdateBuilder) BuilderInterface
WithKeyCondition(expression.KeyConditionBuilder) BuilderInterface
WithCondition(expression.ConditionBuilder) BuilderInterface
Build() (Expression, error)
}
func NewBuilder ¶ added in v0.0.5
func NewBuilder() BuilderInterface
type ClientError ¶ added in v0.0.5
type ClientError struct {
// contains filtered or unexported fields
}
ClientError represents errors originating from the DynamoDB client. It wraps the original client error (accessible via errors.Unwrap / errors.As).
func NewClientError ¶ added in v0.0.5
func NewClientError(err error) *ClientError
NewClientError wraps an error returned by the underlying DynamoDB client in a ClientError.
func (*ClientError) Error ¶ added in v0.0.5
func (e *ClientError) Error() string
Error returns a human-readable message.
func (*ClientError) Is ¶ added in v0.0.5
func (e *ClientError) Is(target error) bool
Is makes ClientError match ErrClient when used with errors.Is, allowing callers to detect client-originated errors without losing the underlying type.
func (*ClientError) Unwrap ¶ added in v0.0.5
func (e *ClientError) Unwrap() error
Unwrap exposes the wrapped client error so callers can use errors.As oto inspect the underlying AWS error type.
type CreateBuilder ¶ added in v0.0.5
type CreateBuilder func() BuilderInterface
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
func DefaultDecoder ¶
func DefaultDecoder() *Decoder
DefaultDecoder returns a new Decoder with pre-configured options.
func NewDecoder ¶
func NewDecoder(optFns ...func(*attributevalue.DecoderOptions)) *Decoder
NewDecoder creates a new Decoder with the provided decoder options.
type DecoderInterface ¶
type DecoderInterface interface {
// Decode unmarshals DynamoDB attribute values into the provided out parameter.
Decode(map[string]types.AttributeValue, interface{}) error
}
DecoderInterface provides a way to decode DynamoDB attribute values into Go types.
type DynamoDB ¶
type DynamoDB interface {
Query(context.Context, *dynamodb.QueryInput, ...func(*dynamodb.Options)) (*dynamodb.QueryOutput, error)
Scan(context.Context, *dynamodb.ScanInput, ...func(*dynamodb.Options)) (*dynamodb.ScanOutput, error)
GetItem(context.Context, *dynamodb.GetItemInput, ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
PutItem(context.Context, *dynamodb.PutItemInput, ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
UpdateItem(context.Context, *dynamodb.UpdateItemInput, ...func(*dynamodb.Options)) (*dynamodb.UpdateItemOutput, error)
DeleteItem(context.Context, *dynamodb.DeleteItemInput, ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
BatchWriteItem(context.Context, *dynamodb.BatchWriteItemInput, ...func(*dynamodb.Options)) (*dynamodb.BatchWriteItemOutput, error)
TransactWriteItems(context.Context, *dynamodb.TransactWriteItemsInput, ...func(*dynamodb.Options)) (*dynamodb.TransactWriteItemsOutput, error)
CreateTable(context.Context, *dynamodb.CreateTableInput, ...func(*dynamodb.Options)) (*dynamodb.CreateTableOutput, error)
DescribeTable(context.Context, *dynamodb.DescribeTableInput, ...func(*dynamodb.Options)) (*dynamodb.DescribeTableOutput, error)
}
DynamoDB defines the interface for interacting with AWS DynamoDB service. This interface abstracts the AWS SDK's DynamoDB client, allowing for easier testing and flexibility in implementation.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
func DefaultEncoder ¶
func DefaultEncoder() *Encoder
DefaultEncoder returns a new Encoder with pre-configured options.
func NewEncoder ¶
func NewEncoder(optFns ...func(*attributevalue.EncoderOptions)) *Encoder
NewEncoder creates a new Encoder with the provided encoder options.
type EncoderInterface ¶
type EncoderInterface interface {
// Encode marshals a Go value into DynamoDB attribute values.
Encode(interface{}) (map[string]types.AttributeValue, error)
}
EncoderInterface provides a way to encode Go types into DynamoDB attribute values.
type Entity ¶
type Entity interface {
// PkSk returns the partition key and sort key for the entity.
// During a save operation (Save or BatchSave) it is called and saves the values respectively in PK/SK columns.
// During a Get() operation it is called to know how to retrieve the entity.
// Both values must be non-empty for operations to succeed.
PkSk() (string, string)
// GSI1 returns the partition key and sort key for the first Global Secondary Index.
// During a save operation (Save or BatchSave) it is called and saves the values respectively in GSI1PK/GSI1SK columns.
// Return empty strings if not using this GSI.
GSI1() (string, string)
// GSI2 returns the partition key and sort key for the second Global Secondary Index.
// During a save operation (Save or BatchSave) it is called and saves the values respectively in GSI2PK/GSI2SK columns.
// Return empty strings if not using this GSI.
GSI2() (string, string)
// BeforeSave is called before the entity is saved to DynamoDB.
// This can be used to set timestamps, perform validation, etc.
// Return an error to abort the save operation.
BeforeSave() error
}
Entity is the core interface that must be implemented by any struct that will be stored in DynamoDB using this package.
Implementing this interface allows the struct to define how it maps to DynamoDB's key schema and provides lifecycle hooks.
type Expression ¶ added in v0.0.5
type GetOption ¶ added in v0.0.5
type GetOption func(*dynamodb.GetItemInput, BuilderInterface) BuilderInterface
GetOption customizes the DynamoDB GetItemInput request built by Storage.Get.
func GetAttribute ¶ added in v0.0.5
GetAttribute limits the attributes returned by the GetItemInput request using a ProjectionExpression built from the provided attribute names
func GetConsistent ¶ added in v0.0.5
GetConsistent sets the ConsistentRead flag on the GetItemInput request. When true, the read is strongly consistent; when false (default), it is eventually consistent.
type Option ¶
type Option func(*Options)
Option is a function type that modifies Options for use with NewStorage().
func WithBuilder ¶ added in v0.0.5
func WithBuilder(nb CreateBuilder) Option
WithBuilder allows providing a custom builder factory used by Storage to create expression builders. Useful for testing or extending behavior.
func WithDecoder ¶
func WithDecoder(d DecoderInterface) Option
WithDecoder provides your own custom decoder when creating a Storage with NewStorage().
func WithEncoder ¶
func WithEncoder(e EncoderInterface) Option
WithEncoder provides your own custom encoder when creating a Storage with NewStorage().
type Options ¶
type Options struct {
Encoder EncoderInterface
Decoder DecoderInterface
NewBuilder CreateBuilder
}
Options contains configuration options for Storage.
func DefaultOptions ¶
func DefaultOptions() *Options
DefaultOptions creates default options for the storage, providing default encoder and decoder.
type Output ¶ added in v0.0.5
type Output struct {
Count int32
ScannedCount int32
Items []map[string]types.AttributeValue
LastEvaluatedKey map[string]types.AttributeValue
}
Output represents the result of a DynamoDB query or scan operation.
func NewOutputFromQueryOutput ¶ added in v0.0.5
func NewOutputFromQueryOutput(q *dynamodb.QueryOutput) *Output
NewOutputFromQueryOutput converts an AWS SDK DynamoDB QueryOutput to the library's Output type.
func NewOutputFromScanOutput ¶ added in v0.0.5
func NewOutputFromScanOutput(s *dynamodb.ScanOutput) *Output
NewOutputFromScanOutput converts an AWS SDK DynamoDB ScanOutput to the library's Output type.
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query implements the QueryInterface for handling DynamoDB query results. It provides methods for iterating through query results, automatic pagination, and decoding items into Go structs.
func NewQuery ¶
func NewQuery(client DynamoDB, query *dynamodb.QueryInput, scan *dynamodb.ScanInput, output *Output, decoder DecoderInterface) *Query
NewQuery creates a new Query instance from the query input and output.
func (*Query) ScannedCount ¶
type QueryInterface ¶
type QueryInterface interface {
// Count returns the number of items of the current the query result
Count() int32
// ScannedCount returns the number of items evaluated of the current the query result
ScannedCount() int32
// First decodes the first item in the current result set into the provided entity.
// Returns ErrIndexOutOfRange if there are no items.
First(Entity) error
// Last decodes the last item in the current result set into the provided entity.
// Returns ErrIndexOutOfRange if there are no items.
Last(Entity) error
// Next advances the iterator to the next item.
// Returns true if there is a next item available, false otherwise.
Next() bool
// NextPage fetches the next page of results using the LastEvaluatedKey.
// Returns true if there are more results to be fetched, false otherwise.
// Any error encountered can be retrieved via Error().
NextPage(context.Context) bool
// Error returns the last error encountered during pagination.
Error() error
// Reset resets the iterator to the beginning
Reset()
// Decode decodes the current item into the provided interface
Decode(Entity) error
}
QueryInterface provides an interface to handle DynamoDB query results. It supports automatic pagination when iterating through results using Next().
type QueryOption ¶ added in v0.0.5
type QueryOption func(*dynamodb.QueryInput, BuilderInterface) BuilderInterface
QueryOption customizes the DynamoDB QueryInput built by Storage.Query, Storage.QueryGSI1, and Storage.QueryGSI2.
func QueryAttribute ¶ added in v0.0.5
func QueryAttribute(attrs ...string) QueryOption
QueryAttribute limits the attributes returned by the Query operation by applying a projection on the expression builder.
func QueryConsistent ¶ added in v0.0.5
func QueryConsistent(consistent bool) QueryOption
QueryConsistent controls whether the Query operation uses strongly consistent reads. When true, strongly consistent reads are used; when false, eventually consistent reads are used.
func QueryFilter ¶ added in v0.0.5
func QueryFilter(filter expression.ConditionBuilder) QueryOption
QueryFilter applies a filter expression to the Query operation. Use AWS SDK's expression builders to compose complex filters.
func QueryForward ¶ added in v0.0.5
func QueryForward(forward bool) QueryOption
QueryForward controls the sort order of the results for a Query operation. When true, results are returned in ascending order by the sort key; when false, results are returned in descending order.
func QueryLimit ¶ added in v0.0.5
func QueryLimit(count int32) QueryOption
QueryLimit sets the maximum number of items DynamoDB should evaluate per Query request by assigning the Limit field on the QueryInput. This controls page size.
type RemoveOption ¶ added in v0.0.5
type RemoveOption func(*dynamodb.DeleteItemInput, BuilderInterface) BuilderInterface
RemoveOption represents a function that can mutate the DynamoDB DeleteItemInput and/or the expression.Builder used to build the delete statement. It returns the (possibly new) BuilderInterface to allow chaining when the builder is immutable.
func RemoveCondition ¶ added in v0.0.5
func RemoveCondition(condition expression.ConditionBuilder) RemoveOption
RemoveCondition returns a RemoveOption that applies a condition to the removal via the expression builder. This results in a ConditionExpression being added to the DeleteItemInput request when the builder is built.
type ReturnValue ¶ added in v0.0.5
type ReturnValue = types.ReturnValue
ReturnValue is an alias of types.ReturnValue for convenience when specifying the expected return values of an UpdateItem operation.
type SaveOption ¶ added in v0.0.5
type SaveOption func(*dynamodb.PutItemInput, BuilderInterface) BuilderInterface
SaveOption represents a function that can mutate the DynamoDB PutItemInput and/or the expression.Builder used to build the put statement. It returns the (possibly new) BuilderInterface to allow chaining when the builder is immutable.
func SaveCondition ¶ added in v0.0.5
func SaveCondition(condition expression.ConditionBuilder) SaveOption
SaveCondition returns a SaveOption that applies a condition to the save via the expression builder. This results in a ConditionExpression being added to the PutItem request when the builder is built.
type ScanOption ¶ added in v0.0.5
type ScanOption func(*dynamodb.ScanInput, BuilderInterface) BuilderInterface
ScanOption customizes the DynamoDB ScanInput request built by Storage.Scan.
func ScanAttribute ¶ added in v0.0.5
func ScanAttribute(attrs ...string) ScanOption
ScanAttribute limits the attributes returned by the Scan operation.
func ScanFilter ¶ added in v0.0.5
func ScanFilter(filter expression.ConditionBuilder) ScanOption
ScanFilter applies a filter expression to the Scan operation.
func ScanLimit ¶ added in v0.0.5
func ScanLimit(count int32) ScanOption
ScanLimit sets the maximum number of items to evaluate per Scan request by assigning the Limit field on the ScanInput.
type SkCondition ¶ added in v0.0.5
type SkCondition func(key string) expression.KeyConditionBuilder
SkCondition represents a function that, given a sort key name, returns a KeyConditionBuilder to be used in a DynamoDB Query key condition. It is used by Storage.Query, Storage.QueryGSI1, and Storage.QueryGSI2 to build the SK part of the key condition expression.
func SkBeginsWith ¶
func SkBeginsWith(prefix string) SkCondition
SkBeginsWith creates a sort key prefix condition: begins_with(SK, prefix).
func SkBetween ¶ added in v0.0.5
func SkBetween(lower, upper interface{}) SkCondition
SkBetween creates a sort key range condition: SK BETWEEN lower AND upper.
func SkEQ ¶
func SkEQ(v interface{}) SkCondition
SkEQ creates a sort key equality condition: SK = value.
func SkGT ¶
func SkGT(v interface{}) SkCondition
SkGT creates a sort key "greater than" condition: SK > value.
func SkGTE ¶
func SkGTE(v interface{}) SkCondition
SkGTE creates a sort key "greater than or equal" condition: SK >= value.
func SkLT ¶
func SkLT(v interface{}) SkCondition
SkLT creates a sort key "less than" condition: SK < value.
func SkLTE ¶
func SkLTE(v interface{}) SkCondition
SkLTE creates a sort key "less than or equal" condition: SK <= value.
type Storage ¶
type Storage struct {
// contains filtered or unexported fields
}
Storage implements the StorageInterface for DynamoDB operations.
func NewStorage ¶
NewStorage creates a new Storage instance with the specified table name and DynamoDB client. Optional configuration can be provided through Option functions.
Example:
storage := dynamorm.NewStorage("MyTable", dynamoDBClient)
With custom options:
storage := dynamorm.NewStorage("MyTable", dynamoDBClient,
dynamorm.WithEncoder(customEncoder),
dynamorm.WithDecoder(customDecoder),
)
func (*Storage) BatchRemove ¶ added in v0.0.5
func (*Storage) Query ¶
func (s *Storage) Query(ctx context.Context, pk string, cond SkCondition, opts ...QueryOption) (QueryInterface, error)
func (*Storage) QueryGSI1 ¶
func (s *Storage) QueryGSI1(ctx context.Context, pk string, cond SkCondition, opts ...QueryOption) (QueryInterface, error)
func (*Storage) QueryGSI2 ¶
func (s *Storage) QueryGSI2(ctx context.Context, pk string, cond SkCondition, opts ...QueryOption) (QueryInterface, error)
func (*Storage) Scan ¶ added in v0.0.5
func (s *Storage) Scan(ctx context.Context, opts ...ScanOption) (QueryInterface, error)
func (*Storage) ScanGSI1 ¶ added in v0.0.5
func (s *Storage) ScanGSI1(ctx context.Context, opts ...ScanOption) (QueryInterface, error)
func (*Storage) ScanGSI2 ¶ added in v0.0.5
func (s *Storage) ScanGSI2(ctx context.Context, opts ...ScanOption) (QueryInterface, error)
func (*Storage) Transaction ¶ added in v0.0.5
func (s *Storage) Transaction() TransactionInterface
func (*Storage) Update ¶ added in v0.0.5
func (s *Storage) Update(ctx context.Context, e Entity, update expression.UpdateBuilder, opts ...UpdateOption) error
type StorageInterface ¶
type StorageInterface interface {
// Get retrieves an entity from DynamoDB by its partition key (PK) and sort key (SK).
// It calls entity.PkSk() to determine how to query the entity.
// Optional Get options can customize the underlying GetItemInput request.
// Returns ErrEntityNotFound if the item doesn't exist in the table.
Get(context.Context, Entity, ...GetOption) error
// Query performs a query operation on the table using the partition key (PK).
// An optional SK condition can be provided to refine the query, as well as additional filters.
// It returns a QueryInterface for iterating through the results.
Query(context.Context, string, SkCondition, ...QueryOption) (QueryInterface, error)
// QueryGSI1 performs a query operation on the Global Secondary Index 1.
// An optional SK condition can be provided to refine the query, as well as additional filters.
// It returns a QueryInterface for iterating through the results.
QueryGSI1(context.Context, string, SkCondition, ...QueryOption) (QueryInterface, error)
// QueryGSI2 performs a query operation on the Global Secondary Index 2.
// An optional SK condition can be provided to refine the query, as well as additional filters.
// It returns a QueryInterface for iterating through the results.
QueryGSI2(context.Context, string, SkCondition, ...QueryOption) (QueryInterface, error)
// Scan performs a scan operation on the table.
// Optional ScanOption(s) can customize the underlying ScanInput (e.g., limit, filter, projection).
// It returns a QueryInterface for iterating through the results.
Scan(context.Context, ...ScanOption) (QueryInterface, error)
// ScanGSI1 performs a scan operation on the Global Secondary Index 1.
// Optional ScanOption(s) can customize the underlying ScanInput (e.g., limit, filter, projection).
// It returns a QueryInterface for iterating through the results.
ScanGSI1(context.Context, ...ScanOption) (QueryInterface, error)
// ScanGSI2 performs a scan operation on the Global Secondary Index 2.
// Optional ScanOption(s) can customize the underlying ScanInput (e.g., limit, filter, projection).
// It returns a QueryInterface for iterating through the results.
ScanGSI2(context.Context, ...ScanOption) (QueryInterface, error)
// Save persists a single entity to DynamoDB.
// It calls entity.PkSk() to populate PK and SK, and entity.GSI1() and entity.GSI2()
// to populate GSI PK and SK. The BeforeSave() hook is called on the entity before saving.
// Optional SaveOption(s) can be provided, such as SaveCondition to apply a conditional write.
Save(context.Context, Entity, ...SaveOption) error
// BatchSave persists one or more entities to DynamoDB using BatchWriteItem.
// It calls BeforeSave() and uses PkSk(), GSI1(), and GSI2() for each entity.
// Note: DynamoDB limits BatchWriteItem to 25 items per request; larger inputs are chunked.
BatchSave(context.Context, ...Entity) error
// BatchRemove deletes one or more entities from DynamoDB using BatchWriteItem with DeleteRequests.
// It uses PkSk() for each entity to compute the key.
// Note: DynamoDB limits BatchWriteItem to 25 items per request; larger inputs are chunked.
BatchRemove(context.Context, ...Entity) error
// Update applies one or more update operations to an existing item identified by its PK/SK.
// If the operation returns attributes, they are decoded into the provided entity; otherwise, the entity is left unchanged.
Update(context.Context, Entity, expression.UpdateBuilder, ...UpdateOption) error
// Remove deletes an entity from DynamoDB by its PK/SK.
// It calls entity.PkSk() to determine how to delete the entity.
// Returns an error if the operation fails.
Remove(context.Context, Entity, ...RemoveOption) error
// Transaction creates a new Transaction to batch multiple write operations
// (put, update, delete) and execute them atomically.
// The returned transaction uses the same table as the storage instance.
Transaction() TransactionInterface
}
StorageInterface defines the operations for interacting with DynamoDB. It provides methods for retrieving, querying, saving, and removing entities.
type Transaction ¶ added in v0.0.5
type Transaction struct {
// contains filtered or unexported fields
}
func NewTransaction ¶ added in v0.0.5
func NewTransaction(table string, client DynamoDB, encoder EncoderInterface, newBuilder CreateBuilder) *Transaction
NewTransaction creates a new Transaction with the provided DynamoDB client.
func (*Transaction) AddConditionCheck ¶ added in v0.0.5
func (tx *Transaction) AddConditionCheck(e Entity, cond expression.ConditionBuilder) error
func (*Transaction) AddRemove ¶ added in v0.0.5
func (tx *Transaction) AddRemove(e Entity, opts ...RemoveOption) error
func (*Transaction) AddSave ¶ added in v0.0.5
func (tx *Transaction) AddSave(e Entity, opts ...SaveOption) error
func (*Transaction) AddUpdate ¶ added in v0.0.5
func (tx *Transaction) AddUpdate(e Entity, update expression.UpdateBuilder, opts ...UpdateOption) error
type TransactionInterface ¶ added in v0.0.5
type TransactionInterface interface {
// AddSave adds a Put operation for the given entity to the transaction.
AddSave(Entity, ...SaveOption) error
// AddUpdate adds an Update operation for the given entity to the transaction.
AddUpdate(Entity, expression.UpdateBuilder, ...UpdateOption) error
// AddRemove adds a Delete operation for the given entity to the transaction.
AddRemove(Entity, ...RemoveOption) error
// AddConditionCheck adds a ConditionCheck operation for the given entity with the provided condition.
AddConditionCheck(Entity, expression.ConditionBuilder) error
// Execute executes the transaction.
Execute(ctx context.Context) error
}
TransactionInterface describes a lightweight builder for DynamoDB transactional writes. It allows queuing multiple write operations (Put, Update, Delete) against a single table and executing them atomically via TransactWriteItems.
Implementations should accumulate operations until Execute is called. If no operations were added, Execute should be a no-op and return nil.
type UpdateOption ¶ added in v0.0.5
type UpdateOption func(*dynamodb.UpdateItemInput, BuilderInterface) BuilderInterface
UpdateOption represents a function that can mutate the DynamoDB UpdateItemInput and/or the expression.Builder used to build the update statement. It returns the (possibly new) BuilderInterface to allow chaining when the builder is immutable.
func UpdateCondition ¶ added in v0.0.5
func UpdateCondition(condition expression.ConditionBuilder) UpdateOption
UpdateCondition returns an UpdateOption that applies a condition to the update via the expression builder. This results in a ConditionExpression being added to the UpdateItem request when the builder is built.
func UpdateReturnValues ¶ added in v0.0.5
func UpdateReturnValues(v ReturnValue) UpdateOption
UpdateReturnValues returns an UpdateOption that sets the ReturnValues field on the UpdateItemInput.