gorm

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: 9 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColumnFor

func ColumnFor[T any](db *gormlib.DB, field string) (clause.Column, error)

ColumnFor resolves field to a validated GORM column for T.

func Database

func Database(ctx context.Context, db *gormlib.DB, action string) (*gormlib.DB, error)

Database validates db and binds ctx for generated GORM queries.

func EscapeLike

func EscapeLike(value string) string

EscapeLike escapes SQL LIKE wildcard characters for generated queries.

func WrapError

func WrapError(action string, err error) error

WrapError maps adapter-specific errors to public Helix data errors.

Types

type ConnectionPoolConfig

type ConnectionPoolConfig struct {
	MaxOpenConns int
	MaxIdleConns int
}

ConnectionPoolConfig holds database/sql connection-pool settings.

type DB

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

DB wraps a GORM database connection, hiding gorm.io types from other packages.

func OpenSQLite

func OpenSQLite(dsn string) (*DB, error)

OpenSQLite opens a SQLite database at dsn using GORM with TranslateError enabled.

func (*DB) AutoMigrate

func (d *DB) AutoMigrate(models ...any) error

AutoMigrate runs GORM AutoMigrate for the provided models.

func (*DB) Close

func (d *DB) Close() error

Close releases all database resources.

func (*DB) Components

func (d *DB) Components() []any

Components returns the components to register in the DI container: the internal *gorm.DB, *TransactionManager, and *DB wrapper.

func (*DB) ConfigurePool

func (d *DB) ConfigurePool(cfg ConnectionPoolConfig) error

ConfigurePool applies database/sql pool settings. Zero values are treated as no-op; negative values return an error.

func (*DB) HasTable

func (d *DB) HasTable(model any) bool

HasTable reports whether the database has a table for model.

func (*DB) Ping

func (d *DB) Ping() error

Ping verifies the database connection is alive.

type Repository

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

Repository implements data.Repository using a GORM database handle.

func NewRepository

func NewRepository[T, ID any](db *gormlib.DB) *Repository[T, ID]

NewRepository creates a GORM-backed repository. If db is nil, the returned repository is invalid: every method call will return an error wrapping errInvalidDB without panicking.

func (*Repository[T, ID]) Delete

func (r *Repository[T, ID]) Delete(ctx context.Context, id ID) error

Delete removes the record matching id.

func (*Repository[T, ID]) FindAll

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

FindAll returns all records for T.

func (*Repository[T, ID]) FindByID

func (r *Repository[T, ID]) FindByID(ctx context.Context, id ID) (*T, error)

FindByID returns the record matching id.

func (*Repository[T, ID]) FindWhere

func (r *Repository[T, ID]) FindWhere(ctx context.Context, filter data.Filter) ([]T, error)

FindWhere returns records matching filter.

func (*Repository[T, ID]) Paginate

func (r *Repository[T, ID]) Paginate(ctx context.Context, page, size int) (data.Page[T], error)

Paginate returns one page of records and the total count.

func (*Repository[T, ID]) Save

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

Save creates or updates entity using GORM's traditional upsert semantics.

func (*Repository[T, ID]) WithTransaction

func (r *Repository[T, ID]) WithTransaction(tx data.Transaction[*gormlib.DB]) data.Repository[T, ID, *gormlib.DB]

WithTransaction returns a new Repository bound to tx.

type Transaction

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

Transaction wraps a GORM transaction handle for data.Repository.WithTransaction.

func NewTransaction

func NewTransaction(db *gormlib.DB) Transaction

NewTransaction wraps db as a data.Transaction-compatible GORM transaction.

func (Transaction) Unwrap

func (t Transaction) Unwrap() *gormlib.DB

Unwrap returns the wrapped GORM transaction handle.

type TransactionManager

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

TransactionManager executes callbacks inside GORM transactions.

func NewTransactionManager

func NewTransactionManager(db *gormlib.DB) *TransactionManager

NewTransactionManager creates a transaction manager backed by db. A nil db is reported when WithinTransaction is called.

func (*TransactionManager) WithinTransaction

func (m *TransactionManager) WithinTransaction(ctx context.Context, fn func(context.Context, data.Transaction[*gormlib.DB]) error) error

WithinTransaction runs fn inside a GORM transaction. If ctx already carries a GORM transaction, fn joins it instead of creating a nested GORM transaction or savepoint.

Jump to

Keyboard shortcuts

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