store

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package store defines tenant metadata storage contracts and memory, cached, and SQL implementations.

Index

Constants

View Source
const (
	// DefaultSQLTableName is the default tenant metadata table name.
	DefaultSQLTableName = "tenants"
)

Variables

View Source
var (
	// ErrTenantNotFound reports that a tenant does not exist in the store.
	ErrTenantNotFound = errors.New("gotenancy/store: tenant not found")

	// ErrTenantAlreadyExists reports that a tenant already exists in the store.
	ErrTenantAlreadyExists = errors.New("gotenancy/store: tenant already exists")

	// ErrInvalidTenant reports that tenant metadata is not valid for persistence.
	ErrInvalidTenant = errors.New("gotenancy/store: invalid tenant")

	// ErrInvalidListFilter reports an invalid tenant list filter.
	ErrInvalidListFilter = errors.New("gotenancy/store: invalid list filter")

	// ErrNilStore reports that a store dependency is nil.
	ErrNilStore = errors.New("gotenancy/store: nil store")

	// ErrNilCache reports that a cache dependency is nil.
	ErrNilCache = errors.New("gotenancy/store: nil cache")

	// ErrNilDB reports that a SQL store was created with a nil database handle.
	ErrNilDB = errors.New("gotenancy/store: nil db")

	// ErrInvalidTableName reports an unsafe SQL table name.
	ErrInvalidTableName = errors.New("gotenancy/store: invalid table name")

	// ErrUnsupportedSQLDialect reports an unsupported SQLStore dialect.
	ErrUnsupportedSQLDialect = errors.New("gotenancy/store: unsupported sql dialect")

	// ErrInvalidCacheSize reports an invalid bounded memory cache size.
	ErrInvalidCacheSize = errors.New("gotenancy/store: invalid cache size")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Get(ctx context.Context, id types.TenantID) (types.Tenant, bool, error)
	Set(ctx context.Context, tenant types.Tenant, ttl time.Duration) error
	Delete(ctx context.Context, id types.TenantID) error
	Invalidate(ctx context.Context) error
}

Cache stores tenant metadata for faster lookups.

type CachedStore

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

CachedStore wraps a Store with cache-aside reads and write-through invalidation.

func NewCachedStore

func NewCachedStore(next Store, cache Cache, ttl time.Duration) (*CachedStore, error)

NewCachedStore creates a cached store decorator.

func (*CachedStore) Create

func (store *CachedStore) Create(ctx context.Context, tenant types.Tenant) error

Create inserts tenant metadata and refreshes the cache.

func (*CachedStore) Delete

func (store *CachedStore) Delete(ctx context.Context, id types.TenantID) error

Delete removes tenant metadata and invalidates its cache entry.

func (*CachedStore) Get

func (store *CachedStore) Get(ctx context.Context, id types.TenantID) (types.Tenant, error)

Get returns tenant metadata from cache when available, otherwise from the wrapped store.

func (*CachedStore) List

func (store *CachedStore) List(ctx context.Context, filter ListFilter) ([]types.Tenant, error)

List delegates to the wrapped store.

func (*CachedStore) Update

func (store *CachedStore) Update(ctx context.Context, tenant types.Tenant) error

Update replaces tenant metadata and refreshes the cache.

type ListFilter

type ListFilter struct {
	Statuses []types.TenantStatus
	Limit    int
	Offset   int
}

ListFilter restricts tenant list queries.

type MemoryCache

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

MemoryCache is a thread-safe in-memory tenant cache with TTL support.

func NewBoundedMemoryCache

func NewBoundedMemoryCache(maxEntries int) (*MemoryCache, error)

NewBoundedMemoryCache creates a memory cache with a maximum number of entries.

func NewMemoryCache

func NewMemoryCache() *MemoryCache

NewMemoryCache creates an empty memory cache.

func (*MemoryCache) Delete

func (cache *MemoryCache) Delete(ctx context.Context, id types.TenantID) error

Delete removes cached tenant metadata by ID.

func (*MemoryCache) Get

func (cache *MemoryCache) Get(ctx context.Context, id types.TenantID) (types.Tenant, bool, error)

Get returns cached tenant metadata.

func (*MemoryCache) Invalidate

func (cache *MemoryCache) Invalidate(ctx context.Context) error

Invalidate removes all cached tenant metadata.

func (*MemoryCache) Set

func (cache *MemoryCache) Set(ctx context.Context, tenant types.Tenant, ttl time.Duration) error

Set stores tenant metadata with ttl. A non-positive ttl means no expiration.

type MemoryStore

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

MemoryStore is a thread-safe in-memory tenant metadata store.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates an empty memory store.

func (*MemoryStore) Create

func (store *MemoryStore) Create(ctx context.Context, tenant types.Tenant) error

Create inserts tenant metadata.

func (*MemoryStore) Delete

func (store *MemoryStore) Delete(ctx context.Context, id types.TenantID) error

Delete removes tenant metadata by ID.

func (*MemoryStore) Get

func (store *MemoryStore) Get(ctx context.Context, id types.TenantID) (types.Tenant, error)

Get returns tenant metadata by ID.

func (*MemoryStore) List

func (store *MemoryStore) List(ctx context.Context, filter ListFilter) ([]types.Tenant, error)

List returns tenants matching filter.

func (*MemoryStore) Update

func (store *MemoryStore) Update(ctx context.Context, tenant types.Tenant) error

Update replaces existing tenant metadata.

type SQLDialect

type SQLDialect string

SQLDialect controls SQL placeholder rendering for SQLStore.

const (
	// SQLDialectMySQL uses question-mark placeholders and is the default.
	SQLDialectMySQL SQLDialect = "mysql"

	// SQLDialectSQLite uses question-mark placeholders.
	SQLDialectSQLite SQLDialect = "sqlite"

	// SQLDialectPostgres uses numbered placeholders.
	SQLDialectPostgres SQLDialect = "postgres"
)

type SQLStore

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

SQLStore persists tenant metadata through database/sql.

The table is expected to contain these columns: id, name, status, plan_id, config. The config column stores a JSON object with string keys and values.

func NewSQLStore

func NewSQLStore(db *sql.DB, opts ...SQLStoreOption) (*SQLStore, error)

NewSQLStore creates a SQL-backed tenant metadata store.

func (*SQLStore) Create

func (store *SQLStore) Create(ctx context.Context, tenant types.Tenant) error

Create inserts tenant metadata.

func (*SQLStore) Delete

func (store *SQLStore) Delete(ctx context.Context, id types.TenantID) error

Delete removes tenant metadata by ID.

func (*SQLStore) Get

func (store *SQLStore) Get(ctx context.Context, id types.TenantID) (types.Tenant, error)

Get returns tenant metadata by ID.

func (*SQLStore) List

func (store *SQLStore) List(ctx context.Context, filter ListFilter) (tenants []types.Tenant, err error)

List returns tenants matching filter.

func (*SQLStore) Update

func (store *SQLStore) Update(ctx context.Context, tenant types.Tenant) error

Update replaces existing tenant metadata.

type SQLStoreOption

type SQLStoreOption func(*SQLStore) error

SQLStoreOption configures SQLStore.

func WithSQLDialect

func WithSQLDialect(dialect SQLDialect) SQLStoreOption

WithSQLDialect configures SQL placeholder rendering.

func WithTableName

func WithTableName(table string) SQLStoreOption

WithTableName overrides the default tenant metadata table name.

type Store

type Store interface {
	Get(ctx context.Context, id types.TenantID) (types.Tenant, error)
	List(ctx context.Context, filter ListFilter) ([]types.Tenant, error)
	Create(ctx context.Context, tenant types.Tenant) error
	Update(ctx context.Context, tenant types.Tenant) error
	Delete(ctx context.Context, id types.TenantID) error
}

Store persists tenant metadata.

Jump to

Keyboard shortcuts

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