orm

package
v0.43.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: AGPL-3.0 Imports: 15 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 CreatedAt added in v0.11.0

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

CreatedAt provides only the created timestamp for immutable models. Embed this when you need only created_at without updated_at (e.g., audit logs, event sourcing).

type DeletedAt added in v0.12.0

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

DeletedAt represents a soft delete trait using the "deleted_at" column (Laravel-compatible). Use this when your schema follows the Laravel Eloquent convention.

func (*DeletedAt) DeletedAtColumn deprecated added in v0.12.0

func (sd *DeletedAt) DeletedAtColumn() string

DeletedAtColumn returns the soft delete column name used in database queries.

Deprecated: Use SoftDeletedAtColumn() instead.

func (*DeletedAt) SoftDeletedAtColumn added in v0.12.0

func (sd *DeletedAt) SoftDeletedAtColumn() string

SoftDeletedAtColumn returns the soft delete column name used in database queries. Implements the SoftDeleteColumnNamer interface.

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 contractsdb.Driver, 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 the database connection for this Orm instance only.

Only the connection matching r.connection is closed and cleaned up. This prevents a secondary Database (created via Connection()) from closing connections belonging to other Database instances that share the same dbConnections map.

For array-driver connections, Array.Cleanup() is called to remove populated/locks sync.Map entries — preventing unbounded memory growth in long-running services.

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) DisableDebug added in v0.10.0

func (r *Orm) DisableDebug()

DisableDebug disables debug mode at runtime for all queries.

func (*Orm) DisableQueryLog

func (r *Orm) DisableQueryLog()

func (*Orm) EnableDebug added in v0.10.0

func (r *Orm) EnableDebug()

EnableDebug enables debug mode at runtime for all queries.

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) IsDebug added in v0.10.0

func (r *Orm) IsDebug() bool

IsDebug returns true if debug mode is enabled.

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 ShortID added in v0.11.0

type ShortID struct {
	ID string `json:"id" db:"id"`
}

ShortID provides a short string primary key field. Embed it in your model to opt into client-generated short IDs. No timestamps, no soft deletes — just the ID.

type SoftDeletes

type SoftDeletes struct {
	SoftDeletedAt sql.NullTime `json:"soft_deleted_at,omitempty" db:"soft_deleted_at"`
}

SoftDeletes represents a soft delete trait using the "soft_deleted_at" column. This is the default — use DeletedAt for Laravel-compatible "deleted_at" column.

func (*SoftDeletes) DeletedAtColumn deprecated added in v0.12.0

func (sd *SoftDeletes) DeletedAtColumn() string

DeletedAtColumn returns the soft delete column name used in database queries.

Deprecated: Use SoftDeletedAtColumn() instead.

func (*SoftDeletes) SoftDeletedAtColumn added in v0.12.0

func (sd *SoftDeletes) SoftDeletedAtColumn() string

SoftDeletedAtColumn returns the soft delete column name used in database queries. Implements the SoftDeleteColumnNamer interface.

type Timestamps

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

Timestamps represents both created and updated timestamp fields. This is a convenience embed combining CreatedAt and UpdatedAt.

type UpdatedAt added in v0.11.0

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

UpdatedAt provides only the updated timestamp. Embed this when you need only updated_at without created_at.

Jump to

Keyboard shortcuts

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