data

package
v0.1.0-preview.4 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package data provides standard-library-first SQL transaction contracts for generated Spice applications and application-owned repositories.

Index

Constants

This section is empty.

Variables

View Source
var ErrPanicked = errors.New("transaction callback panicked")

ErrPanicked identifies an observed transaction callback panic. Within rolls the transaction back, reports this error to observers, and re-panics with the original value.

Functions

This section is empty.

Types

type Definition

type Definition struct {
	ID        string
	Module    string
	Isolation sql.IsolationLevel
	ReadOnly  bool
}

Definition is compiler-owned metadata for one transaction boundary.

type Executor

type Executor interface {
	ExecContext(context.Context, string, ...any) (sql.Result, error)
	PrepareContext(context.Context, string) (*sql.Stmt, error)
	QueryContext(context.Context, string, ...any) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...any) *sql.Row
}

Executor is the common database/sql surface implemented by *sql.DB and *sql.Tx. Repositories can depend on this interface and remain usable inside and outside a managed transaction.

func ExecutorFromContext

func ExecutorFromContext(ctx context.Context) (Executor, bool)

ExecutorFromContext returns the transaction-owned executor installed by Manager.Within. Generated service decorators use this explicit accessor so repository implementations can participate without receiving a transaction parameter through every service method. It returns false outside a managed transaction.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager executes transaction callbacks against one caller-owned database.

func NewManager

func NewManager(db *sql.DB, observers ...Observer) (*Manager, error)

NewManager constructs a transaction manager. It makes no connection and does not mutate database pool settings.

func (*Manager) Within

func (manager *Manager) Within(
	ctx context.Context,
	definition Definition,
	work Work,
) (resultErr error)

Within begins a transaction, invokes work, and commits only when work succeeds. Callback errors and panics cause rollback. A rollback failure is joined with the callback failure; a panic is re-raised after observation.

type Observer

type Observer interface {
	BeginTransaction(context.Context, Definition) (context.Context, func(Result))
}

Observer receives transaction begin/end information on the calling goroutine. Implementations must not panic or block indefinitely.

type Result

type Result struct {
	Definition Definition
	Duration   time.Duration
	Err        error
	Panicked   bool
}

Result describes one completed transaction for an Observer. Err is the begin, callback, rollback, or commit failure. Panicked is true when Within rolled back because the callback panicked.

type Work

type Work func(context.Context, Executor) error

Work is application logic executed inside a managed transaction. The Executor deliberately omits Commit and Rollback so ownership stays with the manager.

Directories

Path Synopsis
Package repository provides bounded, typed database/sql query primitives for application-owned and generated repositories.
Package repository provides bounded, typed database/sql query primitives for application-owned and generated repositories.

Jump to

Keyboard shortcuts

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