repository

package
v0.0.0-...-51ec7a6 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package repository provides typed PostgreSQL persistence for Soro models.

Index

Constants

View Source
const DefaultMaxAllRows = 10000

DefaultMaxAllRows bounds All so an unbounded table cannot be loaded into memory by accident. Override with WithMaxAllRows.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock func() time.Time

type OperationOption

type OperationOption func(*operationSettings)

func BeforePersist

func BeforePersist(callback func(context.Context) error) OperationOption

BeforePersist runs after validation and all before-lifecycle hooks, immediately before SQL. It is intended for final-state authorization and invariants.

func SkipAllHooks

func SkipAllHooks() OperationOption

SkipAllHooks deliberately bypasses every lifecycle hook. Validation still runs.

func SkipHooks

func SkipHooks(stages ...lifecycle.Stage) OperationOption

SkipHooks deliberately bypasses only the named lifecycle stages.

func WithLifecycleMetadata

func WithLifecycleMetadata(metadata map[string]any) OperationOption

WithLifecycleMetadata adds operation-local data visible to hooks.

type Option

type Option func(*settings)

func WithClock

func WithClock(clock Clock) Option

func WithMaxAllRows

func WithMaxAllRows(limit int) Option

WithMaxAllRows bounds how many rows All may load before it refuses. The default is DefaultMaxAllRows. A negative limit disables the bound for genuinely unbounded reads that the caller has reasoned about.

func WithUUIDGenerator

func WithUUIDGenerator(generator UUIDGenerator) Option

type QueryScope

type QueryScope func(*bun.SelectQuery) *bun.SelectQuery

QueryScope constrains a repository query. Returning nil rejects the operation instead of falling back to an unscoped query.

type Repository

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

func New

func New[T any](db *database.DB, options ...Option) *Repository[T]

New constructs a typed repository. An invalid model contract is reported by every operation, keeping normal construction compatible with one-value use.

func (*Repository[T]) All

func (r *Repository[T]) All(ctx context.Context) ([]*T, error)

All loads every non-deleted row into memory. It suits bounded reference tables; for anything that grows with tenants or time, use Query with pagination instead.

The result is capped at the repository's row limit and exceeding it is an error rather than a silent truncation, so a table that outgrows this access pattern fails loudly instead of quietly returning partial data. Configure with WithMaxAllRows.

func (*Repository[T]) Count

func (r *Repository[T]) Count(ctx context.Context) (int, error)

func (*Repository[T]) Create

func (r *Repository[T]) Create(ctx context.Context, entity *T, options ...OperationOption) error

func (*Repository[T]) DB

func (r *Repository[T]) DB(ctx context.Context) bun.IDB

DB exposes the current Bun executor. It is a bun.Tx when ctx carries a Soro transaction.

func (*Repository[T]) Delete

func (r *Repository[T]) Delete(ctx context.Context, id uuid.UUID, options ...OperationOption) error

func (*Repository[T]) DeleteScoped

func (r *Repository[T]) DeleteScoped(ctx context.Context, id uuid.UUID, scope QueryScope, options ...OperationOption) error

DeleteScoped soft-deletes only when id remains inside scope. The scope is applied to both the read and final mutation.

func (*Repository[T]) Exists

func (r *Repository[T]) Exists(ctx context.Context, id uuid.UUID) (bool, error)

func (*Repository[T]) Find

func (r *Repository[T]) Find(ctx context.Context, id uuid.UUID) (*T, error)

func (*Repository[T]) FindBy

func (r *Repository[T]) FindBy(ctx context.Context, field string, value any) (*T, error)

func (*Repository[T]) FindScoped

func (r *Repository[T]) FindScoped(ctx context.Context, id uuid.UUID, scope QueryScope) (*T, error)

FindScoped loads an object by ID through an explicit caller-owned scope.

func (*Repository[T]) First

func (r *Repository[T]) First(ctx context.Context) (*T, error)

func (*Repository[T]) ForceDelete

func (r *Repository[T]) ForceDelete(ctx context.Context, id uuid.UUID, options ...OperationOption) error

func (*Repository[T]) LockScoped

func (r *Repository[T]) LockScoped(ctx context.Context, id uuid.UUID, scope QueryScope) (*T, error)

LockScoped loads an object by ID through an explicit scope and locks the row for the remainder of the current transaction.

func (*Repository[T]) OnlyDeleted

func (r *Repository[T]) OnlyDeleted() *Repository[T]

func (*Repository[T]) Query

func (r *Repository[T]) Query(ctx context.Context) *bun.SelectQuery

Query returns a Bun query with the repository's deletion scope applied. That scope concerns soft deletion only and carries no tenant predicate: callers serving a request must add their own ownership constraint, or rely on RLS. The same holds for All, Count, First, Exists, and FindBy.

func (*Repository[T]) Restore

func (r *Repository[T]) Restore(ctx context.Context, id uuid.UUID, options ...OperationOption) error

func (*Repository[T]) Save

func (r *Repository[T]) Save(ctx context.Context, entity *T, options ...OperationOption) error

func (*Repository[T]) Transaction

func (r *Repository[T]) Transaction(ctx context.Context, fn func(context.Context, *Repository[T]) error) error

func (*Repository[T]) Update

func (r *Repository[T]) Update(ctx context.Context, entity *T, options ...OperationOption) error

func (*Repository[T]) UpdateScoped

func (r *Repository[T]) UpdateScoped(ctx context.Context, entity *T, scope QueryScope, options ...OperationOption) error

UpdateScoped persists an entity only when its ID remains inside scope. The scoped predicate is applied both to the persisted-state read and final SQL.

func (*Repository[T]) WithDeleted

func (r *Repository[T]) WithDeleted() *Repository[T]

type UUIDGenerator

type UUIDGenerator func() (uuid.UUID, error)

Jump to

Keyboard shortcuts

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