store

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package store is the store tier: the persistence layer a repository is built on. A store issues one entity's statements on its ORM's client — which ORM is the store's Kind (Ent, GORM, …), one subpackage per kind under stores/ — and the repository above it adds caching and resilience.

Tier orders the store tier's layers, switched and tuned per store by the decoration config's stores tier (decoration.stores, built by decorate.Decorations; unobserved by default, so a request is traced once, at its workflow and clients). Base is the embeddable core of a store, built from its contract: its statements run through the tier on the request transaction's client when a transaction manager bound one with WithClient, else on the store's own. An Entity (funcs.go) is a Base that also serves an entity's roles from one statement per role.

Index

Constants

View Source
const (
	// KindEnt builds stores on Ent (stores/ent: the list-query compiler and folds).
	KindEnt = "ent"

	// KindGORM builds stores on GORM (stores/gorm: the lifecycle-managed client).
	KindGORM = "gorm"
)

Variables

This section is empty.

Functions

func ClientFrom

func ClientFrom[C any](ctx context.Context) (C, bool)

ClientFrom returns the client of type C bound to ctx, and whether one is.

func Tier

func Tier() decorate.Tier

Tier is the store decoration tier, outermost first: each statement gets its span, metrics and log line, then its deadline. Retry and the breaker belong to the repository above, which knows whether the statement runs in a transaction.

func WithClient

func WithClient[C any](ctx context.Context, c C) context.Context

WithClient returns ctx carrying c as the transaction-bound client of type C.

Types

type Base

type Base[C any] struct {

	// Subject runs the store's operations through the tier; it supplies Name and Exec.
	decorate.Subject
	// contains filtered or unexported fields
}

Base is the embeddable core of a store named by its Subject over a client of type C (an *ent.Client, a *gorm.DB): Name and Exec run a read through the store tier, Write runs a write the same way but only inside a transaction, and Client returns the client it runs on. Each operation is a method of the store's contract, decorated under its own name. The zero Base runs statements undecorated on the zero client.

func NewBase

func NewBase[S, C any](decs decorate.Decorations, name string, raw C) (Base[C], error)

NewBase builds the Base for the store named name over raw, the client used outside a transaction, whose operations are the methods of its contract S (an interface): the store is decorated from decs as the store-tier component name, and each operation op as name+"."+op. A contract that is not an interface, or a decoration that fails to resolve, is an error.

func (Base[C]) Client

func (b Base[C]) Client(ctx context.Context) C

Client returns the client a statement on ctx runs on: the transaction-bound one, if any, else the store's own.

func (Base[C]) Write added in v0.0.10

func (b Base[C]) Write[R any](
	ctx context.Context,
	op string,
	fn func(context.Context) (R, error),
) (R, error)

Write runs fn as the write op through the store tier, only inside a transaction: on a context with no transaction it fails INTERNAL without running fn, so a write never autocommits outside the workflow that owns it. Reads run through Exec.

type Entity added in v0.0.16

type Entity[C, T, P any, ID comparable] struct {
	// Entity serves the roles.
	decorate.Entity[T, P, ID]

	// Base runs the store's statements and supplies Name, Exec, Write and Client.
	Base[C]
}

Entity is the store-tier entity base: a Base over a client of type C that also serves the entity's roles from its statements. Reads run through Exec, and writes through Write, so a write runs only inside a transaction. A store embeds it and adds its product queries through Exec and Write.

func NewEntity added in v0.0.16

func NewEntity[S, C, T, P any, ID comparable](
	decs decorate.Decorations,
	name string,
	raw C,
	f Funcs[C, T, P, ID],
) (Entity[C, T, P, ID], error)

NewEntity builds the Entity of the store named name over raw, whose contract is S (an interface), serving the roles S has from f: decorated as NewBase decorates. A role S names with no statement in f is CodeInternal.

type Funcs added in v0.0.11

type Funcs[C, T, P any, ID comparable] struct {
	// Get reads the entity identified by id.
	Get func(ctx context.Context, c C, id ID) (*T, error)

	// List reads the page of entities params and page select.
	List func(
		ctx context.Context,
		c C,
		params P,
		page types.PageRequest,
	) (*types.Page[T], error)

	// Create writes a new entity.
	Create func(ctx context.Context, c C, entity *T) (*T, error)

	// Update writes entity to the entity identified by id.
	Update func(ctx context.Context, c C, id ID, entity *T) (*T, error)

	// Delete removes the entity identified by id.
	Delete func(ctx context.Context, c C, id ID) error

	// Exists reports whether the entity identified by id exists.
	Exists func(ctx context.Context, c C, id ID) (bool, error)

	// Insert appends a new entity.
	Insert func(ctx context.Context, c C, entity *T) error

	// Upsert writes entity, inserting it or replacing the one with its key.
	Upsert func(ctx context.Context, c C, entity *T) error

	// PurgeBefore removes the entities older than cutoff.
	PurgeBefore func(ctx context.Context, c C, cutoff time.Time) (int, error)
}

Funcs are an entity store's statements, each run on the client its context's statement runs on (the transaction's, else the store's own). A nil statement is a role the store's contract does not have.

Jump to

Keyboard shortcuts

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