Documentation
¶
Index ¶
- Variables
- func CreateCompositeKey[T interface{}](partitionKey, sortKey interface{}) (key map[string]types.AttributeValue, tableName string, err error)
- func CreateDeleteItem(key interface{}, optFns ...func(*DeleteOptions)) (*dynamodb.DeleteItemInput, error)
- func CreateGetItem(key interface{}, optFns ...func(*GetOptions)) (*dynamodb.GetItemInput, error)
- func CreateKey[T interface{}](partitionKey interface{}) (key map[string]types.AttributeValue, tableName string, err error)
- func CreatePutItem(item interface{}, optFns ...func(*PutOptions)) (*dynamodb.PutItemInput, error)
- func CreateUpdateItem(key interface{}, required func(*UpdateOptions), ...) (*dynamodb.UpdateItemInput, error)
- func Delete(ctx context.Context, key interface{}, optFns ...func(*DeleteOptions)) (*dynamodb.DeleteItemOutput, error)
- func DereferencedType(t reflect.Type) reflect.Type
- func Get(ctx context.Context, key interface{}, out interface{}, ...) (*dynamodb.GetItemOutput, error)
- func Put(ctx context.Context, item interface{}, optFns ...func(opts *PutOptions)) (*dynamodb.PutItemOutput, error)
- func Update(ctx context.Context, key interface{}, required func(*UpdateOptions), ...) (*dynamodb.UpdateItemOutput, error)
- type Attribute
- type BuildOptions
- type Builder
- func (b *Builder) CreateDeleteItem(key interface{}, optFns ...func(*DeleteOptions)) (*dynamodb.DeleteItemInput, error)
- func (b *Builder) CreateGetItem(key interface{}, optFns ...func(*GetOptions)) (*dynamodb.GetItemInput, error)
- func (b *Builder) CreatePutItem(item interface{}, optFns ...func(*PutOptions)) (*dynamodb.PutItemInput, error)
- func (b *Builder) CreateUpdateItem(key interface{}, required func(*UpdateOptions), optFns ...func(*UpdateOptions)) (*dynamodb.UpdateItemInput, error)
- func (b *Builder) ParseFromStruct(in interface{}, optFns ...func(*BuildOptions)) (*Table, error)
- func (b *Builder) ParseFromType(in reflect.Type, optFns ...func(*BuildOptions)) (table *Table, err error)
- type DeleteOptions
- func (o *DeleteOptions) And(right expression.ConditionBuilder, other ...expression.ConditionBuilder) *DeleteOptions
- func (o *DeleteOptions) DisableOptimisticLocking() *DeleteOptions
- func (o *DeleteOptions) Or(right expression.ConditionBuilder, other ...expression.ConditionBuilder) *DeleteOptions
- func (o *DeleteOptions) WithOptions(optFns ...func(*dynamodb.Options)) *DeleteOptions
- func (o *DeleteOptions) WithReturnAllOldValues(out interface{}) *DeleteOptions
- func (o *DeleteOptions) WithReturnAllOldValuesOnConditionCheckFailure(out interface{}) *DeleteOptions
- func (o *DeleteOptions) WithTableName(tableName string) *DeleteOptions
- type GetOptions
- type Manager
- func (m *Manager) Delete(ctx context.Context, key interface{}, optFns ...func(*DeleteOptions)) (*dynamodb.DeleteItemOutput, error)
- func (m *Manager) Get(ctx context.Context, key interface{}, out interface{}, ...) (*dynamodb.GetItemOutput, error)
- func (m *Manager) Put(ctx context.Context, item interface{}, optFns ...func(opts *PutOptions)) (*dynamodb.PutItemOutput, error)
- func (m *Manager) Update(ctx context.Context, key interface{}, required func(*UpdateOptions), ...) (*dynamodb.UpdateItemOutput, error)
- type ManagerAPIClient
- type PutOptions
- func (o *PutOptions) And(right expression.ConditionBuilder, other ...expression.ConditionBuilder) *PutOptions
- func (o *PutOptions) DisableAutoGeneratedTimestamps() *PutOptions
- func (o *PutOptions) DisableOptimisticLocking() *PutOptions
- func (o *PutOptions) Or(right expression.ConditionBuilder, other ...expression.ConditionBuilder) *PutOptions
- func (o *PutOptions) WithOptions(optFns ...func(*dynamodb.Options)) *PutOptions
- func (o *PutOptions) WithReturnAllOldValues(out interface{}) *PutOptions
- func (o *PutOptions) WithReturnAllOldValuesOnConditionCheckFailure(out interface{}) *PutOptions
- func (o *PutOptions) WithTableName(tableName string) *PutOptions
- type Table
- func NewTable(t reflect.Type) (*Table, error)
- func NewTableFromStruct(in interface{}) (*Table, error)
- func NewTableFromStructWithFilter(in interface{}, filter func(*Attribute) (bool, error)) (*Table, error)
- func NewTableWithFilter(in reflect.Type, filter func(*Attribute) (bool, error)) (*Table, error)
- type TypeMismatchError
- type UpdateOptions
- func (o *UpdateOptions) Add(name string, value interface{}) *UpdateOptions
- func (o *UpdateOptions) And(right expression.ConditionBuilder, other ...expression.ConditionBuilder) *UpdateOptions
- func (o *UpdateOptions) Delete(name string, value interface{}) *UpdateOptions
- func (o *UpdateOptions) DisableAutoGeneratedTimestamps() *UpdateOptions
- func (o *UpdateOptions) DisableOptimisticLocking() *UpdateOptions
- func (o *UpdateOptions) Or(right expression.ConditionBuilder, other ...expression.ConditionBuilder) *UpdateOptions
- func (o *UpdateOptions) Remove(name string) *UpdateOptions
- func (o *UpdateOptions) Set(name string, value interface{}) *UpdateOptions
- func (o *UpdateOptions) SetOrRemove(set, remove bool, name string, value interface{}) *UpdateOptions
- func (o *UpdateOptions) SetOrRemoveStringPointer(name string, ptr *string) *UpdateOptions
- func (o *UpdateOptions) WithOptions(optFns ...func(*dynamodb.Options)) *UpdateOptions
- func (o *UpdateOptions) WithReturnValues(returnValue types.ReturnValue, out interface{}) *UpdateOptions
- func (o *UpdateOptions) WithReturnValuesOnConditionCheckFailure(returnValues types.ReturnValuesOnConditionCheckFailure, out interface{}) *UpdateOptions
- func (o *UpdateOptions) WithTableName(tableName string) *UpdateOptions
Constants ¶
This section is empty.
Variables ¶
var DefaultBuilder = &Builder{}
DefaultBuilder is the zero-value Builder instance used by CreateDeleteItem, CreateGetItem, CreatePutItem, and CreateUpdateItem.
var DefaultManager = &Manager{Builder: DefaultBuilder}
DefaultManager is the zero-value Manager that uses DefaultBuilder and is used by Delete, Get, Put, and Update.
Functions ¶
func CreateCompositeKey ¶ added in v0.1.4
func CreateCompositeKey[T interface{}](partitionKey, sortKey interface{}) (key map[string]types.AttributeValue, tableName string, err error)
CreateCompositeKey creates the `map[string]types.AttributeValue` containing both the partition and sort keys for items of type T.
The table name is also returned for convenience. Use this method if you need to extract the key to execute DynamoDB calls that have no out-of-the-box support in this package (e.g. Query, Scan).
func CreateDeleteItem ¶
func CreateDeleteItem(key interface{}, optFns ...func(*DeleteOptions)) (*dynamodb.DeleteItemInput, error)
CreateDeleteItem creates the DeleteItem input parameters for the given key.
The argument key is a struct or pointer to struct containing the key values since DynamoDB DeleteItem only requires key attributes. The argument key should also include the version field that will be used to create the `#version = :version` condition expression if optimistic locking is enabled.
This method is a wrapper around [DefaultFns.CreateDeleteItem].
func CreateGetItem ¶
func CreateGetItem(key interface{}, optFns ...func(*GetOptions)) (*dynamodb.GetItemInput, error)
CreateGetItem creates the GetItem input parameters for the given key.
The argument key is a struct or pointer to struct containing the key values since DynamoDB GetItem only requires key attributes.
This method is a wrapper around [DefaultFns.CreateGetItem].
func CreateKey ¶ added in v0.1.4
func CreateKey[T interface{}](partitionKey interface{}) (key map[string]types.AttributeValue, tableName string, err error)
CreateKey creates the `map[string]types.AttributeValue` containing just the partition key for items of type T.
The table name is also returned for convenience. Use this method if you need to extract the key to execute DynamoDB calls that have no out-of-the-box support in this package (e.g. Query, Scan).
func CreatePutItem ¶
func CreatePutItem(item interface{}, optFns ...func(*PutOptions)) (*dynamodb.PutItemInput, error)
CreatePutItem creates the PutItem input parameters for the given item.
The argument item is a struct or pointer to struct containing all attributes to be written to DynamoDB.
If the item's version has zero value, `attribute_not_exists(#hash_key)` is used as the condition expression to prevent overriding an existing item with the same key. The version attribute in the returned dynamodb.PutItemInput.Item will be incremented by 1.
If the item's version is not zero value, `#version = :version` is used as the condition expression to add optimistic locking. The version attribute in the returned dynamodb.PutItemInput.Item will be incremented by 1.
Any zero-value created or modified timestamps will be set to time.Now.
This method is a wrapper around [DefaultFns.CreatePutItem].
func CreateUpdateItem ¶
func CreateUpdateItem(key interface{}, required func(*UpdateOptions), optFns ...func(opts *UpdateOptions)) (*dynamodb.UpdateItemInput, error)
CreateUpdateItem creates the UpdateItem input parameters for the given key and at least one update expression.
The argument key is a struct or pointer to struct containing the key values since DynamoDB UpdateItem only requires key attributes. The argument key should also include the version field that will be used to create the condition expressions for optimistic locking.
If the key's version has zero value, `attribute_not_exists(#hash_key)` is used as the condition expression to prevent overriding an existing item with the same key. An `ADD #version 1` update expression will be used to update the version.
If the key's version is not zero value, `#version = :version` is used as the condition expression to add optimistic locking. An `ADD #version 1` update expression will be used to update the version.
Modified time will always be set to time.Now.
func Delete ¶
func Delete(ctx context.Context, key interface{}, optFns ...func(*DeleteOptions)) (*dynamodb.DeleteItemOutput, error)
Delete creates and executes a DynamoDB DeleteItem request from the given arguments.
The argument key is a struct or pointer to struct containing the key values since DynamoDB DeleteItem only requires key attributes. The argument key should also include the version attribute that will be used to create the `#version = :version` condition expression if optimistic locking is enabled.
A common use case for Delete is to delete and return old values of an item; pass DeleteOptions.WithReturnAllOldValues to do so. You can also use DeleteOptions.WithReturnAllOldValuesOnConditionCheckFailure if optimistic locking is enabled; they don't have to be pointers to the same struct.
If you need to pass SDK-level options (in addition to options specified by NewManager), use DeleteOptions.WithOptions.
This method is a wrapper around [DefaultManager.Delete].
func DereferencedType ¶ added in v0.2.0
DereferencedType returns the innermost type that is not reflect.Interface or reflect.Ptr.
func Get ¶
func Get(ctx context.Context, key interface{}, out interface{}, optFns ...func(opts *GetOptions)) (*dynamodb.GetItemOutput, error)
Get creates and executes a DynamoDB GetRequest request from the given arguments.
The argument key is a struct or pointer to struct containing the key values since DynamoDB GetItem only requires key attributes.
The argument out is an optional pointer to struct to decode the returned attributes with attributevalue.Decoder.Decode. A common use case for Get is to make a GetItem request and unmarshall the DynamoDB response to a struct; pass a pointer to struct as the out argument to do so.
If you need to pass SDK-level options (in addition to options specified by NewManager), use GetOptions.WithOptions.
This method is a wrapper around [DefaultManager.Get].
func Put ¶
func Put(ctx context.Context, item interface{}, optFns ...func(opts *PutOptions)) (*dynamodb.PutItemOutput, error)
Put creates and executes a DynamoDB PutItem request from the given arguments.
The argument item is a struct or pointer to struct containing all attributes to be written to DynamoDB.
If the item's version has zero value, `attribute_not_exists(#hash_key)` is used as the condition expression to prevent overriding an existing item with the same key. The version attribute in the returned dynamodb.PutItemInput.Item will be incremented by 1.
If the item's version is not zero value, `#version = :version` is used as the condition expression to add optimistic locking. The version attribute in the returned dynamodb.PutItemInput.Item will be incremented by 1.
Any zero-value created or modified timestamps will be set to time.Now.
You can use PutOptions.WithReturnAllOldValues if you wish to return and unmarshal the old values to a struct, or PutOptions.WithReturnAllOldValuesOnConditionCheckFailure in case optimistic locking fails; they don't have to be pointers to the same struct.
If you need to pass SDK-level options (in addition to options specified by NewManager), use PutOptions.WithOptions.
This method is a wrapper around [DefaultManager.Put].
func Update ¶
func Update(ctx context.Context, key interface{}, required func(*UpdateOptions), optFns ...func(*UpdateOptions)) (*dynamodb.UpdateItemOutput, error)
Update creates and executes a DynamoDB UpdateItem request from the given arguments.
The argument key is a struct or pointer to struct containing the key values since DynamoDB UpdateItem only requires key attributes. The argument key should also include the version field that will be used to create the condition expressions for optimistic locking.
If the key's version has zero value, `attribute_not_exists(#hash_key)` is used as the condition expression to prevent overriding an existing item with the same key. An `ADD #version 1` update expression will be used to update the version.
If the key's version is not zero value, `#version = :version` is used as the condition expression to add optimistic locking. An `ADD #version 1` update expression will be used to update the version.
Modified time will always be set to time.Now.
This method is a wrapper around [DefaultManager.Update].
Types ¶
type Attribute ¶ added in v0.2.0
type Attribute struct {
reflect.StructField
// AttributeName is the first tag value in the `dynamodbav` struct tag which is also the name of the attribute
// in DynamoDB.
AttributeName string
// OmitEmpty is true only if the `dynamodbav` struct tag also includes `omitempty`.
OmitEmpty bool
// UnixTime is true only if the `dynamodbav` struct tag also includes `unixtime`.
UnixTime bool
// contains filtered or unexported fields
}
Attribute is a reflect.StructField that is tagged with `dynamodbav` (or whatever TagKey passed to Parse).
func (*Attribute) Encode ¶ added in v0.2.0
func (s *Attribute) Encode(in interface{}) (types.AttributeValue, error)
Encode encodes the given argument "in".
TypeMismatchError is returned if value's type does not match the Attribute's type.
Encode will honour OmitEmpty and UnixTime as much as it can.
func (*Attribute) Get ¶ added in v0.2.0
Get is given a struct v (wrapped as a reflect.Value) that contains the Attribute.StructField in order to return the value (also wrapped as a reflect.Value) of that StructField in the struct v.
Usage example:
type MyStruct struct {
Name string `dynamodbav:"-"`
}
// nameAV is a Attribute modeling the Name field above, this can be used to get and/or set the value of that
// field like this:
v := MyStruct{Name: "John"}
nameAv.Get(reflect.ValueOf(v)).String() // would return "John"
nameAv.Get(reflect.ValueOf(v)).SetString("Jane") // would change v.Name to "Jane"
type BuildOptions ¶
type BuildOptions struct {
// MustHaveVersion, if true, will fail parsing if the struct does not have any field tagged as
// `dynamodbav:",version"`.
MustHaveVersion bool
// MustHaveTimestamps, if true, will fail parsing if the struct does not have any field tagged as
// `dynamodbav:",createdTime" or `dynamodbav:",modifiedTime".
MustHaveTimestamps bool
}
BuildOptions customises how Builder parses struct tags.
type Builder ¶
type Builder struct {
// Encoder is the attributevalue.Encoder to marshal structs into DynamoDB items.
//
// If nil, a default one will be created.
Encoder *attributevalue.Encoder
// Decoder is the attributevalue.Decoder to unmarshal results from DynamoDB.
//
// If nil, a default one will be created.
Decoder *attributevalue.Decoder
// contains filtered or unexported fields
}
Builder parses and creates Table instances which then are used to create DynamoDB request input parameters.
See Table for the `dynamodbav` tags that must be used. Builder is an abstraction on top of Table to create input parameters for DynamoDB DeleteItem (CreateDeleteItem), GetItem (CreateGetItem), PutItem (CreatePutItem), and UpdateItem (CreateUpdateItem).
The zero-value Builder instance is ready for use.
func (*Builder) CreateDeleteItem ¶
func (b *Builder) CreateDeleteItem(key interface{}, optFns ...func(*DeleteOptions)) (*dynamodb.DeleteItemInput, error)
CreateDeleteItem creates the DeleteItem input parameters for the given key.
The argument key is a struct or pointer to struct containing the key values since DynamoDB DeleteItem only requires key attributes. The argument key should also include the version field that will be used to create the `#version = :version` condition expression if optimistic locking is enabled.
func (*Builder) CreateGetItem ¶
func (b *Builder) CreateGetItem(key interface{}, optFns ...func(*GetOptions)) (*dynamodb.GetItemInput, error)
CreateGetItem creates the GetItem input parameters for the given key.
The argument key is a struct or pointer to struct containing the key values since DynamoDB GetItem only requires key attributes.
func (*Builder) CreatePutItem ¶
func (b *Builder) CreatePutItem(item interface{}, optFns ...func(*PutOptions)) (*dynamodb.PutItemInput, error)
CreatePutItem creates the PutItem input parameters for the given item.
The argument item is a struct or pointer to struct containing all attributes to be written to DynamoDB.
If the item's version has zero value, `attribute_not_exists(#hash_key)` is used as the condition expression to prevent overriding an existing item with the same key. The version attribute in the returned dynamodb.PutItemInput.Item will be incremented by 1.
If the item's version is not zero value, `#version = :version` is used as the condition expression to add optimistic locking. The version attribute in the returned dynamodb.PutItemInput.Item will be incremented by 1.
Any zero-value created or modified timestamps will be set to time.Now.
func (*Builder) CreateUpdateItem ¶
func (b *Builder) CreateUpdateItem(key interface{}, required func(*UpdateOptions), optFns ...func(*UpdateOptions)) (*dynamodb.UpdateItemInput, error)
CreateUpdateItem creates the CreateDeleteItem input parameters for the given item.
See package-level CreateUpdateItem for more information.
func (*Builder) ParseFromStruct ¶
func (b *Builder) ParseFromStruct(in interface{}, optFns ...func(*BuildOptions)) (*Table, error)
ParseFromStruct parses and caches the struct tags given by an instance of the struct.
Returns an error if there are validation issues.
func (*Builder) ParseFromType ¶
func (b *Builder) ParseFromType(in reflect.Type, optFns ...func(*BuildOptions)) (table *Table, err error)
ParseFromType parses and caches the struct tags given by its type.
Returns an error if there are validation issues.
type DeleteOptions ¶
type DeleteOptions struct {
// EnableOptimisticLocking is true by default to add optimistic locking.
EnableOptimisticLocking bool
// TableName modifies the [dynamodb.DeleteItemInput.TableName]
TableName *string
// ReturnConsumedCapacity modifies the [dynamodb.DeleteItemInput.ReturnConsumedCapacity]
ReturnConsumedCapacity types.ReturnConsumedCapacity
// ReturnItemCollectionMetrics modifies the [dynamodb.DeleteItemInput.ReturnItemCollectionMetrics]
ReturnItemCollectionMetrics types.ReturnItemCollectionMetrics
// ReturnValues modifies the [dynamodb.DeleteItemInput.ReturnValues]
ReturnValues types.ReturnValue
// ReturnValuesOnConditionCheckFailure modifies the [dynamodb.DeleteItemInput.ReturnValuesOnConditionCheckFailure].
ReturnValuesOnConditionCheckFailure types.ReturnValuesOnConditionCheckFailure
// contains filtered or unexported fields
}
DeleteOptions customises Delete and CreateDeleteItem via chainable methods.
func (*DeleteOptions) And ¶
func (o *DeleteOptions) And(right expression.ConditionBuilder, other ...expression.ConditionBuilder) *DeleteOptions
And adds an expression.And to the condition expression.
func (*DeleteOptions) DisableOptimisticLocking ¶
func (o *DeleteOptions) DisableOptimisticLocking() *DeleteOptions
DisableOptimisticLocking disables optimistic locking logic.
func (*DeleteOptions) Or ¶
func (o *DeleteOptions) Or(right expression.ConditionBuilder, other ...expression.ConditionBuilder) *DeleteOptions
Or adds an expression.Or to the condition expression.
func (*DeleteOptions) WithOptions ¶
func (o *DeleteOptions) WithOptions(optFns ...func(*dynamodb.Options)) *DeleteOptions
WithOptions adds SDK options to the DeleteItem call following the ones provided by NewManager.
func (*DeleteOptions) WithReturnAllOldValues ¶
func (o *DeleteOptions) WithReturnAllOldValues(out interface{}) *DeleteOptions
WithReturnAllOldValues sets the dynamodb.DeleteItemInput.ReturnValues to "ALL_OLD" and instructs Delete to decode the returned attributes to the optional out argument.
If out is given, it must be a struct pointer that can be passed to attributevalue.Decoder.Decode.
func (*DeleteOptions) WithReturnAllOldValuesOnConditionCheckFailure ¶ added in v0.1.2
func (o *DeleteOptions) WithReturnAllOldValuesOnConditionCheckFailure(out interface{}) *DeleteOptions
WithReturnAllOldValuesOnConditionCheckFailure sets the dynamodb.DeleteItemInput.ReturnValuesOnConditionCheckFailure to "ALL_OLD" and instructs Delete to decode the returned attributes to the optional out argument.
If out is given, it must be a struct pointer that can be passed to attributevalue.Decoder.Decode.
func (*DeleteOptions) WithTableName ¶
func (o *DeleteOptions) WithTableName(tableName string) *DeleteOptions
WithTableName overrides [DeleteOptions.TableName].
type GetOptions ¶
type GetOptions struct {
// TableName modifies the [dynamodb.GetItemInput.TableName]
TableName *string
// ConsistentRead modifies the [dynamodb.GetItemInput.ConsistentRead]
ConsistentRead *bool
// ReturnConsumedCapacity modifies the [dynamodb.GetItemInput.ReturnConsumedCapacity]
ReturnConsumedCapacity types.ReturnConsumedCapacity
// contains filtered or unexported fields
}
GetOptions customises Get and CreateGetItem via chainable methods.
func (*GetOptions) WithOptions ¶
func (o *GetOptions) WithOptions(optFns ...func(*dynamodb.Options)) *GetOptions
WithOptions adds SDK options to the GetItem call following the ones provided by NewManager.
func (*GetOptions) WithProjectionExpression ¶
func (o *GetOptions) WithProjectionExpression(name string, names ...string) *GetOptions
WithProjectionExpression replaces the current projection expression with the given names.
func (*GetOptions) WithTableName ¶
func (o *GetOptions) WithTableName(tableName string) *GetOptions
WithTableName overrides [GetOptions.TableName].
type Manager ¶
type Manager struct {
// Client is the DynamoDB client to execute requests.
//
// If not given, the default config (`config.LoadDefaultConfig`) will be used to create the DynamoDB client.
Client ManagerAPIClient
// Builder is the Builder instance to use when building input parameters.
//
// Default to DefaultBuilder.
Builder *Builder
// ClientOptions are passed to each DynamoDB request.
ClientOptions []func(*dynamodb.Options)
// contains filtered or unexported fields
}
Manager provides methods to execute DynamoDB requests.
The zero-value is ready for use.
func NewManager ¶
func NewManager(client ManagerAPIClient, optFns ...func(*Manager)) *Manager
NewManager returns a new Manager using the given client.
func (*Manager) Delete ¶
func (m *Manager) Delete(ctx context.Context, key interface{}, optFns ...func(*DeleteOptions)) (*dynamodb.DeleteItemOutput, error)
Delete creates and executes a DynamoDB DeleteItem request from the given arguments.
The argument key is a struct or pointer to struct containing the key values since DynamoDB DeleteItem only requires key attributes. The argument key should also include the version attribute that will be used to create the `#version = :version` condition expression if optimistic locking is enabled.
A common use case for Delete is to delete and return old values of an item; pass DeleteOptions.WithReturnAllOldValues to do so. You can also use DeleteOptions.WithReturnAllOldValuesOnConditionCheckFailure if optimistic locking is enabled; they don't have to be pointers to the same struct.
If you need to pass SDK-level options (in addition to options specified by NewManager), use DeleteOptions.WithOptions.
If you need to pass SDK-level options (in addition to options specified by NewManager), use DeleteOptions.WithOptions.
func (*Manager) Get ¶
func (m *Manager) Get(ctx context.Context, key interface{}, out interface{}, optFns ...func(opts *GetOptions)) (*dynamodb.GetItemOutput, error)
Get creates and executes a DynamoDB GetRequest request from the given arguments.
The argument key is a struct or pointer to struct containing the key values since DynamoDB GetItem only requires key attributes.
The argument out is an optional pointer to struct to decode the returned attributes with attributevalue.Decoder.Decode. A common use case for Get is to make a GetItem request and unmarshall the DynamoDB response to a struct; pass a pointer to struct as the out argument to do so.
If you need to pass SDK-level options (in addition to options specified by NewManager), use GetOptions.WithOptions.
func (*Manager) Put ¶
func (m *Manager) Put(ctx context.Context, item interface{}, optFns ...func(opts *PutOptions)) (*dynamodb.PutItemOutput, error)
Put creates and executes a DynamoDB PutItem request from the given arguments.
The argument item is a struct or pointer to struct containing all attributes to be written to DynamoDB.
If the item's version has zero value, `attribute_not_exists(#hash_key)` is used as the condition expression to prevent overriding an existing item with the same key. The version attribute in the returned dynamodb.PutItemInput.Item will be incremented by 1.
If the item's version is not zero value, `#version = :version` is used as the condition expression to add optimistic locking. The version attribute in the returned dynamodb.PutItemInput.Item will be incremented by 1.
Any zero-value created or modified timestamps will be set to time.Now.
You can use PutOptions.WithReturnAllOldValues if you wish to return and unmarshal the old values to a struct, or PutOptions.WithReturnAllOldValuesOnConditionCheckFailure in case optimistic locking fails; they don't have to be pointers to the same struct.
If you need to pass SDK-level options (in addition to options specified by NewManager), use PutOptions.WithOptions.
func (*Manager) Update ¶
func (m *Manager) Update(ctx context.Context, key interface{}, required func(*UpdateOptions), optFns ...func(*UpdateOptions)) (*dynamodb.UpdateItemOutput, error)
Update creates and executes a DynamoDB UpdateItem request from the given arguments.
The argument key is a struct or pointer to struct containing the key values since DynamoDB UpdateItem only requires key attributes. The argument key should also include the version field that will be used to create the condition expressions for optimistic locking.
If the key's version has zero value, `attribute_not_exists(#hash_key)` is used as the condition expression to prevent overriding an existing item with the same key. An `ADD #version 1` update expression will be used to update the version.
If the key's version is not zero value, `#version = :version` is used as the condition expression to add optimistic locking. An `ADD #version 1` update expression will be used to update the version.
Modified time will always be set to time.Now.
type ManagerAPIClient ¶
type ManagerAPIClient interface {
DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
UpdateItem(ctx context.Context, params *dynamodb.UpdateItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.UpdateItemOutput, error)
}
ManagerAPIClient abstracts the DynamoDB APIs that are used by Manager.
type PutOptions ¶
type PutOptions struct {
// EnableOptimisticLocking is true by default to add optimistic locking.
EnableOptimisticLocking bool
// EnableAutoGeneratedTimestamps is true by default to add generated timestamp attributes.
EnableAutoGeneratedTimestamps bool
// TableName modifies the [dynamodb.PutItemInput.TableName]
TableName *string
// ReturnConsumedCapacity modifies the [dynamodb.PutItemInput.ReturnConsumedCapacity]
ReturnConsumedCapacity types.ReturnConsumedCapacity
// ReturnItemCollectionMetrics modifies the [dynamodb.PutItemInput.ReturnItemCollectionMetrics]
ReturnItemCollectionMetrics types.ReturnItemCollectionMetrics
// ReturnValues modifies the [dynamodb.PutItemInput.ReturnValues]
ReturnValues types.ReturnValue
// ReturnValuesOnConditionCheckFailure modifies the [dynamodb.PutItemInput.ReturnValuesOnConditionCheckFailure].
ReturnValuesOnConditionCheckFailure types.ReturnValuesOnConditionCheckFailure
// contains filtered or unexported fields
}
PutOptions customises Put and CreatePutItem via chainable methods.
func (*PutOptions) And ¶
func (o *PutOptions) And(right expression.ConditionBuilder, other ...expression.ConditionBuilder) *PutOptions
And adds an expression.And to the condition expression.
func (*PutOptions) DisableAutoGeneratedTimestamps ¶
func (o *PutOptions) DisableAutoGeneratedTimestamps() *PutOptions
DisableAutoGeneratedTimestamps disables auto-generated timestamps logic.
func (*PutOptions) DisableOptimisticLocking ¶
func (o *PutOptions) DisableOptimisticLocking() *PutOptions
DisableOptimisticLocking disables optimistic locking logic.
func (*PutOptions) Or ¶
func (o *PutOptions) Or(right expression.ConditionBuilder, other ...expression.ConditionBuilder) *PutOptions
Or adds an expression.Or to the condition expression.
func (*PutOptions) WithOptions ¶
func (o *PutOptions) WithOptions(optFns ...func(*dynamodb.Options)) *PutOptions
WithOptions adds SDK options to the PutItem call following the ones provided by NewManager.
func (*PutOptions) WithReturnAllOldValues ¶
func (o *PutOptions) WithReturnAllOldValues(out interface{}) *PutOptions
WithReturnAllOldValues sets the dynamodb.PutItemInput.ReturnValues to "ALL_OLD" and instructs Put to decode the returned attributes to the optional out argument.
If out is given, it must be a struct pointer that can be passed to attributevalue.Decoder.Decode.
func (*PutOptions) WithReturnAllOldValuesOnConditionCheckFailure ¶
func (o *PutOptions) WithReturnAllOldValuesOnConditionCheckFailure(out interface{}) *PutOptions
WithReturnAllOldValuesOnConditionCheckFailure sets the dynamodb.PutItemInput.ReturnValuesOnConditionCheckFailure to "ALL_OLD" and instructs Put to decode the returned attributes to the optional out argument.
If out is given, it must be a struct pointer that can be passed to attributevalue.Decoder.Decode.
func (*PutOptions) WithTableName ¶
func (o *PutOptions) WithTableName(tableName string) *PutOptions
WithTableName overrides [PutOptions.TableName].
type Table ¶ added in v0.2.0
type Table struct {
TableName string
HashKey *Attribute
SortKey *Attribute
Version *Attribute
CreatedTime *Attribute
ModifiedTime *Attribute
// contains filtered or unexported fields
}
Table is a collection of Attribute where the hash key must always be present.
`dynamodav` struct tag must be used to model these attributes:
// Hash key is required, sort key is optional. If present, their types must marshal to a valid key type // (S, N, or B). The keys are required to make `attribute_not_exists` work on creating the condition expression // for the PutItem request of an item that shouldn't exist in database. Field string `dynamodbav:"-,hashkey" tableName:"my-table"` Field string `dynamodbav:"-,sortkey"` // Versioned attribute must have `version` show up in its `dynamodbav` tag. It must be a numeric type that // marshals to type N in DynamoDB. Field int64 `dynamodbav:"-,version"` // Timestamp attributes must have `createdTime` and/or `modifiedTime` in its `dynamodbav` tag. It must be a // [time.Time] value. In this example, both attributes marshal to type N in DynamoDB as epoch millisecond. Field time.Time `dynamodbav:"-,createdTime,unixtime"` Field time.Time `dynamodbav:"-,modifiedTime,unixtime"`
Other attributes tagged with `dynamodbav` can also be modeled by Table by passing a filter function to NewTableFromStructWithFilter or NewTableWithFilter. Note that the filter only affects attributes that can be returned with Table.Get. The five attributes described above are always stored by the Table.
func NewTable ¶ added in v0.2.0
NewTable parses the struct tags given by its type.
Returns an error if there are validation issues. By default, all attributes are parsed and stored in the returned Table.
It is recommended to add a unit test and verify your struct is modeled correctly with NewTableFromStruct.
Usage:
func TestMyStruct_IsValid(t *testing.T) {
_, err := NewTable[MyStruct]()
assert.NoError(t, err)
}
func NewTableFromStruct ¶ added in v0.2.0
NewTableFromStruct parses the struct tags given by an instance of the struct.
Returns an error if there are validation issues. By default, all attributes are parsed and stored in the returned Table. To have control and validation over the attributes, use NewTableFromStructWithFilter instead.
It is recommended to add a unit test and verify your struct is modeled correctly with NewTableFromStruct.
Usage:
func TestMyStruct_IsValid(t *testing.T) {
_, err := NewTableFromStruct(MyStruct{})
assert.NoError(t, err)
}
func NewTableFromStructWithFilter ¶ added in v0.2.0
func NewTableFromStructWithFilter(in interface{}, filter func(*Attribute) (bool, error)) (*Table, error)
NewTableFromStructWithFilter is a variant of NewTableFromStruct that allows caller to filter and validate additional fields.
The filter function must return true in order for the field to be modeled in Table. If there is a validation error with the attribute (its type is not expected, etc.), the function may return an error which will stop the parsing process and return the error to caller. Hash and sort keys will always be stored.
func NewTableWithFilter ¶ added in v0.2.0
NewTableWithFilter is a variant of NewTable that allows caller to filter and validate additional fields.
The filter function must return true in order for the field to be modeled in Table. If there is a validation error with the attribute (its type is not expected, etc.), the function may return an error which will stop the parsing process and return the error to caller. Hash and sort keys will always be stored.
type TypeMismatchError ¶ added in v0.2.0
TypeMismatchError is returned by Attribute.Get if the type of the argument "in" does not match the Attribute's type.
func (TypeMismatchError) Error ¶ added in v0.2.0
func (e TypeMismatchError) Error() string
type UpdateOptions ¶
type UpdateOptions struct {
// EnableOptimisticLocking is true by default to add optimistic locking.
EnableOptimisticLocking bool
// EnableAutoGeneratedTimestamps is true by default to add generated timestamp attributes.
EnableAutoGeneratedTimestamps bool
// TableName modifies the [dynamodb.UpdateItemInput.TableName]
TableName *string
// ReturnConsumedCapacity modifies the [dynamodb.UpdateItemInput.ReturnConsumedCapacity]
ReturnConsumedCapacity types.ReturnConsumedCapacity
// ReturnItemCollectionMetrics modifies the [dynamodb.UpdateItemInput.ReturnItemCollectionMetrics]
ReturnItemCollectionMetrics types.ReturnItemCollectionMetrics
// ReturnValues modifies the [dynamodb.UpdateItemInput.ReturnValues]
ReturnValues types.ReturnValue
// ReturnValuesOnConditionCheckFailure modifies the [dynamodb.UpdateItemInput.ReturnValuesOnConditionCheckFailure].
ReturnValuesOnConditionCheckFailure types.ReturnValuesOnConditionCheckFailure
// contains filtered or unexported fields
}
UpdateOptions customises Update and CreateUpdateItem via chainable methods.
func (*UpdateOptions) Add ¶
func (o *UpdateOptions) Add(name string, value interface{}) *UpdateOptions
Add adds an expression.UpdateBuilder.Add expression.
Like all other UpdateOptions methods to modify the update expression, the name and value will be wrapped with an `expression.Name` and `expression.Value`.
func (*UpdateOptions) And ¶
func (o *UpdateOptions) And(right expression.ConditionBuilder, other ...expression.ConditionBuilder) *UpdateOptions
And adds an expression.And to the condition expression.
func (*UpdateOptions) Delete ¶
func (o *UpdateOptions) Delete(name string, value interface{}) *UpdateOptions
Delete adds an expression.UpdateBuilder.Delete expression.
Like all other UpdateOptions methods to modify the update expression, the name and value will be wrapped with an `expression.Name` and `expression.Value`.
func (*UpdateOptions) DisableAutoGeneratedTimestamps ¶
func (o *UpdateOptions) DisableAutoGeneratedTimestamps() *UpdateOptions
DisableAutoGeneratedTimestamps disables auto-generated timestamps logic.
func (*UpdateOptions) DisableOptimisticLocking ¶
func (o *UpdateOptions) DisableOptimisticLocking() *UpdateOptions
DisableOptimisticLocking disables optimistic locking logic.
func (*UpdateOptions) Or ¶
func (o *UpdateOptions) Or(right expression.ConditionBuilder, other ...expression.ConditionBuilder) *UpdateOptions
Or adds an expression.And to the condition expression.
func (*UpdateOptions) Remove ¶
func (o *UpdateOptions) Remove(name string) *UpdateOptions
Remove adds an expression.UpdateBuilder.Set expression.
Like all other UpdateOptions methods to modify the update expression, the name and value will be wrapped with an `expression.Name` and `expression.Value`.
func (*UpdateOptions) Set ¶
func (o *UpdateOptions) Set(name string, value interface{}) *UpdateOptions
Set adds an expression.UpdateBuilder.Set expression.
Like all other UpdateOptions methods to modify the update expression, the name and value will be wrapped with an `expression.Name` and `expression.Value`.
func (*UpdateOptions) SetOrRemove ¶
func (o *UpdateOptions) SetOrRemove(set, remove bool, name string, value interface{}) *UpdateOptions
SetOrRemove adds either Set or Remove action to the update expression.
If set is true, a SET action will be added. If set is false, only when remove is true then a REMOVE action will be added.
| set | remove | action | true | * | SET | false | true | REMOVE | false | false | no-op
This is useful for distinguishing between PUT/POST (remove=true) that replaces attributes with clobbering behaviour vs. PATCH (remove=false) that will only update attributes that are non-nil. An example is given:
func PUT(body Request) {
// because it's a PUT request, if notes is empty, instead of writing empty string to database, we'll remove it.
opts.SetOrRemove(true, true, "notes", body.Notes)
}
func PATCH(body Request) {
// only when notes is non-empty that we'll update it. an empty notes just means caller didn't try to update it.
opts.SetOrRemove(body.Notes != "", false, "notes", body.Notes)
}
func CreateUpdateItem(method string, body Request) {
// an attempt to unify the methods may look like this.
opts.SetOrRemove(expression.UpdateBuilder{}, body.Notes != "", method != "PATCH", "notes", body.Notes)
}
Like all other UpdateOptions methods to modify the update expression, the name and value will be wrapped with an `expression.Name` and `expression.Value`.
func (*UpdateOptions) SetOrRemoveStringPointer ¶
func (o *UpdateOptions) SetOrRemoveStringPointer(name string, ptr *string) *UpdateOptions
SetOrRemoveStringPointer is a specialization of SetOrRemove for string pointer value.
If ptr is a nil pointer, no action is taken. If ptr dereferences to an empty string, a REMOVE action is used. A non-empty string otherwise will result in a SET action.
Like all other UpdateOptions methods, the name will be wrapped with an `expression.Name` and dereferenced value `expression.Value`.
func (*UpdateOptions) WithOptions ¶
func (o *UpdateOptions) WithOptions(optFns ...func(*dynamodb.Options)) *UpdateOptions
WithOptions adds SDK options to the UpdateItem call following the ones provided by NewManager.
func (*UpdateOptions) WithReturnValues ¶
func (o *UpdateOptions) WithReturnValues(returnValue types.ReturnValue, out interface{}) *UpdateOptions
WithReturnValues sets [UpdateOptions.ReturnValues] and instructs Update to decode the returned attributes to the optional out argument.
If out is given, it must be a struct pointer that can be passed to attributevalue.Decoder.Decode.
func (*UpdateOptions) WithReturnValuesOnConditionCheckFailure ¶ added in v0.1.2
func (o *UpdateOptions) WithReturnValuesOnConditionCheckFailure(returnValues types.ReturnValuesOnConditionCheckFailure, out interface{}) *UpdateOptions
WithReturnValuesOnConditionCheckFailure sets [UpdateOptions.ReturnValuesOnConditionCheckFailure] and instructs Update to decode the returned attributes to the optional out argument.
If out is given, it must be a struct pointer that can be passed to attributevalue.Decoder.Decode.
func (*UpdateOptions) WithTableName ¶
func (o *UpdateOptions) WithTableName(tableName string) *UpdateOptions
WithTableName overrides [UpdateOptions.TableName].