Documentation
¶
Index ¶
- Variables
- func ExecInTx(ctx context.Context, factory TxFactory, fn func(ctx context.Context) error) (err error)
- func WithTxContext(parent context.Context, driver TxDriver, tx Transaction) context.Context
- type NoopStorable
- type ReadRepository
- type Storable
- type Transaction
- type TxDriver
- type TxFactory
- type TxManager
- type WriteBatchRepository
- type WriteRepository
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidTxContext is returned when a transaction context is invalid or does not contain a transaction. ErrInvalidTxContext = errors.New("geck.persistence: invalid transaction context") )
Functions ¶
func ExecInTx ¶
func ExecInTx(ctx context.Context, factory TxFactory, fn func(ctx context.Context) error) (err error)
ExecInTx executes the provided function `fn` within the context of a transaction. It automatically handles transaction commit and rollback based on the function's execution result. If `fn` panics, it will recover and rollback the transaction, returning the panic as an error. The function `fn` receives a context that has the transaction set, allowing it to perform database operations within the transaction scope.
func WithTxContext ¶
WithTxContext creates a new context with the given Transaction.
Types ¶
type NoopStorable ¶
type NoopStorable struct {
IsNewValue bool
}
NoopStorable is a no-op implementation of the Storable interface.
func (NoopStorable) IsNew ¶
func (n NoopStorable) IsNew() bool
IsNew checks if the type was just created.
type ReadRepository ¶
type ReadRepository[K comparable, T any] interface { FindByKey(ctx context.Context, key K) (*T, error) }
ReadRepository component used to retrieve T instances from a storage system.
type Storable ¶
type Storable interface {
// IsNew checks if the type was just created.
IsNew() bool
}
Storable is an interface indicating the implementing type can be stored into a persistence system.
type Transaction ¶
type Transaction interface {
// Commit commits the transaction.
Commit(ctx context.Context) error
// Rollback rolls back the transaction.
Rollback(ctx context.Context) error
}
Transaction is a database transaction interface that provides methods for managing transaction lifecycle. Implementations should handle the underlying database-specific transaction operations while maintaining consistent behavior. Use with WithTxContext and FromTxContext to propagate transactions through context.
func FromTxContext ¶
func FromTxContext(ctx context.Context, driver TxDriver) (Transaction, bool)
FromTxContext retrieves the Transaction from the given context.
type TxDriver ¶
type TxDriver string
TxDriver is a type that represents the driver of a transaction.
It is used as a key in the context to store and retrieve the Transaction instance.
type TxFactory ¶
type TxFactory interface {
// Driver returns the [TxDriver] associated with this factory.
Driver() TxDriver
// NewTx creates a new [Transaction] instance.
NewTx(ctx context.Context) (Transaction, error)
}
TxFactory is a component responsible for the creation of persistence transactions.
type TxManager ¶
type TxManager struct {
// contains filtered or unexported fields
}
TxManager is a manager for transaction factories that allows registering multiple transaction factories and executing functions within the context of all registered transactions. It provides a way to coordinate multiple transactions as a single logical transaction.
It is useful for scenarios where multiple databases or transaction sources need to be coordinated together, ensuring that all operations across these transactions are either committed or rolled back as a single unit of work.
func NewTxManager ¶
func NewTxManager() *TxManager
NewTxManager creates a new instance of TxManager.
func (*TxManager) Execute ¶
Execute executes the provided function `fn` within the context of transactions from all registered factories. It creates transactions from all registered factories and coordinates them as a single logical transaction.
If any transaction fails during commit, all transactions are rolled back to maintain consistency. The function `fn` receives a context that has all transactions set, allowing it to perform database operations across multiple transaction contexts.
func (*TxManager) GetFactories ¶
GetFactories returns a slice of all registered transaction factories.
type WriteBatchRepository ¶
type WriteBatchRepository[K comparable, T Storable] interface { SaveAll(ctx context.Context, entities []T) error DeleteAll(ctx context.Context, entities []T) error DeleteAllByKeys(ctx context.Context, keys []K) error }
WriteBatchRepository component used to write several T instances into a storage system.