dynamo

package module
v0.0.0-...-d565e33 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2022 License: MIT Imports: 11 Imported by: 0

README

aws-go-dynamo

A wrapper around the aws-sdk dynamo db sdk

Installation

go get github.com/teleology-io/aws-go-dynamo

Configuration

Not all configuration is required, the following example uses mapstructure to convert dynamo responses to our expected structs. Generally you'll want to create a struct representation of your table complete with json mapping, and then implement some ObjectResolver interface methods.

package main

import (
	"fmt"
	"time"

	"github.com/mitchellh/mapstructure"
	dynamo "github.com/teleology-io/aws-go-dynamo"
)

type Expiration struct {
	ID     string `json:"id" dynamo:"pk"`
	Email  string `json:"email" dynamo:"gsi,EmailGSI"`
	Role   string `json:"role" dynamo:"gsi,RoleGSI"`
	Expiry int64  `json:"expiry"`
}

func main() {
	sess := session.Must(session.NewSession())
	dd := dynamo.New(sess, &aws.Config{
		Region: aws.String("us-east-1"),
	})

	table := dd.Table(dynamo.Config{
		Table: "sample-table",
		Model: Expiration{},
	})

	
	exp, err := table.Get("123")

	updates := Expiration{
		Role: "admin",
	}

	updatedExp, err := table.Update("123", updates)
}

Usage

Create
// Does collision checking
createdItem, err := table.Create("test-id", Expiration{
  ID:     "test-id",
  Email:  "someemail@domain.com",
  Role:   "N/A",
  Expiry: time.Now().Unix() + 300,
})
Update
// Does a get, merges updates and writes
updatedItem, err := table.Update("test-id", Expiration{
  ID:     "test-id",
  Expiry: 0,
})
Put
// Does not collision check and just writes
putItem, err := table.Put(Expiration{
  ID:     "test-id-2",
  Email:  "anotherone@domain.com",
  Role:   "N/A",
  Expiry: time.Now().Unix(),
})
Get
// Uses primary key to get item
gotItem, err := table.Get("test-id-2")
Delete
// Uses primary key to delete item
err := table.Delete("test-id-2")
Query
// Can search across n+1 global secondary indexes - must map to data def
results, err := table.Query([]dynamo.QueryParams{
  {
    Key:   "email",
    Value: "anotherone@domain.com",
  },
  {
    Key:   "role",
    Value: "admin",
  },
})

Changelog

1.0.0

  • Initial Port from @teleology/dynamo

Documentation

Index

Constants

This section is empty.

Variables

View Source
var PUT_RETURN_VALUES = "ALL_OLD"

Functions

This section is empty.

Types

type Config

type Config struct {
	Table string
	Model interface{}
}

type DynamoService

type DynamoService struct{}

func New

func New(s *session.Session, c *aws.Config) *DynamoService

func (*DynamoService) Table

func (d *DynamoService) Table(c Config) *TableDef

type QueryParams

type QueryParams struct {
	Key   string
	Value interface{}
}

type TableDef

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

func (TableDef) Create

func (t TableDef) Create(v interface{}) (interface{}, error)

func (TableDef) Delete

func (t TableDef) Delete(pk string) error

func (TableDef) Get

func (t TableDef) Get(pk string) (interface{}, error)

func (TableDef) Put

func (t TableDef) Put(in interface{}) (interface{}, error)

func (TableDef) Query

func (t TableDef) Query(qps []QueryParams) ([]interface{}, error)

func (TableDef) Update

func (t TableDef) Update(v interface{}) (interface{}, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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