Documentation
¶
Overview ¶
Package store provides universal data access contracts for the Credo framework.
This package defines error sentinels, lifecycle/health interfaces, a connection registry, a registration API, 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.
Registration ¶
A store is bound like any other component — app.ProvideValue, or app.Provide with a constructor, with credo.Borrowed() for a handle the caller shares — and Register adds that binding to the App's stores:
app.ProvideValue(db) store.Register[*sqldb.DB](app)
Register performs no I/O. The start phase resolves the binding once, after Finalize and so after every override, and pings it before the components that depend on it start; a failed ping fails the start. A registration whose type has no binding fails Finalize. Shutdown follows the binding: the App shuts the store down after its consumers unless it is borrowed.
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 shares one teardown with the other holders of that resource, which happens once, after the last holder retires. Only a value without state of its own to release may share an identity.
Every pinged store contributes a stable readiness probe, built once at start; nothing is resolved per readiness request. Named and store checks run in parallel with enforced per-check deadlines and panic isolation. Health.Cause carries typed diagnostics for logging while remaining excluded from JSON; free-form Details values are never interpreted as error causes. Overlapping readiness requests share one in-flight execution per store, so a cancellation-ignoring probe cannot accumulate a goroutine per request.
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
- Variables
- func Conn[T any](ctx context.Context, fallback T) Tdeprecated
- func ConnInScope[T any](ctx context.Context, scope *TxScope[T], fallback T) T
- func GetTx[T any](ctx context.Context) (T, bool)deprecated
- func GetTxInScope[T any](ctx context.Context, scope *TxScope[T]) (T, bool)
- func IsTransient(err error) bool
- func Register[R Lifecycle](app *credo.App, opts ...RegisterOption)
- func RequireTxInScope[T any](ctx context.Context, scope *TxScope[T]) (T, error)
- func WithTx[T any](ctx context.Context, tx T) context.Contextdeprecated
- func WithTxInScope[T any](ctx context.Context, scope *TxScope[T], tx T) context.Context
- func Wrap(kind error, cause error) error
- type Error
- type Health
- type HealthStatus
- type Kind
- type Lifecycle
- type RegisterOption
- type TxScope
Constants ¶
const DefaultPingTimeout = 5 * time.Second
DefaultPingTimeout is the default context deadline of the ping the start phase sends a registered store. Lifecycle.Ping implementations must honor the context; the start phase abandons a Ping that ignores it only at the rollback deadline.
Variables ¶
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") // 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.
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
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 ¶
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
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 ¶
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 ¶
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 Register ¶
func Register[R Lifecycle](app *credo.App, opts ...RegisterOption)
Register adds the binding of R to the App's store registry. The store itself is bound where its ownership is decided — app.ProvideValue, or app.Provide with a constructor, with credo.Borrowed() for a handle the caller shares — and Register names that binding; it performs no I/O.
The start phase resolves R once, after Finalize and so after every override, and pings that value as its first start step, before the components that depend on it start; a failed ping is a start failure. The pinged value is what /ready reports, with its typed Health, and nothing is resolved per readiness request. The binding is a component like any other: the App shuts it down after its consumers unless it is borrowed, and holders of one resource — a *sqldb.DB bound raw and through a wrapper that embeds it — share one teardown.
R may be bound directly or be an interface an Alias names. A registration whose R has no binding fails Finalize. Register panics on a nil app, a nil option, an invalid name or ping timeout, a type or name registered twice, and after Finalize or shutdown.
func RequireTxInScope ¶
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
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 ¶
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.
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) HTTPStatus
deprecated
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.
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 ¶
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 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.
type Lifecycle ¶
type Lifecycle interface {
// Ping verifies the connection is alive and must honor ctx cancellation;
// the start phase calls it once, with a deadline.
Ping(ctx context.Context) error
// Shutdown gracefully closes the connection.
// Implementations should respect ctx.Done() for timely cleanup.
Shutdown(ctx context.Context) error
// Health returns structured health information including status,
// latency, and adapter-specific details (pool stats, version, etc.).
Health(ctx context.Context) Health
}
Lifecycle is the contract a data store binding meets for Register: the start phase pings it, /ready reports its health, and the App shuts it down after its consumers, as it does every component. Adapters such as store/sqldb implement it. A value that cannot is registered through a wrapper type that does.
An implementation that represents a resource another value also holds — a wrapper over a *sqldb.DB — shares its teardown through credo.ResourceIdentifier: values with one identity are shut down once.
type RegisterOption ¶
type RegisterOption func(*registerOptions)
RegisterOption configures a Register call.
func WithName ¶
func WithName(name string) RegisterOption
WithName sets the stable identifier used in health reporting. It rejects an empty or padded name, control characters, and the reserved "credo." prefix. If omitted, Register uses the pointer-unwrapped, package-qualified name of R.
func WithPingTimeout ¶
func WithPingTimeout(d time.Duration) RegisterOption
WithPingTimeout overrides the default deadline (5s) of the ping the start phase sends the store.
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 ¶
NewTxScope creates a unique transaction scope whose transaction type is T.
func (*TxScope[T]) Conn ¶
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 ¶
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 ¶
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.