db

package
v0.0.0-...-25a6cea Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: MPL-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const DynamoDbPendingIndexName = string(SubscriberPending)

Sparse Global Secondary Index for records containing a "pending" attribute.

View Source
const DynamoDbPendingIndexPartitionKey = string(SubscriberPending)
View Source
const DynamoDbPrimaryKey = "email"
View Source
const DynamoDbVerifiedIndexName string = string(SubscriberVerified)

Sparse Global Secondary Index for records containing a "verified" attribute.

View Source
const DynamoDbVerifiedIndexPartitionKey string = string(SubscriberVerified)
View Source
const ErrSubscriberNotFound = types.SentinelError("is not a subscriber")

ErrSubscriberNotFound indicates that an email address isn't subscribed.

Database.Get returns this error when the underlying database request succeeded, but there was no such Subscriber.

View Source
const TimestampFormat = time.RFC1123Z

Variables

View Source
var DynamoDbCreateTableInput = &dynamodb.CreateTableInput{
	AttributeDefinitions: []dbtypes.AttributeDefinition{
		{
			AttributeName: aws.String(DynamoDbPrimaryKey),
			AttributeType: dbtypes.ScalarAttributeTypeS,
		},
		{
			AttributeName: aws.String(DynamoDbPendingIndexPartitionKey),
			AttributeType: dbtypes.ScalarAttributeTypeN,
		},
		{
			AttributeName: aws.String(DynamoDbVerifiedIndexPartitionKey),
			AttributeType: dbtypes.ScalarAttributeTypeN,
		},
	},
	KeySchema: []dbtypes.KeySchemaElement{
		{
			AttributeName: aws.String(DynamoDbPrimaryKey),
			KeyType:       dbtypes.KeyTypeHash,
		},
	},
	BillingMode: dbtypes.BillingModePayPerRequest,
	GlobalSecondaryIndexes: []dbtypes.GlobalSecondaryIndex{
		{
			IndexName: aws.String(DynamoDbPendingIndexName),
			KeySchema: []dbtypes.KeySchemaElement{
				{
					AttributeName: aws.String(DynamoDbPendingIndexPartitionKey),
					KeyType:       dbtypes.KeyTypeHash,
				},
			},
			Projection: DynamoDbIndexProjection,
		},
		{
			IndexName: aws.String(DynamoDbVerifiedIndexName),
			KeySchema: []dbtypes.KeySchemaElement{
				{
					AttributeName: aws.String(
						DynamoDbVerifiedIndexPartitionKey,
					),
					KeyType: dbtypes.KeyTypeHash,
				},
			},
			Projection: DynamoDbIndexProjection,
		},
	},
}
View Source
var DynamoDbIndexProjection *dbtypes.Projection = &dbtypes.Projection{
	ProjectionType: dbtypes.ProjectionTypeAll,
}

Functions

This section is empty.

Types

type Database

type Database interface {
	Get(ctx context.Context, email string) (*Subscriber, error)
	Put(ctx context.Context, subscriber *Subscriber) error
	Delete(ctx context.Context, email string) error
	ProcessSubscribers(
		context.Context, SubscriberStatus, SubscriberProcessor,
	) error
}

type DynamoDb

type DynamoDb struct {
	Client    DynamoDbClient
	TableName string
}

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html

func NewDynamoDb

func NewDynamoDb(cfg aws.Config, tableName string) *DynamoDb

func (*DynamoDb) CreateSubscribersTable

func (db *DynamoDb) CreateSubscribersTable(
	ctx context.Context, maxWaitDuration time.Duration,
) (err error)

func (*DynamoDb) Delete

func (db *DynamoDb) Delete(ctx context.Context, email string) (err error)

func (*DynamoDb) DeleteTable

func (db *DynamoDb) DeleteTable(ctx context.Context) (err error)

func (*DynamoDb) Get

func (db *DynamoDb) Get(
	ctx context.Context, email string,
) (subscriber *Subscriber, err error)

func (*DynamoDb) ProcessSubscribers

func (db *DynamoDb) ProcessSubscribers(
	ctx context.Context, status SubscriberStatus, sp SubscriberProcessor,
) error

func (*DynamoDb) Put

func (db *DynamoDb) Put(ctx context.Context, sub *Subscriber) (err error)

type Subscriber

type Subscriber struct {
	Email     string
	Uid       uuid.UUID
	Status    SubscriberStatus
	Timestamp time.Time
}

func NewSubscriber

func NewSubscriber(email string) *Subscriber

func (*Subscriber) String

func (sub *Subscriber) String() string

type SubscriberFunc

type SubscriberFunc func(sub *Subscriber) bool

SubscriberFunc is an adapter to allow processing of Subscriber objects using plain functions.

Inspired by: https://pkg.go.dev/net/http#HandlerFunc

func (SubscriberFunc) Process

func (f SubscriberFunc) Process(sub *Subscriber) bool

SubscriberFunc calls and returns f(sub).

type SubscriberProcessor

type SubscriberProcessor interface {
	Process(*Subscriber) bool
}

A SubscriberProcessor performs an operation on a Subscriber.

Process should return true if processing should continue with the next Subscriber, or false if processing should halt.

type SubscriberStatus

type SubscriberStatus string
const (
	SubscriberPending  SubscriberStatus = "pending"
	SubscriberVerified SubscriberStatus = "verified"
)

Jump to

Keyboard shortcuts

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