orm

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: AGPL-3.0 Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	EventCreating  = "model.creating"
	EventCreated   = "model.created"
	EventUpdating  = "model.updating"
	EventUpdated   = "model.updated"
	EventSaving    = "model.saving"
	EventSaved     = "model.saved"
	EventDeleting  = "model.deleting"
	EventDeleted   = "model.deleted"
	EventRestoring = "model.restoring"
	EventRestored  = "model.restored"
)

Event names for model lifecycle events

Variables

This section is empty.

Functions

This section is empty.

Types

type EventBus

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

EventBus is a lightweight internal event bus for model lifecycle events.

func NewEventBus

func NewEventBus() *EventBus

NewEventBus creates a new event bus.

func (*EventBus) Dispatch

func (e *EventBus) Dispatch(eventName string, event any)

Dispatch dispatches an event to all registered listeners.

func (*EventBus) Forget

func (e *EventBus) Forget(eventName string)

Forget removes all listeners for an event.

func (*EventBus) Listen

func (e *EventBus) Listen(eventName string, handler EventHandler)

Listen registers a handler for an event name.

type EventHandler

type EventHandler func(event any)

EventHandler is a function that handles an event.

type Factory

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

Factory implements the Factory interface for creating test data.

func NewFactory

func NewFactory(orm *Orm) *Factory

NewFactory creates a new Factory instance.

func (*Factory) Count

func (f *Factory) Count(count int) contractsorm.Factory

Count sets the number of models that should be generated.

func (*Factory) Create

func (f *Factory) Create(value any, attributes ...map[string]any) (any, error)

Create creates a model and persists it to the database, returning the created instance(s).

func (*Factory) CreateQuietly

func (f *Factory) CreateQuietly(value any, attributes ...map[string]any) (any, error)

CreateQuietly creates a model and persists it to the database without firing any model events, returning the created instance(s).

func (*Factory) Make

func (f *Factory) Make(value any, attributes ...map[string]any) (any, error)

Make creates a model and returns it, but does not persist it to the database.

func (*Factory) Table added in v0.3.0

func (f *Factory) Table(table string) contractsorm.Factory

Table sets the table name for database operations.

type Model

type Model struct {
	ID uint `json:"id"`
	Timestamps
}

Model represents a base model with ID and timestamps.

type Orm

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

Orm represents the ORM instance for database operations.

func BuildOrm

func BuildOrm(ctx context.Context, dbConfig *db.DBConfig, connection string, log log.Log, refresh func(), opts ...OrmOption) (*Orm, error)

BuildOrm builds and initializes a new Orm instance with the given configuration.

func BuildOrmFromDB added in v0.9.0

func BuildOrmFromDB(ctx context.Context, sqlDB *sql.DB, driverName string, connection string, dbConfig *db.DBConfig, log log.Log, refresh func()) (*Orm, error)

BuildOrmFromDB builds an Orm instance from an already-open *sql.DB. The caller retains ownership of sqlDB; connection pool settings are not modified.

func NewOrm

func NewOrm(
	ctx context.Context,
	dbConfig *db.DBConfig,
	connection string,
	query contractsorm.Query,
	queries map[string]contractsorm.Query,
	log log.Log,
	modelToObserver []contractsorm.ModelToObserver,
	refresh func(),
	drivers map[string]driver.Driver,
	dbConnections map[string]*sql.DB,
) *Orm

NewOrm creates a new Orm instance.

func (*Orm) Close

func (r *Orm) Close() error

Close closes all database connections.

func (*Orm) Connection

func (r *Orm) Connection(name string) contractsorm.Orm

func (*Orm) DB

func (r *Orm) DB() (*sql.DB, error)

func (*Orm) DatabaseName

func (r *Orm) DatabaseName() string

func (*Orm) DisableQueryLog

func (r *Orm) DisableQueryLog()

func (*Orm) EnableQueryLog

func (r *Orm) EnableQueryLog()

func (*Orm) Factory

func (r *Orm) Factory() contractsorm.Factory

func (*Orm) FlushQueryLog

func (r *Orm) FlushQueryLog()

func (*Orm) GetQueryLog

func (r *Orm) GetQueryLog() []contractsorm.QueryLog

func (*Orm) Name

func (r *Orm) Name() string

func (*Orm) Observe

func (r *Orm) Observe(model any, observer contractsorm.Observer)

func (*Orm) Query

func (r *Orm) Query() contractsorm.Query

func (*Orm) Refresh

func (r *Orm) Refresh()

func (*Orm) SetQuery

func (r *Orm) SetQuery(query contractsorm.Query)

func (*Orm) Transaction

func (r *Orm) Transaction(txFunc func(tx contractsorm.Query) error, opts ...*sql.TxOptions) error

Transaction runs a callback wrapped in a database transaction. It automatically commits the transaction if the callback returns nil. If the callback returns an error or a panic occurs, the transaction is rolled back.

func (*Orm) WithContext

func (r *Orm) WithContext(ctx context.Context) contractsorm.Orm

type OrmOption added in v0.5.0

type OrmOption func(*ormOptions)

OrmOption is a function that configures Orm options.

func WithSkipPing added in v0.5.0

func WithSkipPing(skip bool) OrmOption

WithSkipPing sets whether to skip the database ping during initialization.

type SoftDeletes

type SoftDeletes struct {
	DeletedAt sql.NullTime `json:"deleted_at,omitempty"`
}

SoftDeletes represents a soft delete trait.

type Timestamps

type Timestamps struct {
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

Timestamps represents created and updated timestamp fields.

Jump to

Keyboard shortcuts

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