dynamostore

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package dynamostore provides a generic single-table DynamoDB store with tenant isolation via partition key prefix.

Table design:

PK: TENANT#{org_id}
SK: {FEATURE}#{id}
GSI1PK: {FEATURE}      (feature-time-index)
GSI1SK: {created_at}

All features share one table; tenant isolation is enforced by scoping every read and write to the tenant's partition key.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoTenant = errors.New("dynamostore: tenant ID not set")

ErrNoTenant is returned when a tenant context has not been set.

View Source
var ErrNotFound = errors.New("dynamostore: not found")

ErrNotFound is returned when a requested item does not exist.

Functions

This section is empty.

Types

type Item

type Item struct {
	PK        string `dynamodbav:"pk"`
	SK        string `dynamodbav:"sk"`
	Feature   string `dynamodbav:"feature"`
	Data      string `dynamodbav:"data"` // JSON-encoded payload
	CreatedAt string `dynamodbav:"created_at"`
	UpdatedAt string `dynamodbav:"updated_at"`
	TTL       *int64 `dynamodbav:"ttl,omitempty"`
}

Item is the raw DynamoDB item shape stored in the table.

type QueryOpts

type QueryOpts struct {
	Limit     int32
	StartTime *time.Time // inclusive lower bound on created_at
	EndTime   *time.Time // exclusive upper bound on created_at
	UseGSI    bool       // if true, queries the feature-time-index GSI
}

QueryOpts controls the behaviour of Query.

type Store

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

Store is a generic DynamoDB store scoped to a single table. Create one Store per application and use WithTenant to produce per-request copies bound to a specific tenant.

func New

func New(client *dynamodb.Client, tableName string) *Store

New creates a new Store for the given table.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, feature, id string) error

Delete removes a single item.

func (*Store) Get

func (s *Store) Get(ctx context.Context, feature, id string, out any) error

Get retrieves a single item and unmarshals it into out.

func (*Store) List

func (s *Store) List(ctx context.Context, feature string, out any) error

List retrieves all items for a given feature within the current tenant and unmarshals them into out (which must be a pointer to a slice).

func (*Store) Put

func (s *Store) Put(ctx context.Context, feature, id string, item any) error

Put stores an item, serialising it to JSON in the data column.

func (*Store) PutWithTTL

func (s *Store) PutWithTTL(ctx context.Context, feature, id string, item any, ttl *int64) error

PutWithTTL stores an item with an optional TTL (epoch seconds).

func (*Store) Query

func (s *Store) Query(ctx context.Context, feature string, opts QueryOpts) ([]Item, error)

Query retrieves items for a feature, optionally using the GSI and time filters.

func (*Store) TenantID

func (s *Store) TenantID() string

TenantID returns the current tenant context.

func (*Store) UpdateData

func (s *Store) UpdateData(ctx context.Context, feature, id string, item any) error

UpdateData performs a Put that preserves the original created_at timestamp.

func (*Store) WithTenant

func (s *Store) WithTenant(tenantID string) *Store

WithTenant returns a shallow copy of the Store bound to the given tenant. This must be called per-request from auth middleware.

Jump to

Keyboard shortcuts

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