modulepackage
0.2.2
Repository: https://github.com/nguyengg/go-aws-commons.git
Documentation: pkg.go.dev
# README
DynamoDB goodies
This module adds optimistic locking and auto-generated timestamps by modifying the expressions being created as part of a DynamoDB service call.
Usage
Get with:
go get github.com/nguyengg/go-aws-commons/ddb
package main
import (
"context"
"time"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/nguyengg/go-aws-commons/ddb"
)
type Item struct {
Id string `dynamodbav:"id,hashkey" tableName:"my-table"`
Sort string `dynamodbav:"sort,sortkey"`
Version int64 `dynamodbav:"version,version"`
CreatedTime time.Time `dynamodbav:"createdTime,createdTime,unixtime"`
ModifiedTime time.Time `dynamodbav:"modifiedTime,modifiedTime,unixtime"`
}
func main() {
ctx := context.TODO()
// ddbfns.Put and ddbfns.Update is smart enough to add condition expression for me.
item := Item{
Id: "hello",
Sort: "world",
// Since version has zero value, ddbfns.Put and ddbfns.Put will add a condition expression that's basically
// `attribute_not_exists (id)`, and increment the version's value in the request for me.
Version: 0,
// Since these timestamps have zero value, they will be updated to the same time.Now in the request for me.
CreatedTime: time.Time{},
ModifiedTime: time.Time{},
}
// my original item is never mutated by ddbfns.
// Instead, the map[string]AttributeValue sent to DynamoDB is the one that is modified along with its
// the request's ConditionExpression, ExpressionAttributeNames, and ExpressionAttributeValues.
_, _ = ddb.Put(ctx, item)
// If the version is not at zero value, ddbfns.Put and ddbfns.Update knows to add `#version = :old_value` instead.
item = Item{
Id: "hello",
Sort: "world",
// Since version has non-zero value, ddbfns.Put and ddbfns.Put will add `#version = 3` instead, and increment
// the version's value in the request for me.
Version: 3,
// In ddbfns.Update, only ModifiedTime is updated with a `SET #modifiedTime = :now`.
ModifiedTime: time.Time{},
}
// Update requires me to specify at least one update expression. Here's an example of how to return updated values
// as well.
_, _ = ddb.Update(ctx, Item{Id: "hello", Sort: " world", Version: 3}, func(options *ddb.UpdateOptions) {
options.
Set("anotherField", "notes").
// by passing a struct pointer to WithReturnValues, ddbfns will unmarshall the response to
// this struct so that item will have updated attribute values.
WithReturnValues(types.ReturnValueAllNew, &item)
})
// ddbfns.Delete will only use the version attribute, and it does not care if the attribute has zero value or not
// (i.e. you can't attempt to delete an item that doesn't exist).
_, _ = ddb.Delete(ctx, Item{
Id: "hello",
// Even if the version's value was 0, `SET #version = :old_value` is used regardless.
Version: 3,
})
// ddbfns.Get accepts a key which can be struct or struct pointer, and an optional struct pointer argument to
// unmarshal the response of the GetItem request.
item = Item{}
_, _ = ddb.Get(ctx, Item{Id: "hello", Sort: "world"}, &item)
}
# Functions
CreateCompositeKey creates the `map[string]types.AttributeValue` containing both the partition and sort keys for items of type T.
CreateDeleteItem creates the DeleteItem input parameters for the given key.
CreateGetItem creates the GetItem input parameters for the given key.
CreateKey creates the `map[string]types.AttributeValue` containing just the partition key for items of type T.
CreatePutItem creates the PutItem input parameters for the given item.
CreateUpdateItem creates the UpdateItem input parameters for the given key and at least one update expression.
Delete creates and executes a DynamoDB DeleteItem request from the given arguments.
DereferencedType returns the innermost type that is not reflect.Interface or reflect.Ptr.
Get creates and executes a DynamoDB GetRequest request from the given arguments.
NewManager returns a new Manager using the given client.
NewTable parses the struct tags given by its type.
NewTableFromStruct parses the struct tags given by an instance of the struct.
Put creates and executes a DynamoDB PutItem request from the given arguments.
Update creates and executes a DynamoDB UpdateItem request from the given arguments.
# Variables
DefaultBuilder is the zero-value Builder instance used by CreateDeleteItem, CreateGetItem, CreatePutItem, and CreateUpdateItem.
DefaultManager is the zero-value Manager that uses DefaultBuilder and is used by Delete, Get, Put, and Update.
ErrNoAttribute is returned by Table.Get if there is no attribute with the requested name.
# Structs
Attribute is a reflect.StructField that is tagged with `dynamodbav` (or whatever TagKey passed to Parse).
Builder parses and creates Table instances which then are used to create DynamoDB request input parameters.
DeleteOptions customises Delete and CreateDeleteItem via chainable methods.
GetOptions customises Get and CreateGetItem via chainable methods.
InvalidModelErr is returned by NewTable or NewTableFromStruct if the struct type is invalid.
Manager provides methods to execute DynamoDB requests.
PutOptions customises Put and CreatePutItem via chainable methods.
Table is a collection of Attribute where the hash key must always be present.
TableOptions customises the behaviour of the various methods to create parse struct tags for a Table.
TypeMismatchErr is returned by Table.Get if the type of the argument "in" does not match the Table's type.
UpdateOptions customises Update and CreateUpdateItem via chainable methods.
# Interfaces
ManagerAPIClient abstracts the DynamoDB APIs that are used by Manager.