storage

package
v0.0.0-...-6a2234f Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package storage provides low-level access to storage

Index

Constants

View Source
const (
	DynamoDBDefaultTableName = "slink"
	DynamoDBDefaultGSI1Name  = "GSI1"
	DynamoDBDefaultRegion    = "us-east-1"
)

Variables

This section is empty.

Functions

func WithDynamoDBClient

func WithDynamoDBClient(client DynamoDBClient) func(*DynamoDBStorage)

func WithDynamoDBConfig

func WithDynamoDBConfig(cfg aws.Config) func(*DynamoDBStorage)

func WithDynamoDBGSI1Name

func WithDynamoDBGSI1Name(gsi1Name string) func(*DynamoDBStorage)

func WithDynamoDBRegion

func WithDynamoDBRegion(region string) func(*DynamoDBStorage)

func WithDynamoDBTableName

func WithDynamoDBTableName(tableName string) func(*DynamoDBStorage)

Types

type DynamoDBClient

type DynamoDBClient interface {
	PutItem(ctx context.Context, params *dynamodb.PutItemInput, options ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
	GetItem(ctx context.Context, params *dynamodb.GetItemInput, options ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
	Query(ctx context.Context, params *dynamodb.QueryInput, optFns ...func(*dynamodb.Options)) (*dynamodb.QueryOutput, error)
	DescribeTable(ctx context.Context, params *dynamodb.DescribeTableInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DescribeTableOutput, error)
	CreateTable(ctx context.Context, params *dynamodb.CreateTableInput, optFns ...func(*dynamodb.Options)) (*dynamodb.CreateTableOutput, error)
}

type DynamoDBStorage

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

DynamoDBStorage implements the Storage interface using Amazon DynamoDB as the backend.

The DynamoDB table can be created elsewhere as long as it has the same structure as used in `CreateTable`. `NewDynamoDBStorage` will create the table automatically if the table doesn’t exist (requires `dynamodb:CreateTable` IAM permission).

The suggested method to supply AWS credentials to the process is by using a dedicated IAM role. DynamoDBStorage will use STS by default when available. You can customise this by using `WithDynamoDBConfig` and supplying your own AWS Config.

The minimum required permissions are: - `dynamodb:PutItem` - `dynamodb:GetItem` - `dynamodb:Query` - `dynamodb:DescribeTable` - `dynamodb:CreateTable` (only if you want the table to be created automatically)

(or check `DynamoDBClient` interface if this list is out of date)

func NewDynamoDBStorage

func NewDynamoDBStorage(ctx context.Context, options ...func(*DynamoDBStorage)) (*DynamoDBStorage, error)

NewDynamoDBStorage returns an initialised `*DynamoDBStorage`.

It checks if the DynamoDB table exists, if not it will create one first. This may fail if the assumed IAM role (or user) doesn't have `dynamodb:CreateTable` permission.

func (*DynamoDBStorage) Create

func (d *DynamoDBStorage) Create(ctx context.Context, shortLink *models.ShortLink) error

func (*DynamoDBStorage) CreateTable

func (s *DynamoDBStorage) CreateTable(ctx context.Context) error

func (*DynamoDBStorage) EnsureTable

func (s *DynamoDBStorage) EnsureTable(ctx context.Context) error

func (*DynamoDBStorage) GetByID

func (d *DynamoDBStorage) GetByID(ctx context.Context, shortLinkID string) (*models.ShortLink, error)

func (*DynamoDBStorage) GetByURL

func (d *DynamoDBStorage) GetByURL(ctx context.Context, linkURL string) ([]*models.ShortLink, error)

type ErrShortLinkAlreadyExists

type ErrShortLinkAlreadyExists struct {
	ShortLinkID string
}

func (*ErrShortLinkAlreadyExists) Error

func (e *ErrShortLinkAlreadyExists) Error() string

type MemoryStorage

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

MemoryStorage implements the Storage interface using `sync.Map`s (in-memory hash maps) as the storage backend.

MemoryStorage is intended to be used only in development or testing, NOT in production.

func NewMemoryStorage

func NewMemoryStorage() *MemoryStorage

func (*MemoryStorage) Create

func (s *MemoryStorage) Create(ctx context.Context, shortLink *models.ShortLink) error

func (*MemoryStorage) GetByID

func (s *MemoryStorage) GetByID(ctx context.Context, shortLinkID string) (*models.ShortLink, error)

func (*MemoryStorage) GetByURL

func (s *MemoryStorage) GetByURL(ctx context.Context, linkURL string) ([]*models.ShortLink, error)

type Storage

type Storage interface {
	Create(ctx context.Context, shortLink *models.ShortLink) error
	GetByID(ctx context.Context, shortLinkID string) (*models.ShortLink, error)
	GetByURL(ctx context.Context, linkURL string) ([]*models.ShortLink, error) // TODO: pagination of output
}

Jump to

Keyboard shortcuts

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