orm

package
v0.9.16 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRecordNotFound = sql.ErrNoRows
	ErrInvalidSQL     = errors.New("invalid SQL query")
	ErrConnection     = errors.New("database connection error")
	ErrTransaction    = errors.New("transaction error")
)

Common errors

Functions

func CreateMany

func CreateMany[T any](records []T) error

func Hash

func Hash(password string) string

Hash hashes a password using bcrypt

func ResetDefault added in v0.9.14

func ResetDefault()

ResetDefault clears the default manager. Used in tests to ensure isolation.

func Save

func Save[T any](m *Manager, model *T) error

func SetDefault added in v0.9.14

func SetDefault(m *Manager)

SetDefault sets the package-level default Manager. Called by velocity.New() after constructing the manager — application code should never call this.

Types

type AfterCreateHook

type AfterCreateHook interface {
	AfterCreate() error
}

type AfterDeleteHook

type AfterDeleteHook interface {
	AfterDelete() error
}

type AfterUpdateHook

type AfterUpdateHook interface {
	AfterUpdate() error
}

type BeforeCreateHook

type BeforeCreateHook interface {
	BeforeCreate() error
}

type BeforeDeleteHook

type BeforeDeleteHook interface {
	BeforeDelete() error
}

type BeforeUpdateHook

type BeforeUpdateHook interface {
	BeforeUpdate() error
}

type Fillable added in v0.9.2

type Fillable interface {
	Fillable() []string
}

Fillable interface allows models to specify which fields can be mass-assigned

type Guarded added in v0.9.2

type Guarded interface {
	Guarded() []string
}

Guarded interface allows models to specify which fields are protected from mass-assignment

type Manager added in v0.9.5

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

Manager manages database connections. It is the instance-based alternative to the package-level global functions.

func Default added in v0.9.14

func Default() *Manager

Default returns the package-level default Manager, or nil if none is set.

func NewManager added in v0.9.5

func NewManager(config ManagerConfig) (*Manager, error)

NewManager creates a new ORM Manager with a connected database driver.

func (*Manager) AddConnection added in v0.9.5

func (m *Manager) AddConnection(name string, driver drivers.Driver)

AddConnection registers a named database connection.

func (*Manager) Begin added in v0.9.5

func (m *Manager) Begin() (*sql.Tx, error)

Begin starts a new transaction.

func (*Manager) Close added in v0.9.5

func (m *Manager) Close() error

Close closes the default database connection and all named connections.

func (*Manager) Connection added in v0.9.5

func (m *Manager) Connection(name string) (drivers.Driver, error)

Connection returns a named database connection.

func (*Manager) DB added in v0.9.5

func (m *Manager) DB() *sql.DB

DB returns the underlying *sql.DB from the default connection.

func (*Manager) DatabaseName added in v0.9.5

func (m *Manager) DatabaseName() string

DatabaseName returns the name of the current database.

func (*Manager) DefaultDriver added in v0.9.11

func (m *Manager) DefaultDriver() drivers.Driver

DefaultDriver returns the default database driver (used internally by model Save).

func (*Manager) DriverName added in v0.9.5

func (m *Manager) DriverName() string

DriverName returns the name of the default database driver.

func (*Manager) Exec added in v0.9.5

func (m *Manager) Exec(query string, args ...any) (sql.Result, error)

Exec executes a raw SQL statement.

WARNING: The caller is responsible for preventing SQL injection by using parameterized queries. Never concatenate user input into the query string.

func (*Manager) Ping added in v0.9.5

func (m *Manager) Ping() error

Ping verifies the default database connection.

func (*Manager) Raw added in v0.9.5

func (m *Manager) Raw(query string, args ...any) (*sql.Rows, error)

Raw executes a raw SQL query and returns the resulting rows.

WARNING: The caller is responsible for preventing SQL injection by using parameterized queries. Never concatenate user input into the query string.

func (*Manager) SetEventDispatcher added in v0.9.11

func (m *Manager) SetEventDispatcher(fn func(event interface{}) error)

SetEventDispatcher sets the function used to dispatch ORM events.

func (*Manager) Stats added in v0.9.5

func (m *Manager) Stats() sql.DBStats

Stats returns database connection pool statistics.

func (*Manager) Transaction added in v0.9.5

func (m *Manager) Transaction(fn func(tx *sql.Tx) error) error

Transaction executes a function within a database transaction.

type ManagerConfig added in v0.9.5

type ManagerConfig struct {
	Driver          string
	Host            string
	Port            string
	Database        string
	Username        string
	Password        string
	Charset         string
	SSLMode         string
	MaxIdleConns    int
	MaxOpenConns    int
	ConnMaxLifetime time.Duration
	LogQueries      bool
	SlowThreshold   time.Duration
}

ManagerConfig holds typed configuration for creating an ORM Manager.

type Model

type Model[T any] struct {
	ID        uint      `orm:"primaryKey;autoIncrement" json:"id"`
	CreatedAt time.Time `orm:"autoCreateTime" json:"created_at"`
	UpdatedAt time.Time `orm:"autoUpdateTime" json:"updated_at"`

	// Internal fields (not persisted)
	IsExisting bool            `orm:"-" json:"-"`
	Original   map[string]any  `orm:"-" json:"-"`
	Changed    map[string]bool `orm:"-" json:"-"`
}

Model is the generic base model that provides Laravel-style static methods. By default, models do NOT have soft deletes. Use SoftDeleteModel for soft delete support.

func (Model[T]) All

func (Model[T]) All() ([]T, error)

All retrieves all records

func (Model[T]) Count

func (Model[T]) Count() (int, error)

Count returns the number of records

func (Model[T]) Create

func (Model[T]) Create(data any) (*T, error)

Create inserts a new record or multiple records. Requires a *Manager — use orm.Save(manager, model) directly.

func (Model[T]) CreateMany

func (Model[T]) CreateMany(records []T) error

CreateMany inserts multiple records. Requires a *Manager — use orm.Save(manager, model) directly.

func (*Model[T]) Delete

func (m *Model[T]) Delete() error

Delete permanently deletes the model

func (Model[T]) DeleteWhere

func (Model[T]) DeleteWhere(conditions map[string]any) (int64, error)

DeleteWhere permanently deletes records matching conditions

func (Model[T]) Exists

func (Model[T]) Exists() bool

Exists checks if any records exist

func (Model[T]) Find

func (Model[T]) Find(id any) (*T, error)

Find retrieves a record by primary key

func (Model[T]) FindBy

func (Model[T]) FindBy(field string, value any) (*T, error)

FindBy retrieves a record by a specific field

func (Model[T]) First

func (Model[T]) First() (*T, error)

First retrieves the first record

func (*Model[T]) GetChanges

func (m *Model[T]) GetChanges() map[string]any

GetChanges returns all changed fields

func (*Model[T]) HasChanged

func (m *Model[T]) HasChanged(field string) bool

HasChanged checks if a field has changed

func (*Model[T]) IsClean

func (m *Model[T]) IsClean() bool

IsClean checks if the model has no unsaved changes

func (*Model[T]) IsDirty

func (m *Model[T]) IsDirty() bool

IsDirty checks if the model has any unsaved changes

func (Model[T]) Last

func (Model[T]) Last() (*T, error)

Last retrieves the last record

func (Model[T]) OrderBy

func (Model[T]) OrderBy(column, direction string) *Query[T]

OrderBy starts a query with an ORDER BY clause

func (Model[T]) Pluck

func (Model[T]) Pluck(column string) ([]any, error)

Pluck retrieves a single column values

func (Model[T]) Raw added in v0.8.0

func (Model[T]) Raw(sql string, args ...any) *RawQuery[T]

Raw creates a raw SQL query builder for executing custom queries Usage: Model{}.Raw("SELECT * FROM users WHERE id = ?", 1).First(&user)

func (*Model[T]) Refresh

func (m *Model[T]) Refresh() error

Refresh reloads the model from database

func (*Model[T]) Save

func (m *Model[T]) Save() error

Save inserts or updates the model

func (Model[T]) Update

func (Model[T]) Update(conditions map[string]any, updates map[string]any) (int64, error)

Update updates records matching conditions

func (Model[T]) Where

func (Model[T]) Where(condition string, args ...any) *Query[T]

Where starts a query with a WHERE condition

func (Model[T]) WhereIn

func (Model[T]) WhereIn(field string, values []any) *Query[T]

WhereIn queries for records where field is in the given values

func (Model[T]) WhereNotNull added in v0.8.0

func (Model[T]) WhereNotNull(field string) *Query[T]

WhereNotNull starts a query with a WHERE IS NOT NULL condition

func (Model[T]) WhereNull added in v0.8.0

func (Model[T]) WhereNull(field string) *Query[T]

WhereNull starts a query with a WHERE IS NULL condition

func (Model[T]) With

func (Model[T]) With(relations ...string) *Query[T]

With eager loads relationships

type Query

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

Query represents a chainable query builder with generics

func (*Query[T]) Chunk

func (q *Query[T]) Chunk(size int, callback func([]T) error) error

Chunk processes results in chunks

func (*Query[T]) Count

func (q *Query[T]) Count() (int, error)

Count returns the number of matching records

func (*Query[T]) Delete

func (q *Query[T]) Delete() (int64, error)

Delete soft deletes matching records (if model supports soft deletes) or hard deletes

func (*Query[T]) Distinct

func (q *Query[T]) Distinct() *Query[T]

Distinct adds DISTINCT to the query

func (*Query[T]) Exists

func (q *Query[T]) Exists() bool

Exists checks if any records match

func (*Query[T]) Find

func (q *Query[T]) Find(id any, dest *T) error

Find retrieves a record by primary key

func (*Query[T]) First

func (q *Query[T]) First(dest *T) error

First retrieves the first matching record

func (*Query[T]) ForceDelete

func (q *Query[T]) ForceDelete() (int64, error)

ForceDelete permanently deletes matching records

func (*Query[T]) Get

func (q *Query[T]) Get() ([]T, error)

Get retrieves all matching records

func (*Query[T]) GroupBy

func (q *Query[T]) GroupBy(columns ...string) *Query[T]

GroupBy adds a GROUP BY clause

func (*Query[T]) Having

func (q *Query[T]) Having(condition string, args ...any) *Query[T]

Having adds a HAVING condition

func (*Query[T]) InsertGetId

func (q *Query[T]) InsertGetId(data map[string]any) (int64, error)

InsertGetId inserts a record and returns the ID

func (*Query[T]) Join

func (q *Query[T]) Join(table, first, operator, second string) *Query[T]

Join adds an INNER JOIN

func (*Query[T]) LeftJoin

func (q *Query[T]) LeftJoin(table, first, operator, second string) *Query[T]

LeftJoin adds a LEFT JOIN

func (*Query[T]) Limit

func (q *Query[T]) Limit(n int) *Query[T]

Limit sets the LIMIT

func (*Query[T]) LockForUpdate

func (q *Query[T]) LockForUpdate() *Query[T]

LockForUpdate adds FOR UPDATE clause for pessimistic locking

func (*Query[T]) Offset

func (q *Query[T]) Offset(n int) *Query[T]

Offset sets the OFFSET

func (*Query[T]) OnlyTrashed

func (q *Query[T]) OnlyTrashed() *Query[T]

OnlyTrashed queries only soft deleted records

func (*Query[T]) OrWhere

func (q *Query[T]) OrWhere(condition string, args ...any) *Query[T]

OrWhere adds an OR WHERE condition

func (*Query[T]) OrderBy

func (q *Query[T]) OrderBy(column, direction string) *Query[T]

OrderBy adds an ORDER BY clause

func (*Query[T]) OrderByDesc

func (q *Query[T]) OrderByDesc(column string) *Query[T]

OrderByDesc adds an ORDER BY DESC clause

func (*Query[T]) Pluck

func (q *Query[T]) Pluck(column string) ([]any, error)

Pluck retrieves values of a single column

func (*Query[T]) RightJoin

func (q *Query[T]) RightJoin(table, first, operator, second string) *Query[T]

RightJoin adds a RIGHT JOIN

func (*Query[T]) Select

func (q *Query[T]) Select(columns ...string) *Query[T]

Select specifies columns to select

func (*Query[T]) SkipLocked

func (q *Query[T]) SkipLocked() *Query[T]

SkipLocked adds SKIP LOCKED clause to skip locked rows

func (*Query[T]) ToSQL

func (q *Query[T]) ToSQL() (string, []any)

ToSQL returns the SQL and arguments that would be executed

func (*Query[T]) Update

func (q *Query[T]) Update(updates map[string]any) (int64, error)

Update updates matching records

func (*Query[T]) Where

func (q *Query[T]) Where(condition string, args ...any) *Query[T]

Where adds a WHERE condition

func (*Query[T]) WhereBetween

func (q *Query[T]) WhereBetween(field string, start, end any) *Query[T]

WhereBetween adds a WHERE BETWEEN condition

func (*Query[T]) WhereIn

func (q *Query[T]) WhereIn(field string, values []any) *Query[T]

WhereIn adds a WHERE IN condition

func (*Query[T]) WhereNotIn

func (q *Query[T]) WhereNotIn(field string, values []any) *Query[T]

WhereNotIn adds a WHERE NOT IN condition

func (*Query[T]) WhereNotNull

func (q *Query[T]) WhereNotNull(field string) *Query[T]

WhereNotNull adds a WHERE IS NOT NULL condition

func (*Query[T]) WhereNull

func (q *Query[T]) WhereNull(field string) *Query[T]

WhereNull adds a WHERE IS NULL condition

func (*Query[T]) With

func (q *Query[T]) With(relations ...string) *Query[T]

With eager loads relationships

func (*Query[T]) WithContext added in v0.4.0

func (q *Query[T]) WithContext(ctx context.Context) *Query[T]

WithContext sets the context for the query (for event propagation)

func (*Query[T]) WithTrashed

func (q *Query[T]) WithTrashed() *Query[T]

WithTrashed includes soft deleted records

type QueryExecuted added in v0.4.0

type QueryExecuted struct {
	Context      context.Context
	SQL          string
	Bindings     []any
	Duration     time.Duration
	RowsAffected int64
	Connection   string // Database connection/driver name
	File         string // Caller file
	Line         int    // Caller line
	TraceID      string // APM trace ID
	SpanID       string // APM span ID
	ParentID     string // Parent span ID for correlation
}

QueryExecuted is dispatched when a database query completes

func (*QueryExecuted) Name added in v0.4.0

func (e *QueryExecuted) Name() string

Name returns the event name

type QueryExecutor added in v0.7.0

type QueryExecutor interface {
	Exec(query string, args ...any) (sql.Result, error)
	Query(query string, args ...any) (*sql.Rows, error)
	QueryRow(query string, args ...any) *sql.Row
}

QueryExecutor interface for *sql.DB and *sql.Tx

type RawQuery added in v0.8.0

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

RawQuery represents a raw SQL query that can be executed with First() or Get()

func NewRawQuery added in v0.8.0

func NewRawQuery[T any](sql string, args ...any) *RawQuery[T]

NewRawQuery creates a new raw query builder.

WARNING: This method executes raw SQL directly. The caller is responsible for preventing SQL injection by using parameterized queries with placeholder arguments. Never concatenate user input directly into the sql string.

func (*RawQuery[T]) Exec added in v0.8.0

func (r *RawQuery[T]) Exec() (int64, error)

Exec executes a raw SQL statement (INSERT, UPDATE, DELETE) and returns affected rows

func (*RawQuery[T]) First added in v0.8.0

func (r *RawQuery[T]) First(dest *T) error

First executes the raw query and scans the first result into dest

func (*RawQuery[T]) Get added in v0.8.0

func (r *RawQuery[T]) Get() ([]T, error)

Get executes the raw query and returns all matching results

func (*RawQuery[T]) Scan added in v0.8.0

func (r *RawQuery[T]) Scan(dest ...any) error

Scan executes the raw query and scans into custom destination pointers Useful for queries that return scalar values or don't map to structs

func (*RawQuery[T]) WithContext added in v0.8.0

func (r *RawQuery[T]) WithContext(ctx context.Context) *RawQuery[T]

WithContext sets the context for the raw query

type SoftDeleteModel added in v0.8.0

type SoftDeleteModel[T any] struct {
	ID        uint       `orm:"primaryKey;autoIncrement" json:"id"`
	CreatedAt time.Time  `orm:"autoCreateTime" json:"created_at"`
	UpdatedAt time.Time  `orm:"autoUpdateTime" json:"updated_at"`
	DeletedAt *time.Time `orm:"index" json:"deleted_at,omitempty"`

	// Internal fields (not persisted)
	IsExisting bool            `orm:"-" json:"-"`
	Original   map[string]any  `orm:"-" json:"-"`
	Changed    map[string]bool `orm:"-" json:"-"`
}

SoftDeleteModel is a base model WITH soft delete support. Use this when you need to keep deleted records (e.g., users, orders, audit trails).

func (SoftDeleteModel[T]) All added in v0.8.0

func (SoftDeleteModel[T]) All() ([]T, error)

All retrieves all records

func (SoftDeleteModel[T]) Count added in v0.8.0

func (SoftDeleteModel[T]) Count() (int, error)

Count returns the number of records

func (SoftDeleteModel[T]) Create added in v0.8.0

func (SoftDeleteModel[T]) Create(data any) (*T, error)

Create inserts a new record

func (SoftDeleteModel[T]) CreateMany added in v0.8.0

func (SoftDeleteModel[T]) CreateMany(records []T) error

CreateMany inserts multiple records

func (*SoftDeleteModel[T]) Delete added in v0.8.0

func (m *SoftDeleteModel[T]) Delete() error

Delete soft deletes the model

func (SoftDeleteModel[T]) DeleteWhere added in v0.8.0

func (SoftDeleteModel[T]) DeleteWhere(conditions map[string]any) (int64, error)

DeleteWhere soft deletes records matching conditions

func (SoftDeleteModel[T]) Exists added in v0.8.0

func (SoftDeleteModel[T]) Exists() bool

Exists checks if any records exist

func (SoftDeleteModel[T]) Find added in v0.8.0

func (SoftDeleteModel[T]) Find(id any) (*T, error)

Find retrieves a record by primary key

func (SoftDeleteModel[T]) FindBy added in v0.8.0

func (SoftDeleteModel[T]) FindBy(field string, value any) (*T, error)

FindBy retrieves a record by a specific field

func (SoftDeleteModel[T]) First added in v0.8.0

func (SoftDeleteModel[T]) First() (*T, error)

First retrieves the first record

func (*SoftDeleteModel[T]) ForceDelete added in v0.8.0

func (m *SoftDeleteModel[T]) ForceDelete() error

ForceDelete permanently deletes the model

func (SoftDeleteModel[T]) ForceDeleteWhere added in v0.8.0

func (SoftDeleteModel[T]) ForceDeleteWhere(conditions map[string]any) (int64, error)

ForceDeleteWhere permanently deletes records matching conditions

func (*SoftDeleteModel[T]) GetChanges added in v0.8.0

func (m *SoftDeleteModel[T]) GetChanges() map[string]any

GetChanges returns all changed fields

func (*SoftDeleteModel[T]) HasChanged added in v0.8.0

func (m *SoftDeleteModel[T]) HasChanged(field string) bool

HasChanged checks if a field has changed

func (*SoftDeleteModel[T]) IsClean added in v0.8.0

func (m *SoftDeleteModel[T]) IsClean() bool

IsClean checks if the model has no unsaved changes

func (*SoftDeleteModel[T]) IsDirty added in v0.8.0

func (m *SoftDeleteModel[T]) IsDirty() bool

IsDirty checks if the model has any unsaved changes

func (SoftDeleteModel[T]) Last added in v0.8.0

func (SoftDeleteModel[T]) Last() (*T, error)

Last retrieves the last record

func (SoftDeleteModel[T]) OnlyTrashed added in v0.8.0

func (SoftDeleteModel[T]) OnlyTrashed() *Query[T]

OnlyTrashed retrieves only soft deleted records

func (SoftDeleteModel[T]) OrderBy added in v0.8.0

func (SoftDeleteModel[T]) OrderBy(column, direction string) *Query[T]

OrderBy starts a query with an ORDER BY clause

func (SoftDeleteModel[T]) Pluck added in v0.8.0

func (SoftDeleteModel[T]) Pluck(column string) ([]any, error)

Pluck retrieves a single column values

func (SoftDeleteModel[T]) Raw added in v0.8.0

func (SoftDeleteModel[T]) Raw(sql string, args ...any) *RawQuery[T]

Raw creates a raw SQL query builder for executing custom queries Usage: SoftDeleteModel{}.Raw("SELECT * FROM users WHERE id = ?", 1).First(&user)

func (*SoftDeleteModel[T]) Refresh added in v0.8.0

func (m *SoftDeleteModel[T]) Refresh() error

Refresh reloads the model from database

func (*SoftDeleteModel[T]) Restore added in v0.8.0

func (m *SoftDeleteModel[T]) Restore() error

Restore restores a soft deleted model

func (*SoftDeleteModel[T]) Save added in v0.8.0

func (m *SoftDeleteModel[T]) Save() error

Save inserts or updates the model

func (SoftDeleteModel[T]) Update added in v0.8.0

func (SoftDeleteModel[T]) Update(conditions map[string]any, updates map[string]any) (int64, error)

Update updates records matching conditions

func (SoftDeleteModel[T]) Where added in v0.8.0

func (SoftDeleteModel[T]) Where(condition string, args ...any) *Query[T]

Where starts a query with a WHERE condition

func (SoftDeleteModel[T]) WhereIn added in v0.8.0

func (SoftDeleteModel[T]) WhereIn(field string, values []any) *Query[T]

WhereIn queries for records where field is in the given values

func (SoftDeleteModel[T]) WhereNotNull added in v0.8.0

func (SoftDeleteModel[T]) WhereNotNull(field string) *Query[T]

WhereNotNull starts a query with a WHERE IS NOT NULL condition

func (SoftDeleteModel[T]) WhereNull added in v0.8.0

func (SoftDeleteModel[T]) WhereNull(field string) *Query[T]

WhereNull starts a query with a WHERE IS NULL condition

func (SoftDeleteModel[T]) With added in v0.8.0

func (SoftDeleteModel[T]) With(relations ...string) *Query[T]

With eager loads relationships

func (SoftDeleteModel[T]) WithTrashed added in v0.8.0

func (SoftDeleteModel[T]) WithTrashed() *Query[T]

WithTrashed includes soft deleted records

type SoftDeleteUUIDModel added in v0.8.0

type SoftDeleteUUIDModel[T any] struct {
	ID        string     `orm:"primaryKey;type:uuid" json:"id"`
	CreatedAt time.Time  `orm:"autoCreateTime" json:"created_at"`
	UpdatedAt time.Time  `orm:"autoUpdateTime" json:"updated_at"`
	DeletedAt *time.Time `orm:"index" json:"deleted_at,omitempty"`

	// Internal fields (not persisted)
	IsExisting bool            `orm:"-" json:"-"`
	Original   map[string]any  `orm:"-" json:"-"`
	Changed    map[string]bool `orm:"-" json:"-"`
}

SoftDeleteUUIDModel is a UUID primary key model WITH soft delete support.

func (SoftDeleteUUIDModel[T]) All added in v0.8.0

func (SoftDeleteUUIDModel[T]) All() ([]T, error)

All retrieves all records

func (SoftDeleteUUIDModel[T]) Count added in v0.8.0

func (SoftDeleteUUIDModel[T]) Count() (int, error)

Count returns the number of records

func (SoftDeleteUUIDModel[T]) Create added in v0.8.0

func (SoftDeleteUUIDModel[T]) Create(data any) (*T, error)

Create inserts a new record

func (SoftDeleteUUIDModel[T]) CreateMany added in v0.8.0

func (SoftDeleteUUIDModel[T]) CreateMany(records []T) error

CreateMany inserts multiple records

func (*SoftDeleteUUIDModel[T]) Delete added in v0.8.0

func (m *SoftDeleteUUIDModel[T]) Delete() error

Delete soft deletes the model

func (SoftDeleteUUIDModel[T]) DeleteWhere added in v0.8.0

func (SoftDeleteUUIDModel[T]) DeleteWhere(conditions map[string]any) (int64, error)

DeleteWhere soft deletes records matching conditions

func (SoftDeleteUUIDModel[T]) Exists added in v0.8.0

func (SoftDeleteUUIDModel[T]) Exists() bool

Exists checks if any records exist

func (SoftDeleteUUIDModel[T]) Find added in v0.8.0

func (SoftDeleteUUIDModel[T]) Find(id string) (*T, error)

Find retrieves a record by UUID primary key

func (SoftDeleteUUIDModel[T]) FindBy added in v0.8.0

func (SoftDeleteUUIDModel[T]) FindBy(field string, value any) (*T, error)

FindBy retrieves a record by a specific field

func (SoftDeleteUUIDModel[T]) First added in v0.8.0

func (SoftDeleteUUIDModel[T]) First() (*T, error)

First retrieves the first record

func (*SoftDeleteUUIDModel[T]) ForceDelete added in v0.8.0

func (m *SoftDeleteUUIDModel[T]) ForceDelete() error

ForceDelete permanently deletes the model

func (SoftDeleteUUIDModel[T]) ForceDeleteWhere added in v0.8.0

func (SoftDeleteUUIDModel[T]) ForceDeleteWhere(conditions map[string]any) (int64, error)

ForceDeleteWhere permanently deletes records matching conditions

func (*SoftDeleteUUIDModel[T]) GetChanges added in v0.8.0

func (m *SoftDeleteUUIDModel[T]) GetChanges() map[string]any

GetChanges returns all changed fields

func (*SoftDeleteUUIDModel[T]) HasChanged added in v0.8.0

func (m *SoftDeleteUUIDModel[T]) HasChanged(field string) bool

HasChanged checks if a field has changed

func (*SoftDeleteUUIDModel[T]) IsClean added in v0.8.0

func (m *SoftDeleteUUIDModel[T]) IsClean() bool

IsClean checks if the model has no unsaved changes

func (*SoftDeleteUUIDModel[T]) IsDirty added in v0.8.0

func (m *SoftDeleteUUIDModel[T]) IsDirty() bool

IsDirty checks if the model has any unsaved changes

func (SoftDeleteUUIDModel[T]) Last added in v0.8.0

func (SoftDeleteUUIDModel[T]) Last() (*T, error)

Last retrieves the last record (by created_at descending)

func (SoftDeleteUUIDModel[T]) OnlyTrashed added in v0.8.0

func (SoftDeleteUUIDModel[T]) OnlyTrashed() *Query[T]

OnlyTrashed retrieves only soft deleted records

func (SoftDeleteUUIDModel[T]) OrderBy added in v0.8.0

func (SoftDeleteUUIDModel[T]) OrderBy(column, direction string) *Query[T]

OrderBy starts a query with an ORDER BY clause

func (SoftDeleteUUIDModel[T]) Pluck added in v0.8.0

func (SoftDeleteUUIDModel[T]) Pluck(column string) ([]any, error)

Pluck retrieves a single column values

func (SoftDeleteUUIDModel[T]) Raw added in v0.8.0

func (SoftDeleteUUIDModel[T]) Raw(sql string, args ...any) *RawQuery[T]

Raw creates a raw SQL query builder for executing custom queries Usage: SoftDeleteUUIDModel{}.Raw("SELECT * FROM users WHERE id = ?", 1).First(&user)

func (*SoftDeleteUUIDModel[T]) Refresh added in v0.8.0

func (m *SoftDeleteUUIDModel[T]) Refresh() error

Refresh reloads the model from database

func (*SoftDeleteUUIDModel[T]) Restore added in v0.8.0

func (m *SoftDeleteUUIDModel[T]) Restore() error

Restore restores a soft deleted model

func (*SoftDeleteUUIDModel[T]) Save added in v0.8.0

func (m *SoftDeleteUUIDModel[T]) Save() error

Save inserts or updates the model

func (SoftDeleteUUIDModel[T]) Update added in v0.8.0

func (SoftDeleteUUIDModel[T]) Update(conditions map[string]any, updates map[string]any) (int64, error)

Update updates records matching conditions

func (SoftDeleteUUIDModel[T]) Where added in v0.8.0

func (SoftDeleteUUIDModel[T]) Where(condition string, args ...any) *Query[T]

Where starts a query with a WHERE condition

func (SoftDeleteUUIDModel[T]) WhereIn added in v0.8.0

func (SoftDeleteUUIDModel[T]) WhereIn(field string, values []any) *Query[T]

WhereIn queries for records where field is in the given values

func (SoftDeleteUUIDModel[T]) WhereNotNull added in v0.8.0

func (SoftDeleteUUIDModel[T]) WhereNotNull(field string) *Query[T]

WhereNotNull starts a query with a WHERE IS NOT NULL condition

func (SoftDeleteUUIDModel[T]) WhereNull added in v0.8.0

func (SoftDeleteUUIDModel[T]) WhereNull(field string) *Query[T]

WhereNull starts a query with a WHERE IS NULL condition

func (SoftDeleteUUIDModel[T]) With added in v0.8.0

func (SoftDeleteUUIDModel[T]) With(relations ...string) *Query[T]

With eager loads relationships

func (SoftDeleteUUIDModel[T]) WithTrashed added in v0.8.0

func (SoftDeleteUUIDModel[T]) WithTrashed() *Query[T]

WithTrashed includes soft deleted records

type UUIDModel added in v0.8.0

type UUIDModel[T any] struct {
	ID        string    `orm:"primaryKey;type:uuid" json:"id"`
	CreatedAt time.Time `orm:"autoCreateTime" json:"created_at"`
	UpdatedAt time.Time `orm:"autoUpdateTime" json:"updated_at"`

	// Internal fields (not persisted)
	IsExisting bool            `orm:"-" json:"-"`
	Original   map[string]any  `orm:"-" json:"-"`
	Changed    map[string]bool `orm:"-" json:"-"`
}

UUIDModel is a generic base model with UUID primary key for distributed systems and external-facing APIs where sequential IDs pose security risks. By default, models do NOT have soft deletes. Use SoftDeleteUUIDModel for soft delete support.

func (UUIDModel[T]) All added in v0.8.0

func (UUIDModel[T]) All() ([]T, error)

All retrieves all records

func (UUIDModel[T]) Count added in v0.8.0

func (UUIDModel[T]) Count() (int, error)

Count returns the number of records

func (UUIDModel[T]) Create added in v0.8.0

func (UUIDModel[T]) Create(data any) (*T, error)

Create inserts a new record or multiple records

func (UUIDModel[T]) CreateMany added in v0.8.0

func (UUIDModel[T]) CreateMany(records []T) error

CreateMany inserts multiple records

func (*UUIDModel[T]) Delete added in v0.8.0

func (m *UUIDModel[T]) Delete() error

Delete permanently deletes the model

func (UUIDModel[T]) DeleteWhere added in v0.8.0

func (UUIDModel[T]) DeleteWhere(conditions map[string]any) (int64, error)

DeleteWhere permanently deletes records matching conditions

func (UUIDModel[T]) Exists added in v0.8.0

func (UUIDModel[T]) Exists() bool

Exists checks if any records exist

func (UUIDModel[T]) Find added in v0.8.0

func (UUIDModel[T]) Find(id string) (*T, error)

Find retrieves a record by UUID primary key

func (UUIDModel[T]) FindBy added in v0.8.0

func (UUIDModel[T]) FindBy(field string, value any) (*T, error)

FindBy retrieves a record by a specific field

func (UUIDModel[T]) First added in v0.8.0

func (UUIDModel[T]) First() (*T, error)

First retrieves the first record

func (*UUIDModel[T]) GetChanges added in v0.8.0

func (m *UUIDModel[T]) GetChanges() map[string]any

GetChanges returns all changed fields

func (*UUIDModel[T]) HasChanged added in v0.8.0

func (m *UUIDModel[T]) HasChanged(field string) bool

HasChanged checks if a field has changed

func (*UUIDModel[T]) IsClean added in v0.8.0

func (m *UUIDModel[T]) IsClean() bool

IsClean checks if the model has no unsaved changes

func (*UUIDModel[T]) IsDirty added in v0.8.0

func (m *UUIDModel[T]) IsDirty() bool

IsDirty checks if the model has any unsaved changes

func (UUIDModel[T]) Last added in v0.8.0

func (UUIDModel[T]) Last() (*T, error)

Last retrieves the last record (by created_at descending)

func (UUIDModel[T]) OrderBy added in v0.8.0

func (UUIDModel[T]) OrderBy(column, direction string) *Query[T]

OrderBy starts a query with an ORDER BY clause

func (UUIDModel[T]) Pluck added in v0.8.0

func (UUIDModel[T]) Pluck(column string) ([]any, error)

Pluck retrieves a single column values

func (UUIDModel[T]) Raw added in v0.8.0

func (UUIDModel[T]) Raw(sql string, args ...any) *RawQuery[T]

Raw creates a raw SQL query builder for executing custom queries Usage: UUIDModel{}.Raw("SELECT * FROM users WHERE id = ?", 1).First(&user)

func (*UUIDModel[T]) Refresh added in v0.8.0

func (m *UUIDModel[T]) Refresh() error

Refresh reloads the model from database

func (*UUIDModel[T]) Save added in v0.8.0

func (m *UUIDModel[T]) Save() error

Save inserts or updates the model

func (UUIDModel[T]) Update added in v0.8.0

func (UUIDModel[T]) Update(conditions map[string]any, updates map[string]any) (int64, error)

Update updates records matching conditions

func (UUIDModel[T]) Where added in v0.8.0

func (UUIDModel[T]) Where(condition string, args ...any) *Query[T]

Where starts a query with a WHERE condition

func (UUIDModel[T]) WhereIn added in v0.8.0

func (UUIDModel[T]) WhereIn(field string, values []any) *Query[T]

WhereIn queries for records where field is in the given values

func (UUIDModel[T]) WhereNotNull added in v0.8.0

func (UUIDModel[T]) WhereNotNull(field string) *Query[T]

WhereNotNull starts a query with a WHERE IS NOT NULL condition

func (UUIDModel[T]) WhereNull added in v0.8.0

func (UUIDModel[T]) WhereNull(field string) *Query[T]

WhereNull starts a query with a WHERE IS NULL condition

func (UUIDModel[T]) With added in v0.8.0

func (UUIDModel[T]) With(relations ...string) *Query[T]

With eager loads relationships

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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