orm

package
v0.10.3 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package orm provides an ActiveRecord-style Model, a fluent query builder, hooks, casts, soft-deletes, and relationships. Models are plain Go structs that embed orm.Model (or one of the timestamp variants) and live without a global router — the only required dependency is a *database.Connection.

Example:

type User struct {
    orm.Model
    Name  string
    Email string `orm:"unique"`
}

user := &User{Name: "Ada", Email: "ada@example.com"}
if err := orm.Save(ctx, conn, user); err != nil { ... }

var users []User
err := orm.Query[User](conn).Where("email", "like", "%@example.com").Get(ctx, &users)

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("orm: record not found")

ErrNotFound is returned by First/FirstOrFail when no row matches.

Functions

func Delete

func Delete[T any](ctx context.Context, conn *database.Connection, model *T) error

Delete deletes the model — soft-delete when the schema supports it, hard delete otherwise.

func ForceDelete

func ForceDelete[T any](ctx context.Context, conn *database.Connection, model *T) error

ForceDelete bypasses soft-delete and removes the row.

func Pluck

func Pluck[T any, V any](ctx context.Context, b *Builder[T], col string) ([]V, error)

Pluck extracts a single column as []V.

func Restore

func Restore[T any](ctx context.Context, conn *database.Connection, model *T) error

Restore reverts a soft-delete.

func Save

func Save[T any](ctx context.Context, conn *database.Connection, model *T) error

Save persists a model: it inserts when the primary key is zero, otherwise updates. Hooks are dispatched around the operation. Timestamps are set automatically.

Types

type AfterCreateHook

type AfterCreateHook interface{ AfterCreate(*HookContext) error }

BeforeCreate / AfterCreate / etc. are optional hook interfaces.

type AfterDeleteHook

type AfterDeleteHook interface{ AfterDelete(*HookContext) error }

BeforeCreate / AfterCreate / etc. are optional hook interfaces.

type AfterFindHook

type AfterFindHook interface{ AfterFind(*HookContext) error }

BeforeCreate / AfterCreate / etc. are optional hook interfaces.

type AfterSaveHook

type AfterSaveHook interface{ AfterSave(*HookContext) error }

BeforeCreate / AfterCreate / etc. are optional hook interfaces.

type AfterUpdateHook

type AfterUpdateHook interface{ AfterUpdate(*HookContext) error }

BeforeCreate / AfterCreate / etc. are optional hook interfaces.

type BeforeCreateHook

type BeforeCreateHook interface{ BeforeCreate(*HookContext) error }

BeforeCreate / AfterCreate / etc. are optional hook interfaces.

type BeforeDeleteHook

type BeforeDeleteHook interface{ BeforeDelete(*HookContext) error }

BeforeCreate / AfterCreate / etc. are optional hook interfaces.

type BeforeSaveHook

type BeforeSaveHook interface{ BeforeSave(*HookContext) error }

BeforeCreate / AfterCreate / etc. are optional hook interfaces.

type BeforeUpdateHook

type BeforeUpdateHook interface{ BeforeUpdate(*HookContext) error }

BeforeCreate / AfterCreate / etc. are optional hook interfaces.

type Builder

type Builder[T any] struct {
	// contains filtered or unexported fields
}

Builder is a model-aware wrapper around query.Builder.

func Query

func Query[T any](conn *database.Connection) *Builder[T]

Query returns a fresh model builder.

func (*Builder[T]) Count

func (b *Builder[T]) Count(ctx context.Context) (int64, error)

Count rows matching the query.

func (*Builder[T]) Exists

func (b *Builder[T]) Exists(ctx context.Context) (bool, error)

Exists reports whether any matching row exists.

func (*Builder[T]) Find

func (b *Builder[T]) Find(ctx context.Context, id any) (*T, error)

Find by primary key.

func (*Builder[T]) First

func (b *Builder[T]) First(ctx context.Context) (*T, error)

First returns the first matching row, or ErrNotFound.

func (*Builder[T]) Get

func (b *Builder[T]) Get(ctx context.Context, dst *[]T) error

Get executes the query and populates dst (must be *[]T).

func (*Builder[T]) Limit

func (b *Builder[T]) Limit(n int) *Builder[T]

Limit applies a LIMIT.

func (*Builder[T]) Offset

func (b *Builder[T]) Offset(n int) *Builder[T]

Offset applies an OFFSET.

func (*Builder[T]) OnlyTrashed

func (b *Builder[T]) OnlyTrashed() *Builder[T]

OnlyTrashed restricts to soft-deleted rows.

func (*Builder[T]) OrWhere

func (b *Builder[T]) OrWhere(args ...any) *Builder[T]

OrWhere appends an OR WHERE.

func (*Builder[T]) OrderBy

func (b *Builder[T]) OrderBy(col, dir string) *Builder[T]

OrderBy appends ORDER BY.

func (*Builder[T]) QB

func (b *Builder[T]) QB() *query.Builder

Raw query builder access.

func (*Builder[T]) Where

func (b *Builder[T]) Where(args ...any) *Builder[T]

Where appends a WHERE clause.

func (*Builder[T]) WhereIn

func (b *Builder[T]) WhereIn(col string, values any) *Builder[T]

WhereIn appends an IN constraint.

func (*Builder[T]) WhereNotNull

func (b *Builder[T]) WhereNotNull(col string) *Builder[T]

WhereNotNull appends IS NOT NULL.

func (*Builder[T]) WhereNull

func (b *Builder[T]) WhereNull(col string) *Builder[T]

WhereNull appends an IS NULL constraint.

func (*Builder[T]) WithTrashed

func (b *Builder[T]) WithTrashed() *Builder[T]

WithTrashed includes soft-deleted rows in the result.

func (*Builder[T]) WithTx

func (b *Builder[T]) WithTx(tx *database.Tx) *Builder[T]

WithTx pins the builder to a transaction.

type Connectioner

type Connectioner interface {
	ConnectionName() string
}

Connectioner may be implemented on a model to pin it to a specific named connection from the database.Manager. Useful in multi-tenant or multi-database setups.

type Fillable

type Fillable interface {
	FillableColumns() []string
}

Fillable may be implemented to whitelist columns for mass assignment.

type Hidden

type Hidden interface {
	HiddenColumns() []string
}

Hidden may be implemented to hide columns from ToMap serialization.

type HookContext

type HookContext struct {
	Ctx  context.Context
	Conn *database.Connection
	Tx   *database.Tx
}

HookContext is passed to lifecycle hooks. It exposes the active transaction-or-connection executor and the underlying context.Context.

func (*HookContext) Executor

func (h *HookContext) Executor() database.Executor

Executor returns the active executor, preferring the transaction if open.

type Model

type Model struct {
	ID        uint64       `column:"id" orm:"primary;autoincrement"`
	CreatedAt time.Time    `column:"created_at"`
	UpdatedAt time.Time    `column:"updated_at"`
	DeletedAt sql.NullTime `column:"deleted_at" orm:"nullable"`
}

Model is the base struct most models embed. It provides the standard id/created_at/updated_at/deleted_at columns. Models that do not need soft deletes can embed Timestamps instead.

type Tabler

type Tabler interface {
	TableName() string
}

Tabler may be implemented on a model to override the inferred table name.

type Timestamps

type Timestamps struct {
	ID        uint64    `column:"id" orm:"primary;autoincrement"`
	CreatedAt time.Time `column:"created_at"`
	UpdatedAt time.Time `column:"updated_at"`
}

Timestamps is a lighter base for models that do not need soft deletes.

Jump to

Keyboard shortcuts

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