Documentation
¶
Index ¶
- Variables
- func Begin() (*sql.Tx, error)
- func Close() error
- func Connection(name string) (drivers.Driver, error)
- func CreateMany[T any](records []T) error
- func DB() *sql.DB
- func Exec(query string, args ...any) (sql.Result, error)
- func Fresh() error
- func GetDatabaseName() string
- func GetDriver() string
- func Hash(password string) string
- func Init(driverName string, config map[string]any) error
- func InitFromEnv() error
- func Migrate() error
- func Ping() error
- func Raw(query string, args ...any) *sql.Rows
- func RegisterDriver(name string, factory func() drivers.Driver)
- func Rollback(steps int) error
- func Save[T any](model *T) error
- func Seed(seeders ...string) error
- func SetConnMaxIdleTime(d time.Duration)
- func SetConnMaxLifetime(d time.Duration)
- func SetConnection(name string, driver drivers.Driver)
- func SetMaxIdleConns(n int)
- func SetMaxOpenConns(n int)
- func Stats() sql.DBStats
- func Transaction(fn func() error) error
- type AfterCreateHook
- type AfterDeleteHook
- type AfterUpdateHook
- type BeforeCreateHook
- type BeforeDeleteHook
- type BeforeUpdateHook
- type Model
- func (Model[T]) All() ([]T, error)
- func (Model[T]) Count() (int64, error)
- func (Model[T]) Create(data any) (*T, error)
- func (Model[T]) CreateMany(records []T) error
- func (m *Model[T]) Delete() error
- func (Model[T]) DeleteWhere(conditions map[string]any) (int64, error)
- func (Model[T]) Exists() bool
- func (Model[T]) Find(id any) (*T, error)
- func (Model[T]) FindBy(field string, value any) (*T, error)
- func (Model[T]) First() (*T, error)
- func (m *Model[T]) ForceDelete() error
- func (Model[T]) ForceDeleteWhere(conditions map[string]any) (int64, error)
- func (m *Model[T]) GetChanges() map[string]any
- func (m *Model[T]) HasChanged(field string) bool
- func (m *Model[T]) IsClean() bool
- func (m *Model[T]) IsDirty() bool
- func (Model[T]) Last() (*T, error)
- func (Model[T]) OnlyTrashed() *Query[T]
- func (Model[T]) OrderBy(column, direction string) *Query[T]
- func (Model[T]) Pluck(column string) ([]any, error)
- func (m *Model[T]) Refresh() error
- func (m *Model[T]) Restore() error
- func (m *Model[T]) Save() error
- func (Model[T]) Update(conditions map[string]any, updates map[string]any) (int64, error)
- func (Model[T]) Where(condition string, args ...any) *Query[T]
- func (Model[T]) WhereIn(field string, values []any) *Query[T]
- func (Model[T]) With(relations ...string) *Query[T]
- func (Model[T]) WithTrashed() *Query[T]
- type Query
- func (q *Query[T]) Chunk(size int, callback func([]T) error) error
- func (q *Query[T]) Count() (int64, error)
- func (q *Query[T]) Delete() (int64, error)
- func (q *Query[T]) Distinct() *Query[T]
- func (q *Query[T]) Exists() bool
- func (q *Query[T]) Find(id any, dest *T) error
- func (q *Query[T]) First(dest *T) error
- func (q *Query[T]) ForceDelete() (int64, error)
- func (q *Query[T]) Get() ([]T, error)
- func (q *Query[T]) GroupBy(columns ...string) *Query[T]
- func (q *Query[T]) Having(condition string, args ...any) *Query[T]
- func (q *Query[T]) InsertGetId(data map[string]any) (int64, error)
- func (q *Query[T]) Join(table, first, operator, second string) *Query[T]
- func (q *Query[T]) LeftJoin(table, first, operator, second string) *Query[T]
- func (q *Query[T]) Limit(n int) *Query[T]
- func (q *Query[T]) LockForUpdate() *Query[T]
- func (q *Query[T]) Offset(n int) *Query[T]
- func (q *Query[T]) OnlyTrashed() *Query[T]
- func (q *Query[T]) OrWhere(condition string, args ...any) *Query[T]
- func (q *Query[T]) OrderBy(column, direction string) *Query[T]
- func (q *Query[T]) OrderByDesc(column string) *Query[T]
- func (q *Query[T]) Pluck(column string) ([]any, error)
- func (q *Query[T]) RightJoin(table, first, operator, second string) *Query[T]
- func (q *Query[T]) Select(columns ...string) *Query[T]
- func (q *Query[T]) SkipLocked() *Query[T]
- func (q *Query[T]) ToSQL() (string, []any)
- func (q *Query[T]) Update(updates map[string]any) (int64, error)
- func (q *Query[T]) Where(condition string, args ...any) *Query[T]
- func (q *Query[T]) WhereBetween(field string, start, end any) *Query[T]
- func (q *Query[T]) WhereIn(field string, values []any) *Query[T]
- func (q *Query[T]) WhereNotIn(field string, values []any) *Query[T]
- func (q *Query[T]) WhereNotNull(field string) *Query[T]
- func (q *Query[T]) WhereNull(field string) *Query[T]
- func (q *Query[T]) With(relations ...string) *Query[T]
- func (q *Query[T]) WithTrashed() *Query[T]
Constants ¶
This section is empty.
Variables ¶
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 Connection ¶
Connection returns a named database connection
func CreateMany ¶
func GetDatabaseName ¶
func GetDatabaseName() string
GetDatabaseName returns the name of the current database
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 RegisterDriver ¶
RegisterDriver registers a new database driver
func Rollback ¶
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 SetConnMaxIdleTime ¶
SetConnMaxIdleTime sets the maximum idle time of connections
func SetConnMaxLifetime ¶
SetConnMaxLifetime sets the maximum lifetime of connections
func SetConnection ¶
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 Transaction ¶
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]) CreateMany ¶
CreateMany inserts multiple records
func (Model[T]) DeleteWhere ¶
DeleteWhere soft deletes records matching conditions
func (*Model[T]) ForceDelete ¶
ForceDelete permanently deletes the model
func (Model[T]) ForceDeleteWhere ¶
ForceDeleteWhere permanently deletes records matching conditions
func (*Model[T]) GetChanges ¶
GetChanges returns all changed fields
func (*Model[T]) HasChanged ¶
HasChanged checks if a field has changed
func (Model[T]) OnlyTrashed ¶
OnlyTrashed retrieves only soft deleted records
func (Model[T]) WithTrashed ¶
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]) ForceDelete ¶
ForceDelete permanently deletes matching records
func (*Query[T]) InsertGetId ¶
InsertGetId inserts a record and returns the ID
func (*Query[T]) LockForUpdate ¶
LockForUpdate adds FOR UPDATE clause for pessimistic locking
func (*Query[T]) OnlyTrashed ¶
OnlyTrashed queries only soft deleted records
func (*Query[T]) OrderByDesc ¶
OrderByDesc adds an ORDER BY DESC clause
func (*Query[T]) SkipLocked ¶
SkipLocked adds SKIP LOCKED clause to skip locked rows
func (*Query[T]) WhereBetween ¶
WhereBetween adds a WHERE BETWEEN condition
func (*Query[T]) WhereNotIn ¶
WhereNotIn adds a WHERE NOT IN condition
func (*Query[T]) WhereNotNull ¶
WhereNotNull adds a WHERE IS NOT NULL condition
func (*Query[T]) WithTrashed ¶
WithTrashed includes soft deleted records