Documentation ¶
Overview ¶
Package dynamo contains controls and objects for DynamoDB CRUD operations. Operations in this package are abstracted from all other application logic and are designed to be used with any DynamoDB table and any object schema. This file contains objects for implementing an exponential backoff algorithm for DynamoDB error handling.
Package dynamo contains controls and objects for DynamoDB CRUD operations. Operations in this package are abstracted from all other application logic and are designed to be used with any DynamoDB table and any object schema. This file contains CRUD operations for working with DynamoDB.
Package dynamo contains controls and objects for DynamoDB CRUD operations. Operations in this package are abstracted from all other application logic and are designed to be used with any DynamoDB table and any object schema. This file defines the Table and Query objects, and functions for creating them. It also defines functions for creating DynamoDB AttributeValue objects and database keys in map format.
Index ¶
- Constants
- Variables
- func BatchGet(svc *dynamodb.DynamoDB, t *Table, fc *FailConfig, queries []*Query, ...) ([]interface{}, error)
- func BatchWriteCreate(svc *dynamodb.DynamoDB, t *Table, fc *FailConfig, items []interface{}) error
- func BatchWriteDelete(svc *dynamodb.DynamoDB, t *Table, fc *FailConfig, queries []*Query) error
- func CreateItem(svc *dynamodb.DynamoDB, item interface{}, table *Table) error
- func CreateTable(svc *dynamodb.DynamoDB, table *Table) error
- func DeleteItem(svc *dynamodb.DynamoDB, q *Query, t *Table) error
- func DeleteTable(svc *dynamodb.DynamoDB, t *Table) error
- func GetItem(svc *dynamodb.DynamoDB, q *Query, t *Table, item interface{}, expr Expression) (interface{}, error)
- func InitSesh() *dynamodb.DynamoDB
- func ListTables(svc *dynamodb.DynamoDB) ([]string, int, error)
- func ScanItems(svc *dynamodb.DynamoDB, t *Table, model interface{}, startKey interface{}, ...) ([]interface{}, error)
- func UpdateItem(svc *dynamodb.DynamoDB, q *Query, t *Table, expr Expression) error
- type Conditions
- func (c *Conditions) And(left, right Conditions, other ...Conditions)
- func (c *Conditions) AttributeExists(name string)
- func (c *Conditions) AttributeNotExists(name string)
- func (c *Conditions) AttributeType()
- func (c *Conditions) BeginsWith(name string, prefix string)
- func (c *Conditions) Between(name string, lower, upper interface{})
- func (c *Conditions) Contains(name string, substr string)
- func (c *Conditions) Equal(name string, value interface{})
- func (c *Conditions) GreaterThan(name string, value interface{})
- func (c *Conditions) GreaterThanEqual(name string, value interface{})
- func (c *Conditions) In(name string, values ...interface{})
- func (c *Conditions) LessThan(name string, value interface{})
- func (c *Conditions) LessThanEqual(name string, value interface{})
- func (c *Conditions) Not(cond Conditions)
- func (c *Conditions) NotEqual(name string, value interface{})
- func (c *Conditions) Or(left, right Conditions, other ...Conditions)
- type DbInfo
- type ExprBuilder
- func (e *ExprBuilder) BuildExpression() (Expression, error)
- func (e *ExprBuilder) Reset()
- func (e *ExprBuilder) SetCondition(cond Conditions)
- func (e *ExprBuilder) SetFilter(name string, value interface{})
- func (e *ExprBuilder) SetKeyCondition(name string, value interface{})
- func (e *ExprBuilder) SetProjection(names []string)
- func (e *ExprBuilder) SetUpdate(update UpdateExpr)
- type Expression
- func (e *Expression) Condition() *string
- func (e *Expression) Filter() *string
- func (e *Expression) KeyCondition() *string
- func (e *Expression) Names() map[string]*string
- func (e *Expression) Projection() *string
- func (e *Expression) Update() *string
- func (e *Expression) Values() map[string]*dynamodb.AttributeValue
- type FailConfig
- type Query
- type Table
- type TransactionItem
- func NewConditionCheckTxItem(name string, t *Table, q *Query, e Expression) TransactionItem
- func NewCreateTxItem(name string, item interface{}, t *Table, q *Query, e Expression) TransactionItem
- func NewDeleteTxItem(name string, t *Table, q *Query, e Expression) TransactionItem
- func NewReadTxItem(name string, t *Table, q *Query, e Expression) TransactionItem
- func NewUpdateTxItem(name string, t *Table, q *Query, e Expression) TransactionItem
- func TxWrite(svc *dynamodb.DynamoDB, items []TransactionItem, requestToken string) ([]TransactionItem, error)
- type UpdateExpr
- func (u *UpdateExpr) Add(name string, value interface{})
- func (u *UpdateExpr) Delete(name string, value interface{})
- func (u *UpdateExpr) Remove(name string)
- func (u *UpdateExpr) Reset()
- func (u *UpdateExpr) Set(name string, value interface{})
- func (u *UpdateExpr) SetIfNotExists(name string, value interface{})
- func (u *UpdateExpr) SetListAppend(name string, list interface{})
- func (u *UpdateExpr) SetMinus(name string, min, sub interface{}, variable bool)
- func (u *UpdateExpr) SetPlus(name string, aug, add interface{}, variable bool)
Constants ¶
const ( // ErrTxConditionCheckFailed is returned when a transaction item fails it's conditional check. // This error cannot be retried. ErrTxConditionCheckFailed = "TX_CONDITION_CHECK_FAILED" // ErrTxConflict is returned when another transaction is in progress for a transaction item. // This error can be retried. ErrTxConflict = "TX_CONFLICT" // ErrTxInProgress is returned when multiple transactions are attempted with the same idempotency key. // This error cannot be retried. ErrTxInProgress = "TX_IN_PROGRESS" // ErrTxThrottled is returned when a transaction item fails due to throttling. // This error can be retried. ErrTxThrottled = "TX_THROTTLED" )
const ErrConditionalCheck = "ERR_CONDITIONAL_CHECK"
const ErrRequestThrottled = "ERR_REQUEST_THROTTLED"
Variables ¶
var DefaultFailConfig = &FailConfig{50, 60000, 0, 0, false}
DefaultFailConfig is the default configuration for the exponential backoff alogrithm with a base wait time of 50 miliseconds, and max wait time of 1 minute (60000 ms).
Functions ¶
func BatchGet ¶
func BatchGet(svc *dynamodb.DynamoDB, t *Table, fc *FailConfig, queries []*Query, refObjs []interface{}, expr Expression) ([]interface{}, error)
BatchGet retrieves a list of items from the database refObjs must be non-nil pointers of the same type, 1 for each query/object returned.
- Returns err if len(queries) != len(refObjs).
func BatchWriteCreate ¶
func BatchWriteCreate(svc *dynamodb.DynamoDB, t *Table, fc *FailConfig, items []interface{}) error
BatchWriteCreate writes a list of items to the database.
func BatchWriteDelete ¶
BatchWriteDelete deletes a list of items from the database.
func CreateItem ¶
CreateItem puts a new item in the table.
func CreateTable ¶
CreateTable creates a new table with the parameters passed to the Table struct. NOTE: CreateTable creates Table in * On-Demand * billing mode.
func DeleteItem ¶
DeleteItem deletes the specified item defined in the Query
func DeleteTable ¶
DeleteTable deletes the selected table.
func GetItem ¶
func GetItem(svc *dynamodb.DynamoDB, q *Query, t *Table, item interface{}, expr Expression) (interface{}, error)
GetItem reads an item from the database. Returns Attribute Value map interface (map[stirng]interface{}) if object found. Returns interface of type item if object not found.
func ListTables ¶
ListTables lists the tables in the database.
func ScanItems ¶
func ScanItems(svc *dynamodb.DynamoDB, t *Table, model interface{}, startKey interface{}, expr Expression) ([]interface{}, error)
ScanItems scans the given Table for items matching the given expression parameters.
func UpdateItem ¶
UpdateItem updates the specified item's attribute defined in the Query object with the UpdateValue defined in the Query.
Types ¶
type Conditions ¶
type Conditions struct {
Condition expression.ConditionBuilder
}
Conditions wraps the expression.ConditionBuilder object.
func NewCondition ¶
func NewCondition() Conditions
func (*Conditions) And ¶
func (c *Conditions) And(left, right Conditions, other ...Conditions)
And creates an AND boolean condition.
func (*Conditions) AttributeExists ¶
func (c *Conditions) AttributeExists(name string)
AttributeExists creates
func (*Conditions) AttributeNotExists ¶
func (c *Conditions) AttributeNotExists(name string)
func (*Conditions) AttributeType ¶
func (c *Conditions) AttributeType()
func (*Conditions) BeginsWith ¶
func (c *Conditions) BeginsWith(name string, prefix string)
func (*Conditions) Between ¶
func (c *Conditions) Between(name string, lower, upper interface{})
func (*Conditions) Contains ¶
func (c *Conditions) Contains(name string, substr string)
func (*Conditions) Equal ¶
func (c *Conditions) Equal(name string, value interface{})
func (*Conditions) GreaterThan ¶
func (c *Conditions) GreaterThan(name string, value interface{})
func (*Conditions) GreaterThanEqual ¶
func (c *Conditions) GreaterThanEqual(name string, value interface{})
func (*Conditions) LessThan ¶
func (c *Conditions) LessThan(name string, value interface{})
func (*Conditions) LessThanEqual ¶
func (c *Conditions) LessThanEqual(name string, value interface{})
func (*Conditions) NotEqual ¶
func (c *Conditions) NotEqual(name string, value interface{})
func (*Conditions) Or ¶
func (c *Conditions) Or(left, right Conditions, other ...Conditions)
type DbInfo ¶
type DbInfo struct { Svc *dynamodb.DynamoDB Tables map[string]*Table FailConfig *FailConfig }
DbInfo holds different variables to be passed to db operation functions Contains the Db Svc, map of tables, and FailConfig.
func InitDbInfo ¶
func InitDbInfo() *DbInfo
InitDbInfo constructs a DbInfo object with default values.
func (*DbInfo) AddTable ¶
AddTable adds a new Table obj to the Tables field of the DbInfo obj. TableName field is used for map key.
func (*DbInfo) SetFailConfig ¶
func (d *DbInfo) SetFailConfig(fc *FailConfig)
SetFailConfig sets the FailConfig field of the DbInfo obj.
type ExprBuilder ¶
type ExprBuilder struct { Condition *expression.ConditionBuilder Filter *expression.ConditionBuilder KeyCondition *expression.KeyConditionBuilder Projection *expression.ProjectionBuilder Update *expression.UpdateBuilder }
ExprBuilder is used to contain Builder types (Condition, Filter, etc...) and build DynamoDB expressions.
func NewExprBuilder ¶
func NewExprBuilder() ExprBuilder
NewExprBuilder constructs a new ExprBuilder object.
func (*ExprBuilder) BuildExpression ¶
func (e *ExprBuilder) BuildExpression() (Expression, error)
BuildExpression builds the expression from the ExprBuilder fields and returns the object.
func (*ExprBuilder) Reset ¶
func (e *ExprBuilder) Reset()
Reset clears all values of the ExprBuilder object.
func (*ExprBuilder) SetCondition ¶
func (e *ExprBuilder) SetCondition(cond Conditions)
SetCondition creates a ConditionBuilder object with the given field name and value.
func (*ExprBuilder) SetFilter ¶
func (e *ExprBuilder) SetFilter(name string, value interface{})
SetFilter creates a ConditionBuilder object as a Filter with the given field name and value.
func (*ExprBuilder) SetKeyCondition ¶
func (e *ExprBuilder) SetKeyCondition(name string, value interface{})
SetKeyCondition creates a KeyConditionBuilder object with the given field name and value.
func (*ExprBuilder) SetProjection ¶
func (e *ExprBuilder) SetProjection(names []string)
SetProjection creates a ProjectionBuilder object from the given list of field names.
func (*ExprBuilder) SetUpdate ¶
func (e *ExprBuilder) SetUpdate(update UpdateExpr)
SetUpdate sets the Update field with a predefined UpdateExpr object.
type Expression ¶
type Expression struct {
Expression expression.Expression `json:"expression"`
}
Expression wraps the AWS expression.Expression object.
func NewExpression ¶
func NewExpression() Expression
Object Constructors
NewExpression constructs a new Expression object.
func (*Expression) Condition ¶
func (e *Expression) Condition() *string
Condition returns the Condition expression.
func (*Expression) Filter ¶
func (e *Expression) Filter() *string
Condition returns the Condition expression.
func (*Expression) KeyCondition ¶
func (e *Expression) KeyCondition() *string
Condition returns the KeyCondition expression.
func (*Expression) Names ¶
func (e *Expression) Names() map[string]*string
Condition returns the expression's Names.
func (*Expression) Projection ¶
func (e *Expression) Projection() *string
Condition returns the Projection expression.
func (*Expression) Update ¶
func (e *Expression) Update() *string
Condition returns the Update expression.
func (*Expression) Values ¶
func (e *Expression) Values() map[string]*dynamodb.AttributeValue
Condition returns the expression's Attribute Values.
type FailConfig ¶
type FailConfig struct { Base float64 Cap float64 Attempt float64 Elapsed float64 MaxRetriesReached bool }
FailConfig stores parameters for the exponential backoff algorithm. Attempt, Elapsed, MaxRetiresReached should always be initialized to 0, 0, false.
func (*FailConfig) ExponentialBackoff ¶
func (fc *FailConfig) ExponentialBackoff()
ExponentialBackoff implements the exponential backoff algorithm for request retries and returns true when the max number of retries has been reached (fc.Elapsed > fc.Cap).
type Query ¶
type Query struct { PrimaryValue interface{} SortValue interface{} UpdateFieldName string UpdateExprKey string UpdateValue interface{} }
Query holds the search values for both the Partition and Sort Keys. Query also holds data for updating a specific item in the UpdateFieldName column.
func CreateNewQueryObj ¶
func CreateNewQueryObj(pval, sval interface{}) *Query
CreateNewQueryObj creates a new Query struct. pval, sval == Primary/Partition key, Sort Key
func (*Query) New ¶
func (q *Query) New(pv, sv interface{})
New creates a new query by setting the Partition Key and Sort Key values.
func (*Query) UpdateCurrent ¶
UpdateCurrent sets the update fields for the current item.
type Table ¶
type Table struct { TableName string PrimaryKeyName string PrimaryKeyType string SortKeyName string SortKeyType string }
Table represents a table and holds basic information about it. This object is used to access the Dynamo Table requested for each CRUD op.
func CreateNewTableObj ¶
CreateNewTableObj creates a new Table struct. The Table's key's Go types must be declared as strings. ex: t := CreateNewTableObj("my_table", "Year", "int", "MovieName", "string")
type TransactionItem ¶
type TransactionItem struct { Name string // arbitrary name to reference transaction item Item interface{} Table *Table Query *Query Expr Expression // contains filtered or unexported fields }
TransactionItem contains an item to create / update in a transaction operation.
func NewConditionCheckTxItem ¶
func NewConditionCheckTxItem(name string, t *Table, q *Query, e Expression) TransactionItem
NewConditionalCheckTxItem initializes a new TransactionItem object for conditional check requests.
func NewCreateTxItem ¶
func NewCreateTxItem(name string, item interface{}, t *Table, q *Query, e Expression) TransactionItem
NewCreateTxItem initializes a new TransactionItem object for create requests.
func NewDeleteTxItem ¶
func NewDeleteTxItem(name string, t *Table, q *Query, e Expression) TransactionItem
NeDeletewTxItem initializes a new TransactionItem object for delete requests.
func NewReadTxItem ¶
func NewReadTxItem(name string, t *Table, q *Query, e Expression) TransactionItem
NewReadTxItem initializes a new TransactionItem object for read requests.
func NewUpdateTxItem ¶
func NewUpdateTxItem(name string, t *Table, q *Query, e Expression) TransactionItem
NewUpdateTxItem initializes a new TransactionItem object for update requests.
func TxWrite ¶
func TxWrite(svc *dynamodb.DynamoDB, items []TransactionItem, requestToken string) ([]TransactionItem, error)
TxConditionCheck checks that each conditional check for a list of transaction items passes. Failed condition checks return an error value, and a list of the TransactionItems that failed their condition checks. Successful condition checks return an empty list of TransactionItems and nil error value.
func (*TransactionItem) GetRequest ¶
func (t *TransactionItem) GetRequest() string
type UpdateExpr ¶
type UpdateExpr struct {
Update expression.UpdateBuilder
}
UpdateExpr is used to construct Update Expressions.
func NewUpdateExpr ¶
func NewUpdateExpr() UpdateExpr
NewUpdateExpr constructs a new UpdateExpr object.
func (*UpdateExpr) Add ¶
func (u *UpdateExpr) Add(name string, value interface{})
Add adds a new field and value to an object.
func (*UpdateExpr) Delete ¶
func (u *UpdateExpr) Delete(name string, value interface{})
Delete deletes the specified value set from the specified field name.
func (*UpdateExpr) Remove ¶
func (u *UpdateExpr) Remove(name string)
Remove removes the specified field name entirely.
func (*UpdateExpr) Set ¶
func (u *UpdateExpr) Set(name string, value interface{})
Set sets the value for the given field name with no conditions.
func (*UpdateExpr) SetIfNotExists ¶
func (u *UpdateExpr) SetIfNotExists(name string, value interface{})
SetIfNotExists sets a new field + value conditionally, if the given field name does not exist.
func (*UpdateExpr) SetListAppend ¶
func (u *UpdateExpr) SetListAppend(name string, list interface{})
SetListAppend creates a new Update expression to append the given list to the current value of the given field name.
func (*UpdateExpr) SetMinus ¶
func (u *UpdateExpr) SetMinus(name string, min, sub interface{}, variable bool)
SetPlus creates a new Set Update expression, where the value is the difference of the 'min' and 'sub' args. SetMinus uses the min value as a string type expression variable when the variable value is set to true.
Ex: 'SET #name = #min - sub
func (*UpdateExpr) SetPlus ¶
func (u *UpdateExpr) SetPlus(name string, aug, add interface{}, variable bool)
SetPlus creates a new Set Update expression, where the value is the sum of the 'aug' and 'add' args. SetPlus uses the aug value as a string type expression variable when the variable value is set to true.
Ex: 'SET #name = #min + sub