在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:guregu/dynamo开源软件地址:https://github.com/guregu/dynamo开源编程语言:Go 100.0%开源软件介绍:dynamo
dynamo is an expressive DynamoDB client for Go, with an easy but powerful API. dynamo integrates with the official AWS SDK. This library is stable and versioned with Go modules. Examplepackage 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"` // Change name in the database
Count int `dynamo:",omitempty"` // Omits if zero value
Children []widget // Lists
Friends []string `dynamo:",set"` // Sets
Set map[string]struct{} `dynamo:",set"` // Map sets, too!
SecretKey string `dynamo:"-"` // Ignored
}
func main() {
sess := session.Must(session.NewSession())
db := dynamo.New(sess, &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).
One(&result)
// get all items
var results []widget
err = table.Scan().All(&results)
// use placeholders in filter expressions (see Expressions section below)
var filtered []widget
err = table.Scan().Filter("'Count' > ?", 10).All(&filtered)
} Expressionsdynamo 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 ( Question marks ( Please see the DynamoDB reference on expressions for more information. The Comparison Operator and Function Reference is also handy. // 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 supportdynamo automatically handles the following interfaces:
This allows you to define custom encodings and provides built-in support for types such as Struct tags and fieldsdynamo handles struct tags similarly to the standard library RenamingBy default, dynamo will use the name of your fields as the name of the DynamoDB attribute it corresponds do. You can specify a different name with the OmissionIf you set a field's name to SetsBy default, slices will be marshaled as DynamoDB lists. To marshal a field to sets instead, use the You can use maps as sets too. The following types are supported:
where Note that the order of objects within a set is undefined. Omitting empty values (omitempty)Using the omitempty option (as in It also supports the type isZeroer interface {
IsZero() bool
} If You can also use the Automatic omissionSome values will be automatically omitted.
To override this behavior, use the To override auto-omit behavior for children of a map, for example Using the NULL typeDynamoDB has a special NULL type to represent null values. In general, this library avoids marshaling things as NULL and prefers to omit those values instead. If you want empty/nil values to marshal to NULL, use the Unix timeBy default, If you want Creating tablesYou can use struct tags to specify hash keys, range keys, and indexes when creating a table. For example: type UserAction struct {
UserID string `dynamo:"ID,hash" index:"Seq-ID-index,range"`
Time time.Time `dynamo:",range"`
Seq int64 `localIndex:"ID-Seq-index,range" index:"Seq-ID-index,hash"`
UUID string `index:"UUID-index,hash"`
} This creates a table with the primary hash key ID and range key Time. It creates two global secondary indices called UUID-index and Seq-ID-index, and a local secondary index called ID-Seq-index. Compatibility with the official AWS librarydynamo 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 In order to use dynamodbattribute's encoding facilities, you must wrap objects passed to dynamo with // 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 testsBy default, tests are run in offline mode. Create a table called Change the table name with the environment variable DYNAMO_TEST_REGION=us-west-2 go test github.com/guregu/dynamo/... -cover If you want to use DynamoDB Local to run local tests, specify DYNAMO_TEST_REGION=us-west-2 DYNAMO_TEST_ENDPOINT=http://localhost:8000 go test github.com/guregu/dynamo/... -cover Example of using aws-cli to create a table for testing. aws dynamodb create-table \
--table-name TestDB \
--attribute-definitions \
AttributeName=UserID,AttributeType=N \
AttributeName=Time,AttributeType=S \
AttributeName=Msg,AttributeType=S \
--key-schema \
AttributeName=UserID,KeyType=HASH \
AttributeName=Time,KeyType=RANGE \
--global-secondary-indexes \
IndexName=Msg-Time-index,KeySchema=[{'AttributeName=Msg,KeyType=HASH'},{'AttributeName=Time,KeyType=RANGE'}],Projection={'ProjectionType=ALL'} \
--billing-mode PAY_PER_REQUEST \
--region us-west-2 \
--endpoint-url http://localhost:8000 # using DynamoDB local LicenseBSD 2-Clause |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论