store

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 6 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package store provides universal data access contracts for the Credo framework.

This package defines error sentinels, the typed Health report, and context-based transaction helpers. It has zero external dependencies — only the Go standard library and Credo's root/fault contracts are imported.

The companion package store/sqldb (a separate Go submodule) wraps *bun.DB with lifecycle management, query builder proxies, error mapping, and transaction support.

Universal Errors

Store errors expose a transport-neutral semantic Kind. The default HTTP policy and future protocol adapters consume that kind independently:

if kind, ok := store.KindOf(err); ok {
    // branch on KindNotFound, KindConstraint, KindDeadlock, ...
}

Error retains diagnostic code/constraint/resource metadata and the original cause without exposing them in Credo's default HTTP response. The sentinel HTTPStatus methods remain only as a deprecated compatibility bridge; new application policy should use Kind.

A Bound Store

A store is a component like any other: it is bound with app.ProvideValue, or app.Provide with a constructor, with credo.Borrowed() for a handle the caller shares, and the binding alone is enough:

app.ProvideValue(db)

The start walk calls the store's Start, which pings it, before the components that depend on it start; /ready reports it through its Ready; and the App shuts it down after its consumers unless it is borrowed. A store adapter implements Start, Ready and Shutdown, as store/sqldb's *DB does. credo.OptionalReadiness() reports a store in /ready without letting it gate readiness, and credo.ComponentName names its entry.

Several bindings may hold one physical resource — a *sqldb.DB bound raw and a wrapper type that embeds it. A value that names its resource through credo.ResourceIdentifier is one resource with the other holders of that resource: one start, one readiness entry and one teardown, which happens once, after the last holder retires. Only a value without state of its own to release may share an identity.

Health.Cause carries typed diagnostics for logging while remaining excluded from JSON; free-form Details values are never interpreted as error causes.

Context-Based Transactions

A custom adapter creates one typed TxScope per logical connection. The transaction type is fixed at construction, preventing a concrete/interface mismatch from silently selecting the fallback connection:

scope := store.NewTxScope[Client]()
ctx = scope.WithTx(ctx, tx)
conn := scope.Conn(ctx, base)
tx, err := scope.RequireTx(ctx) // no fallback

The companion store/sqldb adapter owns a private typed scope per DB. Its proxy terminals participate automatically; db.Conn(ctx) is the transaction-aware Bun escape hatch. The standalone WithTx, GetTx, and Conn helpers remain only as deprecated compatibility APIs.

Maturity: beta

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound indicates that the requested record does not exist.
	ErrNotFound error = newKindError(KindNotFound, "store: record not found")

	// ErrAlreadyExists indicates a unique or primary-key violation.
	ErrAlreadyExists error = newKindError(KindAlreadyExists, "store: duplicate record")

	// ErrDuplicate is the compatibility name for [ErrAlreadyExists].
	//
	// Deprecated: use ErrAlreadyExists or inspect [KindAlreadyExists].
	ErrDuplicate = ErrAlreadyExists

	// ErrConstraint indicates a persistent integrity constraint violation other
	// than a duplicate, such as foreign-key, not-null, or check failure.
	ErrConstraint error = newKindError(KindConstraint, "store: constraint violation")

	// ErrSerialization indicates that transaction serialization failed.
	ErrSerialization error = newKindError(KindSerialization, "store: serialization failure")

	// ErrDeadlock indicates that the database detected a deadlock.
	ErrDeadlock error = newKindError(KindDeadlock, "store: deadlock")

	// ErrContention indicates a transient lock/busy conflict whose safe retry
	// scope is not implied by this sentinel.
	ErrContention error = newKindError(KindContention, "store: contention")

	// ErrConflict is the legacy umbrella for constraint and concurrency errors.
	// New classifiers return a more specific sentinel whose errors.Is method
	// still matches ErrConflict.
	//
	// Deprecated: inspect ErrConstraint, ErrSerialization, ErrDeadlock, or
	// ErrContention instead.
	ErrConflict error = newKindError(KindConflict, "store: conflict")

	// ErrTimeout indicates that a database operation exceeded a verified
	// deadline or statement timeout.
	ErrTimeout error = newKindError(KindTimeout, "store: timeout")

	// ErrUnavailable indicates that a database connection or service is
	// temporarily unavailable.
	ErrUnavailable error = newKindError(KindUnavailable, "store: unavailable")

	// ErrReadOnly indicates that a write was attempted against a read-only
	// transaction or server.
	ErrReadOnly error = newKindError(KindReadOnly, "store: read-only")
)

Sentinel errors for data access operations.

View Source
var ErrTxMissing = errors.New("store: transaction missing from context")

ErrTxMissing indicates that a transaction-required operation was called without a transaction in its context scope.

Functions

func Conn deprecated

func Conn[T any](ctx context.Context, fallback T) T

Conn returns the transaction from context if present, otherwise returns the fallback connection. Repositories call this in every method for opt-in TX participation.

Deprecated: create a typed TxScope with NewTxScope and use its Conn method. A standalone type-keyed helper cannot isolate multiple logical connections that share T or prevent concrete/interface mismatches.

func ConnInScope

func ConnInScope[T any](ctx context.Context, scope *TxScope[T], fallback T) T

ConnInScope returns the transaction from context for the given scope if present, otherwise returns the fallback connection. Panics if scope is nil.

func GetTx deprecated

func GetTx[T any](ctx context.Context) (T, bool)

GetTx retrieves a transaction handle from the context. Returns the zero value and false if no TX of type T is stored.

Deprecated: create a typed TxScope with NewTxScope and use its GetTx method. A standalone type-keyed helper cannot prevent concrete/interface type mismatches across call sites.

func GetTxInScope

func GetTxInScope[T any](ctx context.Context, scope *TxScope[T]) (T, bool)

GetTxInScope retrieves a scoped transaction handle from the context. Returns the zero value and false if no TX of type T is stored for the scope. Panics if scope is nil.

func IsTransient

func IsTransient(err error) bool

IsTransient reports whether the primary store classification describes a condition that may clear. It never means an operation, transaction callback, or externally visible side effect is safe to retry.

func RequireTxInScope

func RequireTxInScope[T any](ctx context.Context, scope *TxScope[T]) (T, error)

RequireTxInScope retrieves a scoped transaction handle or returns ErrTxMissing when the scope has no transaction in ctx. Panics if scope is nil.

func WithTx deprecated

func WithTx[T any](ctx context.Context, tx T) context.Context

WithTx stores a transaction handle in the context. T is the transaction type (e.g., bun.Tx, bun.IDB). Panics if tx is nil, including a typed-nil pointer stored in an interface T.

Deprecated: create a typed TxScope with NewTxScope and use its WithTx method. A standalone type-keyed helper cannot prevent a producer and consumer from choosing different concrete/interface forms of the same transaction type.

func WithTxInScope

func WithTxInScope[T any](ctx context.Context, scope *TxScope[T], tx T) context.Context

WithTxInScope stores a transaction handle in the context for a specific logical connection scope. Panics if scope is nil — scopes are created once at wiring time via NewTxScope, so a nil scope is a programming error. Panics if tx is nil, including a typed-nil pointer stored in an interface T.

func Wrap

func Wrap(kind error, cause error) error

Wrap preserves cause while classifying it with a store sentinel. Nil causes return nil. Unsupported kinds and an already matching cause are returned unchanged for compatibility.

Types

type Error

type Error struct {
	Kind       Kind
	Op         string
	Resource   string
	Constraint string
	Code       string
	Transient  bool
	Cause      error
}

Error is a structured, transport-neutral data-access error.

Code, Constraint, Resource, and Cause are internal diagnostic metadata. The default HTTP renderer classifies only Kind and never serializes these fields. Transient describes whether the condition may clear; it does not guarantee that replaying the operation or transaction is safe.

func (*Error) Error

func (e *Error) Error() string

Error returns the original cause message when available.

func (*Error) FaultKind

func (e *Error) FaultKind() fault.Kind

FaultKind exposes the semantic category to transport policy packages.

func (*Error) HTTPStatus deprecated

func (e *Error) HTTPStatus() int

HTTPStatus returns the legacy default HTTP mapping for this error.

Deprecated: inspect KindOf and apply transport policy at the application boundary. Credo's root error pipeline now uses FaultKind before this bridge.

func (*Error) Is

func (e *Error) Is(target error) bool

Is matches the exact semantic sentinel and the deprecated ErrConflict umbrella where applicable. Cause matching is handled by Unwrap.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the original driver or application cause.

type Health

type Health struct {
	// Status is the current health state.
	Status HealthStatus

	// Latency is the round-trip time of the health check probe.
	Latency time.Duration

	// Cause is the typed internal reason for a down or degraded result.
	// It is excluded from JSON so unauthenticated readiness endpoints cannot
	// leak connection targets or driver diagnostics. Credo logs it and exposes
	// its text only when HealthConfig.ExposeErrors is explicitly enabled.
	Cause error `json:"-"`

	// Details holds adapter-specific information such as database version,
	// connection pool statistics, or replication lag.
	Details map[string]any
}

Health is the result of a connection health check.

func (Health) Clone

func (h Health) Clone() Health

Clone returns a defensive copy of the health snapshot. Cause identity is preserved. Details is cloned at the top level; nested maps, slices, pointers, and other reference values remain shared.

type HealthStatus

type HealthStatus string

HealthStatus represents the health state of a data store connection.

const (
	// StatusUp indicates the connection is healthy.
	StatusUp HealthStatus = "UP"

	// StatusDown indicates the connection is unreachable.
	StatusDown HealthStatus = "DOWN"

	// StatusDegraded indicates the connection is available but impaired.
	StatusDegraded HealthStatus = "DEGRADED"
)

type Kind

type Kind = fault.Kind

Kind is the transport-neutral semantic category of a store error.

const (
	KindUnknown       Kind = fault.KindUnknown
	KindNotFound      Kind = fault.KindNotFound
	KindAlreadyExists Kind = fault.KindAlreadyExists
	KindConstraint    Kind = fault.KindConstraint
	KindSerialization Kind = fault.KindSerialization
	KindDeadlock      Kind = fault.KindDeadlock
	KindContention    Kind = fault.KindContention
	KindConflict      Kind = fault.KindConflict
	KindTimeout       Kind = fault.KindTimeout
	KindUnavailable   Kind = fault.KindUnavailable
	KindReadOnly      Kind = fault.KindReadOnly
)

Store error kinds. They are aliases of the shared transport-neutral kinds so Credo's HTTP policy and future gRPC policy can classify them without importing store and creating a package cycle.

func KindOf

func KindOf(err error) (Kind, bool)

KindOf finds the first recognized store Kind in err's unwrap/join tree.

type TxScope

type TxScope[T any] struct {
	// contains filtered or unexported fields
}

TxScope isolates transactions that share the same Go type but belong to different logical connections. T is fixed when the scope is created, so a concrete transaction cannot be stored and then silently missed by reading the same scope through a different interface type.

The marker field keeps TxScope non-zero-size on purpose: in Go, pointers to distinct zero-size structs may share an address, which would let two independent scopes collide as context keys. A non-empty field guarantees a unique address per NewTxScope call.

func NewTxScope

func NewTxScope[T any]() *TxScope[T]

NewTxScope creates a unique transaction scope whose transaction type is T.

func (*TxScope[T]) Conn

func (s *TxScope[T]) Conn(ctx context.Context, fallback T) T

Conn returns this scope's transaction from the context if present, otherwise the fallback connection — the call repositories make for opt-in scoped TX participation. It is the method form of ConnInScope.

func (*TxScope[T]) GetTx

func (s *TxScope[T]) GetTx(ctx context.Context) (T, bool)

GetTx retrieves this scope's transaction handle from the context, returning the zero value and false when none is stored. It is the method form of GetTxInScope.

func (*TxScope[T]) RequireTx

func (s *TxScope[T]) RequireTx(ctx context.Context) (T, error)

RequireTx retrieves this scope's transaction handle or returns ErrTxMissing when none is stored. It never falls back to a base connection. It is the method form of RequireTxInScope.

func (*TxScope[T]) WithTx

func (s *TxScope[T]) WithTx(ctx context.Context, tx T) context.Context

WithTx stores a transaction handle in the context for this scope. It is the method form of WithTxInScope. It panics for nil transaction values, including typed-nil pointers held by an interface T.

Directories

Path Synopsis
sqldb module

Jump to

Keyboard shortcuts

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