Documentation
¶
Index ¶
- Variables
- type DynamoDBClient
- type DynamoTagParser
- type Repository
- func (r *Repository) Create(ctx context.Context, item interface{}) error
- func (r *Repository) Delete(ctx context.Context, id string) error
- func (r *Repository) FindByID(ctx context.Context, id interface{}, out interface{}) error
- func (r *Repository) FindByParameter(ctx context.Context, parameter string, value interface{}, out interface{}) error
- func (r *Repository) GetAll(ctx context.Context, out interface{}) error
- func (r *Repository) Update(ctx context.Context, item interface{}) error
- type TableNamer
Constants ¶
This section is empty.
Variables ¶
var ( ErrDuplicateKey = errors.New("item with this key already exists") ErrNotFound = errors.New("item not found") )
Functions ¶
This section is empty.
Types ¶
type DynamoDBClient ¶
type DynamoDBClient interface {
PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
Query(ctx context.Context, params *dynamodb.QueryInput, optFns ...func(*dynamodb.Options)) (*dynamodb.QueryOutput, error)
Scan(ctx context.Context, params *dynamodb.ScanInput, optFns ...func(*dynamodb.Options)) (*dynamodb.ScanOutput, error)
UpdateItem(ctx context.Context, params *dynamodb.UpdateItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.UpdateItemOutput, error)
DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
}
DynamoDBClient is used by the repository to interface with DynamoDB.
type DynamoTagParser ¶
DynamoTagParser holds parsed values from the `dynamo` struct tag.
func ParseDynamoTag ¶
func ParseDynamoTag(tag string) *DynamoTagParser
ParseDynamoTag parses a struct field tag and returns a parser.
type Repository ¶
type Repository struct {
// contains filtered or unexported fields
}
Repository implements basic CRUD operations using DynamoDB.
func NewRepository ¶
func NewRepository(client DynamoDBClient, defaultTableName string) *Repository
NewRepository returns a new Repository.
func (*Repository) Create ¶
func (r *Repository) Create(ctx context.Context, item interface{}) error
Create stores an item in DynamoDB.
func (*Repository) Delete ¶
func (r *Repository) Delete(ctx context.Context, id string) error
Delete deletes an item from DynamoDB by its primary key id (assumed to be of type string). This method assumes that the primary key attribute in the table is named "id". A conditional expression is used to ensure that the item exists.
func (*Repository) FindByID ¶
func (r *Repository) FindByID(ctx context.Context, id interface{}, out interface{}) error
FindByID retrieves an item by its key.
func (*Repository) FindByParameter ¶
func (r *Repository) FindByParameter(ctx context.Context, parameter string, value interface{}, out interface{}) error
FindByParameter retrieves items by a given parameter value. It uses a Query if an index exists for the parameter and a Scan otherwise.
func (*Repository) GetAll ¶
func (r *Repository) GetAll(ctx context.Context, out interface{}) error
GetAll retrieves all items from a table.
func (*Repository) Update ¶
func (r *Repository) Update(ctx context.Context, item interface{}) error
Update updates an existing item in DynamoDB. It:
- Uses reflection to extract the key field (the one with `key=hash`).
- Builds an UpdateExpression with all other fields tagged with `dynamo`.
- Uses a ConditionExpression to ensure the item exists.
If no updatable field is found or if the key is missing the update will return an error.
type TableNamer ¶
type TableNamer interface {
TableName() string
}
TableNamer should be implemented by items which specify their own table name.