data

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package data provides the generic repository pattern and ORM adapter interfaces for the Helix framework.

Index

Constants

This section is empty.

Variables

View Source
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 Condition

type Condition struct {
	Field    string
	Operator Operator
	Value    any
}

Condition describes one ORM-neutral field comparison.

func (Condition) Validate

func (c Condition) Validate() error

Validate returns an error if the condition cannot be translated safely by adapters.

type Filter

type Filter struct {
	Conditions []Condition
	Logic      LogicalOperator
}

Filter describes ORM-neutral query conditions.

func NewFilter

func NewFilter(logic LogicalOperator, conditions ...Condition) (Filter, error)

NewFilter builds and validates a portable repository filter.

func (Filter) Validate

func (f Filter) Validate() error

Validate returns an error if the filter cannot be translated safely by adapters.

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 Page

type Page[T any] struct {
	Items    []T
	Total    int
	Page     int
	PageSize int
}

Page contains a paginated repository result and its metadata.

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.

Directories

Path Synopsis
Package gorm provides the GORM-based implementation of the Helix repository interfaces.
Package gorm provides the GORM-based implementation of the Helix repository interfaces.

Jump to

Keyboard shortcuts

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