orm

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

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) error

Create creates a model and persists it to the database.

func (*Factory) CreateQuietly

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

CreateQuietly creates a model and persists it to the database without firing any model events.

func (*Factory) Make

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

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

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
}

func BuildOrm

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

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

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 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