orm

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package orm integrates GORM with Scene's data-source, logging, setup, and dependency-injection lifecycle.

Boundary

Package orm is intentionally not a repository framework and does not define a database-independent query language. A module's repository interface is the persistence abstraction. Its GORM adapter uses this package to obtain a native *gorm.DB and is free to use GORM scopes, clauses, associations, or parameterized SQL when those best express the query.

A repository injects the concrete component and creates a context-bound session for each operation:

type userRepository struct {
	db *orm.Gorm `aperture:""`
}

func (r *userRepository) UserByID(
	ctx context.Context,
	id string,
) (userRow, error) {
	var row userRow
	err := r.db.Session(ctx).
		Where(&userRow{UserID: id}).
		First(&row).Error
	return row, err
}

Transactions

Transaction supplies one native transaction handle. Use that handle for every participating model so the transaction is not tied to a generic repository or a single row type:

err := db.Transaction(ctx, func(tx *gorm.DB) error {
	if err := tx.Create(&user).Error; err != nil {
		return err
	}
	return tx.Create(&token).Error
})

Schema setup

AutoMigrate is a concrete GORM startup capability, not a framework port. GORM-backed repositories may call it from Setup:

func (r *userRepository) Setup() error {
	return r.db.AutoMigrate(&userRow{})
}

Persistence rows, GORM tags, table names, and domain conversion helpers belong in the concrete repository package. Domain models should not import GORM or carry GORM-only metadata.

Query guidance

Prefer typed module repository methods and module-specific query values over exposing column names to services or delivery code. Simple equality queries can use struct or map conditions. Complex reads may use native GORM or parameterized SQL, but must remain inside the concrete repository adapter and return domain or read-model values.

Index

Constants

View Source
const Lens scene.CompositionName = "orm"

Variables

This section is empty.

Functions

This section is empty.

Types

type Gorm

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

Gorm owns the configured GORM database handle.

Gorm is deliberately a concrete type. Database substitutability belongs at the module repository boundary; repositories using GORM should depend on *Gorm and use Session to build native GORM queries.

func NewGorm added in v0.3.7

func NewGorm(dialector func() gorm.Dialector, ds datasource.DataSource) *Gorm

NewGorm creates a GORM component. The dialector factory is evaluated during Setup, after the injected data source has completed its own setup.

func NewGormWithMySQL added in v0.3.7

func NewGormWithMySQL(ds datasource.MysqlDataSource) *Gorm

NewGormWithMySQL creates a GORM component backed by a MySQL data source.

func NewGormWithPostgreSQL added in v0.3.7

func NewGormWithPostgreSQL(ds datasource.PostgresDataSource) *Gorm

NewGormWithPostgreSQL creates a GORM component backed by a PostgreSQL data source.

func NewGormWithSQLite added in v0.3.7

func NewGormWithSQLite(ds datasource.SqliteDataSource) *Gorm

NewGormWithSQLite creates a GORM component backed by a SQLite data source.

func (*Gorm) AutoMigrate added in v0.3.7

func (g *Gorm) AutoMigrate(models ...any) error

AutoMigrate applies GORM's model migration.

This is a concrete startup capability rather than a separate framework interface. GORM repository adapters normally call it from Setup.

func (*Gorm) ImplName added in v0.3.7

func (g *Gorm) ImplName() scene.ImplName

func (*Gorm) Session added in v0.3.7

func (g *Gorm) Session(ctx context.Context) *gorm.DB

Session returns a native GORM session carrying ctx.

Callers should obtain a new session for each repository operation instead of retaining the returned *gorm.DB.

func (*Gorm) Setup added in v0.3.7

func (g *Gorm) Setup() error

Setup opens the GORM handle over the configured data source.

func (*Gorm) Transaction added in v0.3.7

func (g *Gorm) Transaction(ctx context.Context, fn func(tx *gorm.DB) error) error

Transaction executes fn in a GORM transaction carrying ctx.

Repositories that need to update multiple models can use the supplied *gorm.DB for every operation in the transaction.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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