orm

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: MIT Imports: 11 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 Begin

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

Begin starts a new transaction

func Close

func Close() error

Close closes the database connection

func Connection

func Connection(name string) (drivers.Driver, error)

Connection returns a named database connection

func CreateMany

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

func DB

func DB() *sql.DB

DB returns the underlying *sql.DB instance

func Exec

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

Exec executes a raw SQL statement

func Fresh

func Fresh() error

Fresh drops all tables and re-runs migrations

func GetDatabaseName

func GetDatabaseName() string

GetDatabaseName returns the name of the current database

func GetDriver

func GetDriver() string

GetDriver returns the name of the current database driver

func Hash

func Hash(password string) string

Hash hashes a password (utility function for seeders)

func Init

func Init(driverName string, config map[string]any) error

Init initializes the ORM with the specified driver and configuration

func InitFromEnv

func InitFromEnv() error

InitFromEnv manually initializes the ORM from environment variables

func Migrate

func Migrate() error

Migrate runs database migrations This is a placeholder - actual implementation should use pkg/orm/migrate package Example: migrator := migrate.NewMigrator(orm.DB(), orm.GetDriver()); migrator.Up()

func Ping

func Ping() error

Ping verifies the database connection

func Raw

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

Raw executes a raw SQL query

func RegisterDriver

func RegisterDriver(name string, factory func() drivers.Driver)

RegisterDriver registers a new database driver

func Rollback

func Rollback(steps int) error

Rollback rolls back migrations This is a placeholder - actual implementation should use pkg/orm/migrate package Example: migrator := migrate.NewMigrator(orm.DB(), orm.GetDriver()); migrator.Down(steps)

func Save

func Save[T any](model *T) error

func Seed

func Seed(seeders ...string) error

Seed runs database seeders

func SetConnMaxIdleTime

func SetConnMaxIdleTime(d time.Duration)

SetConnMaxIdleTime sets the maximum idle time of connections

func SetConnMaxLifetime

func SetConnMaxLifetime(d time.Duration)

SetConnMaxLifetime sets the maximum lifetime of connections

func SetConnection

func SetConnection(name string, driver drivers.Driver)

SetConnection sets a named database connection

func SetMaxIdleConns

func SetMaxIdleConns(n int)

SetMaxIdleConns sets the maximum number of idle connections

func SetMaxOpenConns

func SetMaxOpenConns(n int)

SetMaxOpenConns sets the maximum number of open connections

func Stats

func Stats() sql.DBStats

Stats returns database connection pool statistics

func Transaction

func Transaction(fn func() error) error

Transaction executes a function within a database transaction

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 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"`
	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:"-"`
}

Model is the generic base model that provides Laravel-style static methods

func (Model[T]) All

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

All retrieves all records

func (Model[T]) Count

func (Model[T]) Count() (int64, 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

func (Model[T]) CreateMany

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

CreateMany inserts multiple records

func (*Model[T]) Delete

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

Delete soft deletes the model

func (Model[T]) DeleteWhere

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

DeleteWhere soft 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]) ForceDelete

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

ForceDelete permanently deletes the model

func (Model[T]) ForceDeleteWhere

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

ForceDeleteWhere permanently deletes records matching conditions

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]) OnlyTrashed

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

OnlyTrashed retrieves only soft deleted records

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]) Refresh

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

Refresh reloads the model from database

func (*Model[T]) Restore

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

Restore restores a soft deleted model

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]) With

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

With eager loads relationships

func (Model[T]) WithTrashed

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

WithTrashed includes soft deleted records

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() (int64, error)

Count returns the number of matching records

func (*Query[T]) Delete

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

Delete soft deletes matching records

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]) WithTrashed

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

WithTrashed includes soft deleted records

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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