Categorygithub.com/unifinity/dynamo
modulepackage
1.0.4
Repository: https://github.com/unifinity/dynamo.git
Documentation: pkg.go.dev

# README

dynamo GoDoc

import "github.com/guregu/dynamo"

dynamo is an expressive DynamoDB client for Go, with an API heavily inspired by mgo. dynamo integrates with the official AWS SDK.

dynamo is still under development, so the API may change rarely. However, breaking changes will be avoided and the API can be considered relatively stable.

Example

package dynamo

import (
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/guregu/dynamo"
)

// Use struct tags much like the standard JSON library,
// you can embed anonymous structs too!
type widget struct {
	UserID int       // Hash key, a.k.a. partition key
	Time   time.Time // Range key, a.k.a. sort key

	Msg       string              `dynamo:"Message"`
	Count     int                 `dynamo:",omitempty"`
	Friends   []string            `dynamo:",set"` // Sets
	Set       map[string]struct{} `dynamo:",set"` // Map sets, too!
	SecretKey string              `dynamo:"-"`    // Ignored
	Children  []any               // Lists
}


func main() {
	db := dynamo.New(session.New(), &aws.Config{Region: aws.String("us-west-2")})
	table := db.Table("Widgets")
	
	// put item
	w := widget{UserID: 613, Time: time.Now(), Msg: "hello"}
	err := table.Put(w).Run() 
	
	// get the same item 
	var result widget
	err = table.Get("UserID", w.UserID).
		Range("Time", dynamo.Equal, w.Time).
		Filter("'Count' = ? AND $ = ?", w.Count, "Message", w.Msg). // placeholders in expressions
		One(&result)
	
	// get all items
	var results []widget
	err = table.Scan().All(&results)
}

Expressions

dynamo will help you write expressions used to filter results in queries and scans, and add conditions to puts and deletes.

Attribute names may be written as is if it is not a reserved word, or be escaped with single quotes (''). You may also use dollar signs ($) as placeholders for attribute names. DynamoDB has very large amount of reserved words so it may be a good idea to just escape everything.

Question marks (?) are used as placeholders for attribute values. DynamoDB doesn't have value literals, so you need to substitute everything.

Please see the DynamoDB reference on expressions for more information.

// Using single quotes to escape a reserved word, and a question mark as a value placeholder.
// Finds all items whose date is greater than or equal to lastUpdate.
table.Scan().Filter("'Date' >= ?", lastUpdate).All(&results)

// Using dollar signs as a placeholder for attribute names. 
// Deletes the item with an ID of 42 if its score is at or below the cutoff, and its name starts with G.
table.Delete("ID", 42).If("Score <= ? AND begins_with($, ?)", cutoff, "Name", "G").Run()

// Put a new item, only if it doesn't already exist.
table.Put(item{ID: 42}).If("attribute_not_exists(ID)").Run()

Encoding support

dynamo automatically handles the following interfaces:

This allows you to define custom encodings and provides built-in support for types such as time.Time.

Compatibility with the official AWS library

dynamo has been in development before the official AWS libraries were stable. We use a different encoder and decoder than the dynamodbattribute package. dynamo uses the dynamo struct tag instead of the dynamodbav struct tag, and we also prefer to automatically omit invalid values such as empty strings, whereas the dynamodbattribute package substitutes null values for them. Items that satisfy the dynamodbattribute.(Un)marshaler interfaces are compatibile with both libraries.

In order to use dynamodbattribute's encoding facilities, you must wrap objects passed to dynamo with dynamo.AWSEncoding. Here is a quick example:

// Notice the use of the dynamodbav struct tag
type book struct {
	ID    int    `dynamodbav:"id"`
	Title string `dynamodbav:"title"`
}
// Putting an item
err := db.Table("Books").Put(dynamo.AWSEncoding(book{
	ID:    42,
	Title: "Principia Discordia",
})).Run()
// When getting an item you MUST pass a pointer to AWSEncoding!
var someBook book
err := db.Table("Books").Get("ID", 555).One(dynamo.AWSEncoding(&someBook))

Integration tests

By default, tests are run in offline mode. Create a table called TestDB, with a Number Parition Key called UserID and a String Sort Key called Time. Change the table name with the environment variable DYNAMO_TEST_TABLE. You must specify DYNAMO_TEST_REGION, setting it to the AWS region where your test table is.

DYNAMO_TEST_REGION=us-west-2 go test github.com/guregu/dynamo/... -cover

License

BSD

# Functions

AWSEncoding wraps an object, forcing it to use AWS's official dynamodbattribute package for encoding and decoding.
Marshal converts the given value into a DynamoDB attribute value.
MarshalItem converts the given struct into a DynamoDB item.
New creates a new client with the given configuration.
NewFromIface creates a new client with the given interface.
Unmarshal decodes a DynamoDB value into out, which must be a pointer.
Unmarshal decodes a DynamoDB item into out, which must be a pointer.

# Constants

The table or index is ready for use.
ScanIndexForward = true.
Operators used for comparing against the range key in queries.
Operators used for comparing against the range key in queries.
Key types for table and index hash/range keys.
The table or index is being created.
The table or index is being deleted.
ScanIndexForward = false.
Operators used for comparing against the range key in queries.
Operators used for comparing against the range key in queries.
Operators used for comparing against the range key in queries.
Operators used for comparing against the range key in queries.
Operators used for comparing against the range key in queries.
Key types for table and index hash/range keys.
Operators used for comparing against the range key in queries.
Key types for table and index hash/range keys.
Key types for table and index hash/range keys.
The table or index is being updated.

# Variables

All of the table attributes are projected into the index.
ErrNotFound is returned when no items could be found in Get or OldValue a and similar operations.
ErrTooMany is returned when one item was requested, but the query returned multiple items.
Only the specified table attributes are projected into the index.
Only the key attributes of the modified item are written to the stream.
Only the key attributes of the modified item are written to the stream.
Both the new and the old item images of the item are written to the stream.
The entire item, as it appears after it was modified, is written to the stream.
The entire item, as it appeared before it was modified, is written to the stream.
RetryTimeout defines the maximum amount of time that requests will attempt to automatically retry for.

# Structs

Batch stores the names of the hash key and range key for creating new batches.
BatchGet is a BatchGetItem operation.
BatchWrite is a BatchWriteItem operation.
ConsumedCapacity represents the amount of throughput capacity consumed during an operation.
CreateTable is a request to create a new table.
DB is a DynamoDB client.
Delete is a request to delete an item.
DeleteTable is a request to delete a table.
DescribeTable is a request for information about a table and its indexes.
Description contains information about a table.
No description provided by the author
ListTables is a request to list tables.
Put is a request to create or replace an item.
Query is a request to get one or more items in a table.
Scan is a request to scan all the data in a table.
Table is a DynamoDB table.
No description provided by the author
Update represents changes to an existing item.
UpdateTable is a request to change a table's settings.

# Interfaces

No description provided by the author
Iter is an iterator for request results.
Keyed provides hash key and range key values.
Marshaler is the interface implemented by objects that can marshal themselves into an AttributeValue.
PagingIter is an iterator of request results that can also return a key used for splitting results.
Unmarshaler is the interface implemented by objects that can unmarshal an AttributeValue into themselves.

# Type aliases

IndexProjection determines which attributes are mirrored into indices.
Keys provides an easy way to specify the hash and range keys.
KeyType is used to specify the type of hash and range keys for tables and indexes.
Operator is an operation to apply in key comparisons.
Order is used for specifying the order of results.
PagingKey is a key used for splitting up partial results.
Status is an enumeration of table and index statuses.
StreamView determines what information is written to a table's stream.