dynamo

package
v0.0.0-...-26e3963 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2022 License: Apache-2.0, Apache-2.0 Imports: 12 Imported by: 1

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

View Source
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"
)
View Source
const ErrConditionalCheck = "ERR_CONDITIONAL_CHECK"
View Source
const ErrRequestThrottled = "ERR_REQUEST_THROTTLED"

Variables

View Source
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

func BatchWriteDelete(svc *dynamodb.DynamoDB, t *Table, fc *FailConfig, queries []*Query) error

BatchWriteDelete deletes a list of items from the database.

func CreateItem

func CreateItem(svc *dynamodb.DynamoDB, item interface{}, table *Table) error

CreateItem puts a new item in the table.

func CreateTable

func CreateTable(svc *dynamodb.DynamoDB, table *Table) error

CreateTable creates a new table with the parameters passed to the Table struct. NOTE: CreateTable creates Table in * On-Demand * billing mode.

func DeleteItem

func DeleteItem(svc *dynamodb.DynamoDB, q *Query, t *Table) error

DeleteItem deletes the specified item defined in the Query

func DeleteTable

func DeleteTable(svc *dynamodb.DynamoDB, t *Table) error

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 InitSesh

func InitSesh() *dynamodb.DynamoDB

InitSesh initializes a new session with default config/credentials.

func ListTables

func ListTables(svc *dynamodb.DynamoDB) ([]string, int, error)

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

func UpdateItem(svc *dynamodb.DynamoDB, q *Query, t *Table, expr Expression) error

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) In

func (c *Conditions) In(name string, values ...interface{})

TEST

func (*Conditions) LessThan

func (c *Conditions) LessThan(name string, value interface{})

func (*Conditions) LessThanEqual

func (c *Conditions) LessThanEqual(name string, value interface{})

func (*Conditions) Not

func (c *Conditions) Not(cond Conditions)

Not negates the given Condition.

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

func (d *DbInfo) AddTable(t *Table)

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.

func (*DbInfo) SetSvc

func (d *DbInfo) SetSvc(svc *dynamodb.DynamoDB)

SetSvc sets the Svc 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).

func (*FailConfig) Reset

func (fc *FailConfig) Reset()

Reset resets Attempt and Elapsed fields.

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) Reset

func (q *Query) Reset()

Reset clears all fields.

func (*Query) UpdateCurrent

func (q *Query) UpdateCurrent(fieldName string, value interface{})

UpdateCurrent sets the update fields for the current item.

func (*Query) UpdateNew

func (q *Query) UpdateNew(pv, sv, fieldName string, value interface{})

UpdateNew selects a new item for an update.

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

func CreateNewTableObj(tableName, pKeyName, pType, sKeyName, sType string) *Table

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) Reset

func (u *UpdateExpr) Reset()

Reset clears the Update expression.

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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