No description
- Go 100%
| example/todo-cli | ||
| .gitignore | ||
| client.go | ||
| client_test.go | ||
| errors.go | ||
| go.mod | ||
| LICENSE | ||
| README.md | ||
| types.go | ||
datastore-client-go
Go client for the Datastore service — a DynamoDB-compatible JSON RPC API.
Installation
go get code.m.ac.nz/sophie/datastore-client-go
Usage
import datastore "code.m.ac.nz/sophie/datastore-client-go"
client := datastore.New(datastore.Config{
Endpoint: "http://localhost:8080/",
Namespace: "my_tenant",
JWT: "eyJhbGciOiJSUzI1NiIs...",
})
Configuration
| Field | Description |
|---|---|
Endpoint |
Base URL of the datastore service |
Namespace |
Multi-tenant namespace (default: "default") |
JWT |
Static bearer token for authentication |
JWTProvider |
Function called per-request to obtain a token (takes precedence over JWT) |
HTTPClient |
Custom *http.Client (default: http.DefaultClient) |
API methods
Tables: CreateTable, DescribeTable, ListTables, DeleteTable, UpdateTable
Items: PutItem, GetItem, DeleteItem, UpdateItem
Query: Query, Scan
Batch: BatchWriteItem
Health: Health (no auth required)
Attribute values
datastore.String("hello")
datastore.Number("42")
datastore.Bool(true)
datastore.Null()
datastore.List(datastore.String("a"), datastore.String("b"))
datastore.Map(datastore.AttributeMap{"key": datastore.String("val")})
Error handling
All API errors implement the *DatastoreError base type. Use errors.As to check for specific error types:
out, err := client.GetItem(ctx, datastore.GetItemInput{...})
if err != nil {
var notFound *datastore.ResourceNotFoundError
if errors.As(err, ¬Found) {
// table does not exist
}
return err
}
Error types: ValidationError, ResourceNotFoundError, ResourceInUseError,
ConditionalCheckFailedError, SerializationError, UnauthorizedError,
AccessDeniedError, InternalServerError, MissingActionError, UnknownActionError.
Example
See example/todo-cli/ for a complete CLI app using this client.
# Set up environment
export DATASTORE_URL=http://localhost:8080/
export DATASTORE_JWT=<token>
# Run
go run ./example/todo-cli create-table
go run ./example/todo-cli add "Buy milk"
go run ./example/todo-cli list
go run ./example/todo-cli done <id>
go run ./example/todo-cli delete <id>