Help us shape the future of Go! Take the 2026 Go Developer Survey

orm

package
v1.20.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetID

func GetID(dest any) any

func GetIDByReflect

func GetIDByReflect(t reflect.Type, v reflect.Value) any

func MorphAlias

func MorphAlias(model any) (string, bool)

MorphAlias returns the alias registered for the given model's underlying type. Useful at insert time when writing the value of a `*_type` column from a Go value.

func MorphMap

func MorphMap(entries map[string]any, merge ...bool)

MorphMap registers polymorphic aliases. Each entry maps an alias (the value stored in a `*_type` column) to a sample model instance from which the registry derives the underlying Go type.

Subsequent calls merge with previously registered entries; later writes win on conflict. Pass false in the optional merge argument to replace the registry instead of merging.

orm.MorphMap(map[string]any{
    "post":  &Post{},
    "video": &Video{},
})

Models that implement orm.ModelWithMorphClass take precedence over the registry.

func MorphedModel

func MorphedModel(alias string) any

MorphedModel returns a fresh pointer to a new instance of the model registered under alias, or nil if no model is registered. Used by the MorphTo loader to allocate the right Go type for a row whose `*_type` column contains alias.

Types

type DeletedAt

type DeletedAt struct {
	Time  time.Time
	Valid bool
}

DeletedAt records when a row was soft deleted. It is the type orm.SoftDeletes carries.

It is laid out exactly like database/sql.NullTime — Valid reports whether the row is soft deleted, and Time holds the moment it happened — so scanning and binding behave the same as the GORM type it replaces, and it can be converted to sql.NullTime rather than reimplementing the scanner.

The clause interfaces gorm.DeletedAt also implemented are deliberately absent. They were how GORM learned to filter soft-deleted rows and to turn a delete into an update; the framework now builds that predicate itself, so this type only has to carry the value.

func (DeletedAt) IsZero

func (r DeletedAt) IsZero() bool

IsZero reports whether no deletion time is recorded, which is how a soft-delete field is recognised as carrying a value.

func (DeletedAt) MarshalJSON

func (r DeletedAt) MarshalJSON() ([]byte, error)

MarshalJSON renders the deletion time, or null when the row was not soft deleted.

func (*DeletedAt) Scan

func (r *DeletedAt) Scan(value any) error

Scan implements sql.Scanner.

func (*DeletedAt) UnmarshalJSON

func (r *DeletedAt) UnmarshalJSON(data []byte) error

UnmarshalJSON restores the deletion time, reading null as "not soft deleted".

func (DeletedAt) Value

func (r DeletedAt) Value() (driver.Value, error)

Value implements driver.Valuer. A row that was not soft deleted stores NULL.

type Model

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

Model is the base model for all models in the application.

type SoftDeletes

type SoftDeletes struct {
	DeletedAt DeletedAt `gorm:"column:deleted_at" json:"deleted_at"`
}

SoftDeletes is used to add soft delete functionality to a model.

The field carries the framework's own type, so the ORM builds the soft-delete predicate and the delete rewrite itself instead of asking GORM to.

type Timestamps

type Timestamps struct {
	CreatedAt *carbon.DateTime `gorm:"autoCreateTime;column:created_at" json:"created_at"`
	UpdatedAt *carbon.DateTime `gorm:"autoUpdateTime;column:updated_at" json:"updated_at"`
}

Timestamps is used to add created_at and updated_at timestamps to a model.

Directories

Path Synopsis
Package morphmap holds the process-wide registry of polymorphic aliases shared by the orm and gorm packages.
Package morphmap holds the process-wide registry of polymorphic aliases shared by the orm and gorm packages.

Jump to

Keyboard shortcuts

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