goddb

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: MIT Imports: 15 Imported by: 0

README

GoDDB

An opinionated package to simplify working with a single table in AWS DynamoDB.

Rules

  • Values associated with items in a table must be structs.
  • Your primary key must be a composite primary key with partition PK and sort SK (both strings in table).
  • Your shared global secondary indexes (those used by multiple Go structs) must be composite with partition suffix PK and sort suffix SK (both strings in table).
  • Your unshared global secondary indexes (those used by only one Go struct) must be simple with partition <StructName>GSI (string in table).
  • You must specify primary key and global secondary indexes on your struct fields with the goddb tag.
  • Struct fields tagged with primary key or global secondary indexes must one of the following types:
    • string
    • int
    • int8
    • uint
    • uint8
    • int16
    • uint16
    • int32
    • uint32
    • int64
    • uint64
    • float32
    • float64
    • time.Time
    • time.Duration
  • The following environment vairables must be set:
    • AWS_REGION
    • GODDB_TABLE_NAME
  • Supported types
    • string => S
    • bool => BOOL
    • int => N
    • int8 => N
    • int16 => N
    • int32 => N
    • int64 => N
    • uint => N
    • uint8 => N
    • uint16 => N
    • uint32 => N
    • uint64 => N
    • float32 => N
    • float64 => N
    • time.Time => S
    • time.Duration => N
    • []string => SS
    • []int => NS
    • []int8 => NS
    • []int16 => NS
    • []int32 => NS
    • []int64 => NS
    • []uint => NS
    • []uint8 => NS
    • []uint16 => NS
    • []uint32 => NS
    • []uint64 => NS
    • []float32 => NS
    • []float64 => NS
    • []time.Time => SS

Examples

// define user
type User struct {
  ID   string `goddb:"PK,SK,UserGSI"`
  Name string
}

// add some users to table
goddb.Put(&User{ID: "bob", Name: "Bob"}).Exec()
goddb.Put(&User{ID: "bill", Name: "Bill"}).Exec()

// query (DynamoDB scan) all users (requires global secondary index UserGSI)
users, _ := goddb.Query(&User{}).Exec()

// update user
goddb.Update(&User{ID: "bob"}).Set(&User{Name: "Robert"}).Exec()

// get user
user, _ := goddb.Get(&User{ID: "bob"}).Exec()

// delete user
goddb.Delete(&User{ID: "bob"}).Exec()

// define post
type Post struct {
  ID     string `goddb:"SK"`
  Author string `goddb:"PK"`
  Body   string
}

// add some posts to table
goddb.Put(&User{ID: "hello", Author: "bill", Body: "Hi!"}).Exec()
goddb.Put(&User{ID: "bye", Name: "bill", Body: "Bye!"}).Exec()

// query all of Bill's posts
posts, _ := goddb.Query(&Post{Author: "bill"}).Exec()

// paginate through Bill's posts
var offset string
posts, _ := goddb.Query(&Post{Author: "bill"}).Page(10, &offset).Exec()
// returns Bill's first 10 posts (sorted by id)

// Note:
// Exec() will mutate offset to indicate last item
// send new offset along with posts to client
// client should send offset back when asking for next page

posts, _ := goddb.Query(&Post{Author: "bill"}).Page(10, &offset).Exec()
// returns Bill's next 10 posts

// Note:
// if query reaches the last of posts, offset will be set back to empty string


// delete all of Bill's posts
goddb.DeleteAll(&Post{Author: "bill"}).Exec()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrConditionFailed = errors.New("condition failed")
View Source
var ErrItemNotFound = errors.New("item not found")
View Source
var TagChar = '#'

Functions

This section is empty.

Types

type Condition

type Condition[T any] struct {
	// contains filtered or unexported fields
}

func And

func And[T any](conditions ...*Condition[T]) *Condition[T]

func AttributeExists

func AttributeExists[T any](selector func(*T) any) *Condition[T]

func AttributeNotExists

func AttributeNotExists[T any](selector func(*T) any) *Condition[T]

func Equal

func Equal[T any](v *T) *Condition[T]

func GreaterThan added in v0.0.4

func GreaterThan[T any](v *T) *Condition[T]

func GreaterThanOrEqual added in v0.0.4

func GreaterThanOrEqual[T any](v *T) *Condition[T]

func LessThan added in v0.0.4

func LessThan[T any](v *T) *Condition[T]

func LessThanOrEqual added in v0.0.4

func LessThanOrEqual[T any](v *T) *Condition[T]

func NotEqual

func NotEqual[T any](v *T) *Condition[T]

func Or

func Or[T any](conditions ...*Condition[T]) *Condition[T]

type DeleteAllRequest

type DeleteAllRequest[T any] struct {
	// contains filtered or unexported fields
}

func DeleteAll

func DeleteAll[T any](v *T) *DeleteAllRequest[T]

func (*DeleteAllRequest[T]) BeginsWith

func (r *DeleteAllRequest[T]) BeginsWith(v *T) *DeleteAllRequest[T]

func (*DeleteAllRequest[T]) Between

func (r *DeleteAllRequest[T]) Between(start *T, end *T) *DeleteAllRequest[T]

func (*DeleteAllRequest[T]) Exec

func (r *DeleteAllRequest[T]) Exec() error

type DeleteRequest

type DeleteRequest[T any] struct {
	// contains filtered or unexported fields
}

func Delete

func Delete[T any](v *T) *DeleteRequest[T]

func (*DeleteRequest[T]) Exec

func (r *DeleteRequest[T]) Exec() error

func (*DeleteRequest[T]) If

func (r *DeleteRequest[T]) If(condition *Condition[T]) *DeleteRequest[T]

type GetRequest

type GetRequest[T any] struct {
	// contains filtered or unexported fields
}

func Get

func Get[T any](v *T) *GetRequest[T]

func (*GetRequest[T]) Consistent

func (r *GetRequest[T]) Consistent() *GetRequest[T]

func (*GetRequest[T]) Exec

func (r *GetRequest[T]) Exec() (*T, error)

type PutRequest

type PutRequest[T any] struct {
	// contains filtered or unexported fields
}

func Put

func Put[T any](item *T) *PutRequest[T]

func (*PutRequest[T]) Exec

func (r *PutRequest[T]) Exec() error

func (*PutRequest[T]) If

func (r *PutRequest[T]) If(condition *Condition[T]) *PutRequest[T]

type QueryRequest

type QueryRequest[T any] struct {
	// contains filtered or unexported fields
}

func Query

func Query[T any](item *T) *QueryRequest[T]

func (*QueryRequest[T]) BeginsWith

func (r *QueryRequest[T]) BeginsWith(v *T) *QueryRequest[T]

func (*QueryRequest[T]) Between

func (r *QueryRequest[T]) Between(start *T, end *T) *QueryRequest[T]

func (*QueryRequest[T]) Exec

func (r *QueryRequest[T]) Exec() ([]*T, error)

func (*QueryRequest[T]) Page

func (r *QueryRequest[T]) Page(maxSize int, offset *string) *QueryRequest[T]

type TransactionWriteRequest added in v0.0.5

type TransactionWriteRequest struct {
	// contains filtered or unexported fields
}

func TransactionWrite added in v0.0.5

func TransactionWrite() *TransactionWriteRequest

func (*TransactionWriteRequest) Delete added in v0.0.5

func (*TransactionWriteRequest) Exec added in v0.0.5

func (t *TransactionWriteRequest) Exec() error

func (*TransactionWriteRequest) Put added in v0.0.5

type UpdateRequest

type UpdateRequest[T any] struct {
	// contains filtered or unexported fields
}

func Update

func Update[T any](item *T) *UpdateRequest[T]

func (*UpdateRequest[T]) Add

func (r *UpdateRequest[T]) Add(t *T) *UpdateRequest[T]

func (*UpdateRequest[T]) Delete

func (r *UpdateRequest[T]) Delete(t *T) *UpdateRequest[T]

func (*UpdateRequest[T]) Exec

func (r *UpdateRequest[T]) Exec() error

func (*UpdateRequest[T]) If

func (r *UpdateRequest[T]) If(condition *Condition[T]) *UpdateRequest[T]

func (*UpdateRequest[T]) Remove

func (r *UpdateRequest[T]) Remove(remove func(t *T) any) *UpdateRequest[T]

func (*UpdateRequest[T]) Set

func (r *UpdateRequest[T]) Set(t *T) *UpdateRequest[T]

Jump to

Keyboard shortcuts

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