Documentation
¶
Overview ¶
Package data provides the generic repository pattern and ORM adapter interfaces for the Helix framework.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrRecordNotFound is returned when a repository cannot find the requested record. ErrRecordNotFound = errors.New("data: record not found") // ErrDuplicateKey is returned when a repository detects a unique key conflict. ErrDuplicateKey = errors.New("data: duplicate key") // ErrInvalidFilter is returned when a filter cannot be translated safely. ErrInvalidFilter = errors.New("data: invalid filter") )
Functions ¶
func ContextWithTransaction ¶
func ContextWithTransaction[TX any](ctx context.Context, tx Transaction[TX]) (context.Context, error)
ContextWithTransaction returns a child context carrying tx for propagation. The transaction is keyed by TX, so different adapter transaction types do not collide.
Types ¶
type Filter ¶
type Filter struct {
Conditions []Condition
Logic LogicalOperator
}
Filter describes ORM-neutral query conditions.
type LogicalOperator ¶
type LogicalOperator string
LogicalOperator describes how filter conditions are combined.
const ( // LogicalDefault combines conditions with AND when no operator is provided. LogicalDefault LogicalOperator = "" // LogicalAnd requires every condition to match. LogicalAnd LogicalOperator = "and" // LogicalOr requires at least one condition to match. LogicalOr LogicalOperator = "or" )
type Operator ¶
type Operator string
Operator describes a portable repository filter comparison.
const ( // OperatorEqual matches records where a field equals the provided value. OperatorEqual Operator = "eq" // OperatorNotEqual matches records where a field differs from the provided value. OperatorNotEqual Operator = "ne" // OperatorGreaterThan matches records where a field is greater than the provided value. OperatorGreaterThan Operator = "gt" // OperatorGreaterThanOrEqual matches records where a field is greater than or equal to the value. OperatorGreaterThanOrEqual Operator = "gte" // OperatorLessThan matches records where a field is less than the provided value. OperatorLessThan Operator = "lt" // OperatorLessThanOrEqual matches records where a field is less than or equal to the value. OperatorLessThanOrEqual Operator = "lte" // OperatorContains matches records where a field contains the provided value. OperatorContains Operator = "contains" // OperatorIn matches records where a field belongs to the provided value set. OperatorIn Operator = "in" // OperatorIsNull matches records where a field is null. OperatorIsNull Operator = "is_null" // OperatorIsNotNull matches records where a field is not null. OperatorIsNotNull Operator = "is_not_null" )
type Repository ¶
type Repository[T any, ID any, TX any] interface { FindAll(ctx context.Context) ([]T, error) FindByID(ctx context.Context, id ID) (*T, error) FindWhere(ctx context.Context, filter Filter) ([]T, error) Save(ctx context.Context, entity *T) error Delete(ctx context.Context, id ID) error Paginate(ctx context.Context, page, size int) (Page[T], error) // WithTransaction returns a new Repository bound to tx. tx must not be nil. WithTransaction(tx Transaction[TX]) Repository[T, ID, TX] }
Repository defines the generic, ORM-neutral persistence contract. TX is the adapter-specific transaction type exposed via Transaction[TX].
type Transaction ¶
type Transaction[TX any] interface { Unwrap() TX }
Transaction wraps an adapter-specific transaction without exposing the ORM. Implementations must not return nil from Unwrap.
func TransactionFromContext ¶
func TransactionFromContext[TX any](ctx context.Context) (Transaction[TX], bool)
TransactionFromContext returns the active transaction for TX, when one exists.
type TransactionManager ¶
type TransactionManager[TX any] interface { WithinTransaction(ctx context.Context, fn func(context.Context, Transaction[TX]) error) error }
TransactionManager executes callbacks inside an adapter-specific transaction. Implementations must propagate an existing transaction from ctx when present.