dynamodb

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2020 License: MIT Imports: 4 Imported by: 0

README

DynamoDB

This is a unit-testable version of https://github.com/guregu/dynamo. Basically, this version only wraps all functions to their respective interface.

Example Code

func InitDynamoDB() dynamodb.DB {
	return dynamodb.New(session.New(), aws.NewConfig())
}

func AnyLogic(db dynamodb.DB) (int64, error) {
	return db.Table("user").Get("user_id", "1").Count()
}

Example Unit Test

func TestAnyLogic(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	mockDB := mock.NewMockDB(mockCtrl)
	mockDB.EXPECT().Table("user").Return(
		func() dynamodb.Table {
			mockTable := mock.NewMockTable(mockCtrl)
			mockTable.EXPECT().Get("user_id", "1").Return(
				func() dynamodb.Query {
					mockQuery := mock.NewMockQuery(mockCtrl)
					mockQuery.EXPECT().Count().Return(int64(1), nil)

					return mockQuery
				}(),
			)

			return mockTable
		}(),
	)

	count, err := AnyLogic(mockDB)
	if err != nil || count != 1 {
		t.FailNow()
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Batch

type Batch interface {
	// Get : Creates a new batch get item request with the given keys.
	Get(keys ...dynamo.Keyed) BatchGet
	// Write : Creates a new batch write request, to which puts and deletes can be added.
	Write() BatchWrite
}

Batch : Stores the names of the hash key and range key for creating new batches.

type BatchGet

type BatchGet interface {
	// And : Adds more keys to be gotten.
	And(keys ...dynamo.Keyed) BatchGet
	// Consistent : Set the read consistency to strong or not.
	Consistent(on bool) BatchGet
	// ConsumedCapacity : Measures the throughput capacity consumed by this operation and add it to cc.
	ConsumedCapacity(cc *dynamo.ConsumedCapacity) BatchGet
	// All : Executes this request and unmarshals all results to out, which must be a pointer to a slice.
	All(out interface{}) error
	// AllWithContext : Executes this request and unmarshals all results to out, which must be a pointer to a slice.
	AllWithContext(ctx aws.Context, out interface{}) error
	// Iter : Returns a results iterator for this batch.
	Iter() dynamo.Iter
}

BatchGet : BatchGetItem operation.

type BatchWrite

type BatchWrite interface {
	// Put : Adds put operations for items to this batch.
	Put(items ...interface{}) BatchWrite
	// Delete : Adds delete operations for the given keys to this batch.
	Delete(keys ...dynamo.Keyed) BatchWrite
	// ConsumedCapacity : Measures the throughput capacity consumed by this operation and add it to cc.
	ConsumedCapacity(cc *dynamo.ConsumedCapacity) BatchWrite
	// Run : Executes this batch.
	Run() (wrote int, err error)
	// RunWithContext : Executes this batch.
	RunWithContext(ctx aws.Context) (wrote int, err error)
}

BatchWrite : BatchWriteItem operation.

type ConditionCheck

type ConditionCheck interface {
	// Range : Specifies the name and value of the range key for this item.
	Range(rangeKey string, value interface{}) ConditionCheck
	// If : Specifies a conditional expression for this coniditon check to succeed.
	If(expr string, args ...interface{}) ConditionCheck
	// IfExists : Sets this check to succeed if the item exists.
	IfExists() ConditionCheck
	// IfNotExists : Sets this check to succeed if the item does not exist.
	IfNotExists() ConditionCheck
}

ConditionCheck : Represents a condition for a write transaction to succeed.

type CreateTable

type CreateTable interface {
	// OnDemand : Specifies to create the table with on-demand (pay per request) billing mode.
	OnDemand(enabled bool) CreateTable
	// Provision : Specifies the provisioned read and write capacity for this table.
	Provision(readUnits, writeUnits int64) CreateTable
	// ProvisionIndex : pecifies the provisioned read and write capacity for the given global secondary index.
	ProvisionIndex(index string, readUnits, writeUnits int64) CreateTable
	// Stream : Enables DynamoDB Streams for this table which the specified type of view.
	Stream(view dynamo.StreamView) CreateTable
	// Project : Specifies the projection type for the given table.
	Project(index string, projection dynamo.IndexProjection, includeAttribs ...string) CreateTable
	// Index : Specifies an index to add to this table.
	Index(index dynamo.Index) CreateTable
	// Tag : Specifies a metadata tag for this table. Multiple tags may be specified.
	Tag(key, value string) CreateTable
	// Run : Creates this table or returns and error.
	Run() error
	// RunWithContext : Creates this table or returns and error.
	RunWithContext(ctx aws.Context) error
}

CreateTable : Request to create a new table.

type DB

type DB interface {
	// Client : Returns this DB's internal client used to make API requests.
	Client() dynamodbiface.DynamoDBAPI
	// ListTables : Begins a new request to list all tables.
	ListTables() ListTables
	// Table : Returns a Table handle specified by name.
	Table(name string) Table
	// CreateTable : Begins a new operation to create a table with the given name.
	CreateTable(name string, from interface{}) CreateTable
	// GetTx : Begins a new get transaction.
	GetTx() GetTx
	// WriteTx : Begins a new write transaction.
	WriteTx() WriteTx
}

DB : DynamoDB client.

func New

func New(p client.ConfigProvider, cfgs ...*aws.Config) DB

New : Creates a new client with the given configuration.

type Delete

type Delete interface {
	// Range : Specifies the range key (a.k.a. sort key) to delete.
	Range(name string, value interface{}) Delete
	// If : Specifies a conditional expression for this delete to succeed.
	If(expr string, args ...interface{}) Delete
	// ConsumedCapacity : Measures the throughput capacity consumed by this operation and add it to cc.
	ConsumedCapacity(cc *dynamo.ConsumedCapacity) Delete
	// Run : Executes this delete request.
	Run() error
	// RunWithContext : Executes this delete request.
	RunWithContext(ctx aws.Context) error
	// OldValue : Executes this delete request, unmarshaling the previous value to out.
	OldValue(out interface{}) error
	// OldValueWithContext : Executes this delete request, unmarshaling the previous value to out.
	OldValueWithContext(ctx aws.Context, out interface{}) error
}

Delete : Request to delete an item.

type DeleteTable

type DeleteTable interface {
	// Run : Executes this request and deletes the table.
	Run() error
	// RunWithContext : Executes this request and deletes the table.
	RunWithContext(ctx aws.Context) error
}

DeleteTable : Request to delete a table.

type DescribeTTL

type DescribeTTL interface {
	// Run : Executes this request and returns details about time to live, or an error.
	Run() (dynamo.TTLDescription, error)
	// RunWithContext : Executes this request and returns details about time to live, or an error.
	RunWithContext(ctx aws.Context) (dynamo.TTLDescription, error)
}

DescribeTTL : Request to obtain details about a table's time to live configuration.

type DescribeTable

type DescribeTable interface {
	// Run : Executes this request and describe the table.
	Run() (dynamo.Description, error)
	// RunWithContext : Executes this request and describe the table.
	RunWithContext(ctx aws.Context) (dynamo.Description, error)
}

DescribeTable : Request for information about a table and its indexes.

type GetTx

type GetTx interface {
	// Get : Adds a get request to this transaction.
	Get(q Query) GetTx
	// GetOne : Adds a get request to this transaction, and specifies out to which the results are marshaled.
	GetOne(q Query, out interface{}) GetTx
	// ConsumedCapacity : Measure the throughput capacity consumed by this transaction and add it to cc.
	ConsumedCapacity(cc *dynamo.ConsumedCapacity) GetTx
	// Run : Executes this transaction and unmarshals everything specified by GetOne.
	Run() error
	// RunWithContext : Executes this transaction and unmarshals everything specified by GetOne.
	RunWithContext(ctx aws.Context) error
	// All : Executes this transaction and unmarshals every value to out, which must be a pointer to a slice.
	All(out interface{}) error
	// AllWithContext : Executes this transaction and unmarshals every value to out, which must be a pointer to a slice.
	AllWithContext(ctx aws.Context, out interface{}) error
}

GetTx : Transaction to retrieve items.

type ListTables

type ListTables interface {
	// All : Returns every table or an error.
	All() ([]string, error)
	// AllWithContext : Returns every table or an error.
	AllWithContext(ctx aws.Context) ([]string, error)
	// Iter : Returns an iterator of table names.
	Iter() dynamo.Iter
}

ListTables : Request to list tables.

type Put

type Put interface {
	// If : Specifies a conditional expression for this put to succeed.
	If(expr string, args ...interface{}) Put
	// ConsumedCapacity : Measures the throughput capacity consumed by this operation and add it to cc.
	ConsumedCapacity(cc *dynamo.ConsumedCapacity) Put
	// Run : Executes this put.
	Run() error
	// RunWithContext : Executes this put.
	RunWithContext(ctx aws.Context) error
	// OldValue : Executes this put, unmarshaling the previous value into out.
	OldValue(out interface{}) error
	// OldValueWithContext : Executes this put, unmarshaling the previous value into out.
	OldValueWithContext(ctx aws.Context, out interface{}) error
}

Put : Request to create or replace an item.

type Query

type Query interface {
	// Range : Specifies the range key (a.k.a. sort key) or keys to get.
	Range(name string, op dynamo.Operator, values ...interface{}) Query
	// StartFrom : Makes this query continue from a previous one.
	StartFrom(key dynamo.PagingKey) Query
	// Index : Specifies the name of the index that this query will operate on.
	Index(name string) Query
	// Project : Limits the result attributes to the given paths.
	Project(paths ...string) Query
	// ProjectExpr : Limits the result attributes to the given expression.
	ProjectExpr(expr string, args ...interface{}) Query
	// Filter : Takes an expression that all results will be evaluated against.
	Filter(expr string, args ...interface{}) Query
	// Consistent : Set the read consistency to strong or not.
	Consistent(on bool) Query
	// Limit : Specifies the maximum amount of results to return.
	Limit(limit int64) Query
	// SearchLimit : Specifies the maximum amount of results to examine.
	SearchLimit(limit int64) Query
	// Order : Specifies the desired result order.
	Order(order dynamo.Order) Query
	// ConsumedCapacity : Measures the throughput capacity consumed by this operation and add it to cc.
	ConsumedCapacity(cc *dynamo.ConsumedCapacity) Query
	// One : Executes this query and retrieves a single result.
	One(out interface{}) error
	// OneWithContext : Executes this query and retrieves a single result.
	OneWithContext(ctx aws.Context, out interface{}) error
	// Count : Executes this request, returning the number of results.
	Count() (int64, error)
	// CountWithContext : Executes this request, returning the number of results.
	CountWithContext(ctx aws.Context) (int64, error)
	// All : Executes this request and unmarshals all results to out, which must be a pointer to a slice.
	All(out interface{}) error
	// AllWithContext : Executes this request and unmarshals all results to out, which must be a pointer to a slice.
	AllWithContext(ctx aws.Context, out interface{}) error
	// AllWithLastEvaluatedKey : Executes this request and unmarshals all results to out, which must be a pointer to a slice.
	AllWithLastEvaluatedKey(out interface{}) (dynamo.PagingKey, error)
	// AllWithLastEvaluatedKeyContext : Executes this request and unmarshals all results to out, which must be a pointer to a slice.
	AllWithLastEvaluatedKeyContext(ctx aws.Context, out interface{}) (dynamo.PagingKey, error)
	// Iter : Returns a results iterator for this request.
	Iter() dynamo.PagingIter
}

Query : Request to get one or more items in a table.

type Scan

type Scan interface {
	// StartFrom : Makes this scan continue from a previous one.
	StartFrom(key dynamo.PagingKey) Scan
	// Index : Specifies the name of the index that Scan will operate on.
	Index(name string) Scan
	// Project : Limits the result attributes to the given paths.
	Project(paths ...string) Scan
	// Filter : Takes an expression that all results will be evaluated against.
	Filter(expr string, args ...interface{}) Scan
	// Consistent : Set the read consistency to strong or not.
	Consistent(on bool) Scan
	// Limit : Specifies the maximum amount of results to return.
	Limit(limit int64) Scan
	// SearchLimit : Specifies a maximum amount of results to evaluate.
	SearchLimit(limit int64) Scan
	// ConsumedCapacity : Measure the throughput capacity consumed by this operation and add it to cc.
	ConsumedCapacity(cc *dynamo.ConsumedCapacity) Scan
	// Iter : Returns a results iterator for this request.
	Iter() dynamo.PagingIter
	// All : Executes this request and unmarshals all results to out, which must be a pointer to a slice.
	All(out interface{}) error
	// AllWithContext : Executes this request and unmarshals all results to out, which must be a pointer to a slice.
	AllWithContext(ctx aws.Context, out interface{}) error
	// AllWithLastEvaluatedKey : Executes this request and unmarshals all results to out, which must be a pointer to a slice.
	AllWithLastEvaluatedKey(out interface{}) (dynamo.PagingKey, error)
	// AllWithLastEvaluatedKeyContext : Executes this request and unmarshals all results to out, which must be a pointer to a slice.
	AllWithLastEvaluatedKeyContext(ctx aws.Context, out interface{}) (dynamo.PagingKey, error)
	// Count : Executes this request and returns the number of items matching the scan.
	Count() (int64, error)
	// CountWithContext : Executes this request and returns the number of items matching the scan.
	CountWithContext(ctx aws.Context) (int64, error)
}

Scan : Request to scan all the data in a table.

type Table

type Table interface {
	// Name : Returns this table's name.
	Name() string
	// Batch : Creates a new batch with the given hash key name, and range key name if provided.
	Batch(hashAndRangeKeyName ...string) Batch
	// Check : Creates a new ConditionCheck, which represents a condition for a write transaction to succeed.
	Check(hashKey string, value interface{}) ConditionCheck
	// Delete : Creates a new request to delete an item.
	Delete(name string, value interface{}) Delete
	// Describe : Begins a new request to describe this table.
	Describe() DescribeTable
	// Put : Creates a new request to create or replace an item.
	Put(item interface{}) Put
	// Get : Creates a new request to get an item.
	Get(name string, value interface{}) Query
	// Scan : Creates a new request to scan this table.
	Scan() Scan
	// DeleteTable : Begins a new request to delete this table.
	DeleteTable() DeleteTable
	// UpdateTTL : Begins a new request to enable or disable this table's time to live.
	UpdateTTL(attribute string, enabled bool) UpdateTTL
	// DescribeTTL : Begins a new request to obtain details about this table's time to live configuration.
	DescribeTTL() DescribeTTL
	// Update : Creates a new request to modify an existing item.
	Update(hashKey string, value interface{}) Update
	// UpdateTable : Makes changes to this table's settings.
	UpdateTable() UpdateTable
}

Table : DynamoDB table.

type Update

type Update interface {
	// Range : Specifies the range key (sort key) for the item to update.
	Range(name string, value interface{}) Update
	// Set : Changes path to the given value.
	Set(path string, value interface{}) Update
	// SetNullable : Changes path to the given value, allowing empty and nil values.
	SetNullable(path string, value interface{}) Update
	// SetSet : Changes a set at the given path to the given value.
	SetSet(path string, value interface{}) Update
	// SetIfNotExists : Changes path to the given value, if it does not already exist.
	SetIfNotExists(path string, value interface{}) Update
	// SetExpr : Performs a custom set expression, substituting the args into expr as in filter expressions.
	SetExpr(expr string, args ...interface{}) Update
	// Append : Appends value to the end of the list specified by path.
	Append(path string, value interface{}) Update
	// Prepend : Inserts value to the beginning of the list specified by path.
	Prepend(path string, value interface{}) Update
	// Add : Adds value to path.
	Add(path string, value interface{}) Update
	// AddStringsToSet : Adds the given values to the string set specified by path.
	AddStringsToSet(path string, values ...string) Update
	// AddIntsToSet : Adds the given values to the number set specified by path.
	AddIntsToSet(path string, values ...int) Update
	// AddFloatsToSet : Adds the given values to the number set specified by path.
	AddFloatsToSet(path string, values ...float64) Update
	// DeleteStringsFromSet : Deletes the given values from the string set specified by path.
	DeleteStringsFromSet(path string, values ...string) Update
	// DeleteIntsFromSet : Deletes the given values from the number set specified by path.
	DeleteIntsFromSet(path string, values ...int) Update
	// DeleteFloatsFromSet : Deletes the given values from the number set specified by path.
	DeleteFloatsFromSet(path string, values ...float64) Update
	// Remove : Removes the paths from this item, deleting the specified attributes.
	Remove(paths ...string) Update
	// RemoveExpr : Performs a custom remove expression, substituting the args into expr as in filter expressions.
	RemoveExpr(expr string, args ...interface{}) Update
	// If : Specifies a conditional expression for this update to succeed.
	If(expr string, args ...interface{}) Update
	// ConsumedCapacity : Measure the throughput capacity consumed by this operation and add it to cc.
	ConsumedCapacity(cc *dynamo.ConsumedCapacity) Update
	// Run : Executes this update.
	Run() error
	// RunWithContext : Executes this update.
	RunWithContext(ctx aws.Context) error
	// Value : Executes this update, encoding out with the new value.
	Value(out interface{}) error
	// ValueWithContext : Executes this update, encoding out with the new value.
	ValueWithContext(ctx aws.Context, out interface{}) error
	// OldValue : Executes this update, encoding out with the previous value.
	OldValue(out interface{}) error
	// OldValueWithContext : Executes this update, encoding out with the previous value.
	OldValueWithContext(ctx aws.Context, out interface{}) error
}

Update : Represents changes to an existing item.

type UpdateTTL

type UpdateTTL interface {
	// Run : Executes this request.
	Run() error
	// RunWithContext : Executes this request.
	RunWithContext(ctx aws.Context) error
}

UpdateTTL : Request to enable or disable a table's time to live functionality.

type UpdateTable

type UpdateTable interface {
	// OnDemand : Sets this table to use on-demand (pay per request) billing mode if enabled is true.
	OnDemand(enabled bool) UpdateTable
	// Provision : Sets this table's read and write throughput capacity.
	Provision(read, write int64) UpdateTable
	// ProvisionIndex : Updates a global secondary index's read and write throughput capacity.
	ProvisionIndex(name string, read, write int64) UpdateTable
	// CreateIndex : Adds a new secondary global index.
	CreateIndex(index dynamo.Index) UpdateTable
	// DeleteIndex : Deletes the specified index.
	DeleteIndex(name string) UpdateTable
	// Stream : Enables streaming and sets the stream view type.
	Stream(view dynamo.StreamView) UpdateTable
	// DisableStream : Disables this table's stream.
	DisableStream() UpdateTable
	// Run : Executes this request and describes the table.
	Run() (dynamo.Description, error)
	// RunWithContext : Executes this request and describes the table.
	RunWithContext(ctx aws.Context) (dynamo.Description, error)
}

UpdateTable : Request to change a table's settings.

type WriteTx

type WriteTx interface {
	// Delete : Adds a new delete operation to this transaction.
	Delete(d Delete) WriteTx
	// Put : Adds a put operation to this transaction.
	Put(p Put) WriteTx
	// Update : Adds an update operation to this transaction.
	Update(u Update) WriteTx
	// Check : Adds a conditional check to this transaction.
	Check(check *dynamo.ConditionCheck) WriteTx
	// Idempotent : Marks this transaction as idempotent when enabled is true.
	Idempotent(enabled bool) WriteTx
	// IdempotentWithToken : Marks this transaction as idempotent and explicitly specifies the token value.
	IdempotentWithToken(token string) WriteTx
	// ConsumedCapacity : Measure the throughput capacity consumed by this transaction and add it to cc.
	ConsumedCapacity(cc *dynamo.ConsumedCapacity) WriteTx
	// Run : Executes this transaction.
	Run() error
	// RunWithContext : Executes this transaction.
	RunWithContext(ctx aws.Context) error
}

WriteTx : Transaction to delete, put, update, and check items.

Directories

Path Synopsis
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.

Jump to

Keyboard shortcuts

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