Documentation
¶
Overview ¶
Package database provides the seeder infrastructure for populating the database with test or production seed data.
Index ¶
- Variables
- func FakeAddress() string
- func FakeBool() bool
- func FakeCompany() string
- func FakeEmail() string
- func FakeInt(min, max int) int
- func FakeName() string
- func FakePhone() string
- func FakeText(length int) string
- func InitializeEncryption(appKey string) error
- func ObservePool(db *sql.DB, meter metric.Meter, driverName string) error
- func Register(seeders ...Seeder)
- func RegisterModel[T any](meta ModelMeta)
- func ScanIter[T any](db *DB, rows Rows, meta *ModelMeta) iter.Seq2[*T, error]
- func SetAttachmentResolver(fn func(disk, path string) (string, error))
- func TenantIDFromContext(ctx context.Context) (any, bool)
- func WithContext(ctx context.Context, db *DB) context.Context
- func WithTenantID(ctx context.Context, tenantID any) context.Context
- type AfterCreateHook
- type AfterDeleteHook
- type AfterFindHook
- type AfterUpdateHook
- type AuditEntry
- type Auditable
- type Auditor
- type BaseRepository
- func (r *BaseRepository[T]) Count(ctx context.Context) (int64, error)
- func (r *BaseRepository[T]) Create(ctx context.Context, model *T) (*T, error)
- func (r *BaseRepository[T]) Delete(ctx context.Context, id any) error
- func (r *BaseRepository[T]) FindAll(ctx context.Context, page, perPage int) ([]T, error)
- func (r *BaseRepository[T]) FindByID(ctx context.Context, id any) (*T, error)
- func (r *BaseRepository[T]) Update(ctx context.Context, model *T) error
- type BeforeCreateHook
- type BeforeDeleteHook
- type BeforeUpdateHook
- type BelongsTo
- type ColumnMeta
- type Config
- type Connection
- type CursorPaginated
- type CustomScanner
- type DB
- func (db *DB) Begin(ctx context.Context) (Transaction, error)
- func (db *DB) Close() error
- func (db *DB) Dialect() Dialect
- func (db *DB) DropAllTables(ctx context.Context) error
- func (db *DB) Exec(ctx context.Context, sqlStr string, args ...any) (sql.Result, error)
- func (db *DB) Pool() *sql.DB
- func (db *DB) Query(ctx context.Context, sqlStr string, args ...any) (Rows, error)
- func (db *DB) QueryRow(ctx context.Context, sqlStr string, args ...any) Row
- func (db *DB) Schema() *schema.Builder
- func (db *DB) SetQueryHook(hook QueryHook)
- func (db *DB) Transaction(ctx context.Context, fn func(txCtx context.Context) error) error
- func (db *DB) WithTx(tx Transaction) *DB
- type Dialect
- type Encrypted
- type FactoryBuilder
- func (fb *FactoryBuilder[T]) Create(ctx context.Context, db ...*DB) (*T, error)
- func (fb *FactoryBuilder[T]) CreateMany(ctx context.Context, count int, db ...*DB) ([]*T, error)
- func (fb *FactoryBuilder[T]) Make() T
- func (fb *FactoryBuilder[T]) State(name string) *FactoryBuilder[T]
- func (fb *FactoryBuilder[T]) WithDB(db *DB) *FactoryBuilder[T]
- type FactoryDef
- type HasMany
- type HasOne
- type ManyToMany
- type Model
- type ModelMeta
- type MorphMany
- type MorphTo
- type MySQLDialect
- func (d MySQLDialect) AdvisoryLock(id int64) string
- func (d MySQLDialect) AdvisoryUnlock(id int64) string
- func (d MySQLDialect) AutoIncrementDDL() string
- func (d MySQLDialect) ConfigurePool(db *sql.DB)
- func (d MySQLDialect) LimitOffsetSQL(limit, offset int) string
- func (d MySQLDialect) Name() string
- func (d MySQLDialect) Placeholder(n int) string
- func (d MySQLDialect) QuoteIdentifier(name string) string
- func (d MySQLDialect) SupportsReturning() bool
- func (d MySQLDialect) UpsertSQL(table string, columns []string, conflict string) string
- type NeonDialect
- type Paginated
- type PaginationResult
- type PostgresDialect
- func (d PostgresDialect) AdvisoryLock(id int64) string
- func (d PostgresDialect) AdvisoryUnlock(id int64) string
- func (d PostgresDialect) AutoIncrementDDL() string
- func (d PostgresDialect) ConfigurePool(db *sql.DB)
- func (d PostgresDialect) LimitOffsetSQL(limit, offset int) string
- func (d PostgresDialect) Name() string
- func (d PostgresDialect) Placeholder(n int) string
- func (d PostgresDialect) QuoteIdentifier(name string) string
- func (d PostgresDialect) SupportsReturning() bool
- func (d PostgresDialect) UpsertSQL(table string, columns []string, conflict string) string
- type QueryBuilder
- func (q *QueryBuilder[T]) All(ctx ...context.Context) iter.Seq2[*T, error]
- func (q *QueryBuilder[T]) AllSlice(ctx ...context.Context) ([]T, error)
- func (q *QueryBuilder[T]) ApplyScopes() *QueryBuilder[T]
- func (q *QueryBuilder[T]) Attach(relation string, ownerID uint, relatedIDs []uint, ctx ...context.Context) error
- func (q *QueryBuilder[T]) Chunk(size int, fn func([]T) error, ctx ...context.Context) error
- func (q *QueryBuilder[T]) Count(ctx ...context.Context) (int64, error)
- func (q *QueryBuilder[T]) Create(model *T, ctx ...context.Context) (*T, error)
- func (q *QueryBuilder[T]) CursorPaginate(ctx context.Context, column, cursor string, perPage int) (*CursorPaginated[T], error)
- func (q *QueryBuilder[T]) Delete(ctx ...context.Context) error
- func (q *QueryBuilder[T]) Detach(relation string, ownerID uint, relatedIDs []uint, ctx ...context.Context) error
- func (q *QueryBuilder[T]) Each(fn func(*T) error, ctx ...context.Context) error
- func (q *QueryBuilder[T]) Exists(ctx ...context.Context) (bool, error)
- func (q *QueryBuilder[T]) FindBy(column string, value any, ctx ...context.Context) (*T, error)
- func (q *QueryBuilder[T]) FindByID(id any, ctx ...context.Context) (*T, error)
- func (q *QueryBuilder[T]) First(ctx ...context.Context) (*T, error)
- func (q *QueryBuilder[T]) FirstOrCreate(attributes *T, ctx ...context.Context) (*T, bool, error)
- func (q *QueryBuilder[T]) ForceDelete(ctx ...context.Context) error
- func (q *QueryBuilder[T]) Get(ctx ...context.Context) ([]T, error)
- func (q *QueryBuilder[T]) GlobalScope(fn func(*QueryBuilder[T]) *QueryBuilder[T]) *QueryBuilder[T]
- func (q *QueryBuilder[T]) Last(ctx ...context.Context) (*T, error)
- func (q *QueryBuilder[T]) Limit(n int) *QueryBuilder[T]
- func (q *QueryBuilder[T]) LockForUpdate() *QueryBuilder[T]
- func (q *QueryBuilder[T]) Offset(n int) *QueryBuilder[T]
- func (q *QueryBuilder[T]) OrWhere(column, operator string, value any) *QueryBuilder[T]
- func (q *QueryBuilder[T]) OrderBy(column, direction string) *QueryBuilder[T]
- func (q *QueryBuilder[T]) Paginate(page, perPage int, ctx ...context.Context) (*PaginationResult[T], error)
- func (q *QueryBuilder[T]) Pluck(column string, ctx ...context.Context) ([]any, error)
- func (q *QueryBuilder[T]) Restore(ctx ...context.Context) error
- func (q *QueryBuilder[T]) Save(model *T, ctx ...context.Context) error
- func (q *QueryBuilder[T]) Scope(fn func(*QueryBuilder[T]) *QueryBuilder[T]) *QueryBuilder[T]
- func (q *QueryBuilder[T]) Sync(relation string, ownerID uint, relatedIDs []uint, ctx ...context.Context) error
- func (q *QueryBuilder[T]) Table(name string) *QueryBuilder[T]
- func (q *QueryBuilder[T]) ToSQL() (string, []any)
- func (q *QueryBuilder[T]) Update(data map[string]any, ctx ...context.Context) error
- func (q *QueryBuilder[T]) Where(column, operator string, value any) *QueryBuilder[T]
- func (q *QueryBuilder[T]) WhereIn(column string, values []any) *QueryBuilder[T]
- func (q *QueryBuilder[T]) WhereNotNull(column string) *QueryBuilder[T]
- func (q *QueryBuilder[T]) WhereNull(column string) *QueryBuilder[T]
- func (q *QueryBuilder[T]) WhereRaw(raw string, args ...any) *QueryBuilder[T]
- func (q *QueryBuilder[T]) With(relations ...string) *QueryBuilder[T]
- func (q *QueryBuilder[T]) WithBaseURL(url string) *QueryBuilder[T]
- func (q *QueryBuilder[T]) WithTrashed() *QueryBuilder[T]
- type QueryHook
- type RawQuery
- func (r *RawQuery[T]) All(ctx ...context.Context) iter.Seq2[*T, error]
- func (r *RawQuery[T]) Rows(ctx ...context.Context) (Rows, error)
- func (r *RawQuery[T]) Scan(dest any, ctx ...context.Context) error
- func (r *RawQuery[T]) ScanOne(dest any, ctx ...context.Context) error
- func (r *RawQuery[T]) ScanSlice(dest any, ctx ...context.Context) error
- type Relation
- type RelationMeta
- type Repository
- type Row
- type Rows
- type SQLiteDialect
- func (d SQLiteDialect) AdvisoryLock(id int64) string
- func (d SQLiteDialect) AdvisoryUnlock(id int64) string
- func (d SQLiteDialect) AutoIncrementDDL() string
- func (d SQLiteDialect) ConfigurePool(db *sql.DB)
- func (d SQLiteDialect) LimitOffsetSQL(limit, offset int) string
- func (d SQLiteDialect) Name() string
- func (d SQLiteDialect) Placeholder(n int) string
- func (d SQLiteDialect) QuoteIdentifier(name string) string
- func (d SQLiteDialect) SupportsReturning() bool
- func (d SQLiteDialect) UpsertSQL(table string, columns []string, conflict string) string
- type Seeder
- type SeederRunner
- type TableNamer
- type Transaction
Constants ¶
This section is empty.
Variables ¶
var AttachmentResolver func(disk, path string) (string, error)
AttachmentResolver allows the ORM to resolve attachment URLs without depending directly on the storage package.
var ( // DefaultRunner is the global seeder runner used by the CLI. DefaultRunner = NewSeederRunner() )
Functions ¶
func FakeAddress ¶
func FakeAddress() string
func FakeCompany ¶
func FakeCompany() string
func InitializeEncryption ¶
InitializeEncryption derives the 32-byte AES key from the application key. This should be called during application startup (e.g., by the ORM provider).
func ObservePool ¶
ObservePool registers OTel metrics for a database connection pool.
func Register ¶
func Register(seeders ...Seeder)
Register adds one or more seeders to the default global runner.
func RegisterModel ¶
RegisterModel manually adds metadata for a type, typically called by generated code.
func SetAttachmentResolver ¶
SetAttachmentResolver sets the global attachment resolver.
func TenantIDFromContext ¶
TenantIDFromContext retrieves the tenant ID from the context.
func WithContext ¶
WithContext returns a new context with the transaction DB instance attached.
Types ¶
type AfterCreateHook ¶
type AfterDeleteHook ¶
type AfterUpdateHook ¶
type AuditEntry ¶
type AuditEntry struct {
Action string
Table string
RecordID string
UserID string
Changes map[string]any
Timestamp int64
}
AuditEntry represents a single change record in the audit log.
type Auditable ¶
type Auditable interface {
AfterCreate(ctx context.Context, db *DB, model any) error
AfterUpdate(ctx context.Context, db *DB, model any) error
}
Auditable is an interface for models that support auditing.
type Auditor ¶
type Auditor interface {
Audit(ctx context.Context, entry AuditEntry) error
}
Auditor defines the interface for custom audit loggers.
type BaseRepository ¶
BaseRepository provides a generic implementation of the Repository interface.
func NewBaseRepository ¶
func NewBaseRepository[T any](db *DB) *BaseRepository[T]
NewBaseRepository creates a new BaseRepository for the given model type.
func (*BaseRepository[T]) Count ¶
func (r *BaseRepository[T]) Count(ctx context.Context) (int64, error)
Count returns the total number of records.
func (*BaseRepository[T]) Create ¶
func (r *BaseRepository[T]) Create(ctx context.Context, model *T) (*T, error)
Create inserts a new record into the database.
func (*BaseRepository[T]) Delete ¶
func (r *BaseRepository[T]) Delete(ctx context.Context, id any) error
Delete removes a record by its primary key.
func (*BaseRepository[T]) FindAll ¶
func (r *BaseRepository[T]) FindAll(ctx context.Context, page, perPage int) ([]T, error)
FindAll returns a slice of records, optionally paginated.
type BeforeCreateHook ¶
Lifecycle hook interfaces
type BeforeDeleteHook ¶
type BeforeUpdateHook ¶
type ColumnMeta ¶
type ColumnMeta struct {
FieldName string
ColumnName string
FieldIndex []int // replaces uintptr Offset — safe, GC-correct
IsPK bool
IsAuto bool
IsSoftDel bool
IsGuarded bool // Mass assignment protection
IsNullZero bool
Type reflect.Type
}
ColumnMeta holds metadata for a single column/field. FieldIndex is a multi-level index compatible with reflect.Value.FieldByIndex, which correctly handles embedded structs without any unsafe pointer arithmetic.
type Config ¶
type Config struct {
Driver string
DSN string
MaxOpen int
MaxIdle int
Lifetime time.Duration
SlowQueryThreshold time.Duration
Auditor Auditor
// Optional OpenTelemetry tracer. When set, all queries emit spans.
Tracer trace.Tracer
// LogQueries enables query logging to slog (development only).
LogQueries bool
// QueryHook, when set, is called after every SQL statement with the query text,
// bound arguments, and execution duration. Use this to feed the Astra Cockpit
// SQL Timeline without importing the core package.
QueryHook QueryHook
}
Config holds the database connection configuration.
type Connection ¶
type CursorPaginated ¶
type CursorPaginated[T any] struct { Data []T `json:"data"` NextCursor string `json:"next_cursor"` HasMore bool `json:"has_more"` }
CursorPaginated represents a cursor-based paginated result set
type CustomScanner ¶
CustomScanner allows types to control how they are scanned from the database.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB wraps a database connection
func FromContext ¶
FromContext retrieves the transaction DB instance from the context if it exists.
func MustOpen ¶
MustOpen calls Open and panics on error. Suitable for application startup where a database connection failure should be fatal.
func New ¶
func New(conn Connection, dialect Dialect) *DB
func NewStandalone ¶
NewStandalone establishes a database connection and returns an *DB. It is identical to Open() but explicitly documented as requiring no dependency on the engine.App framework struct. Suitable for:
- Standard net/http projects
- CLI tools that need ORM access
- Microservices that want only the ORM package
func (*DB) Begin ¶
func (db *DB) Begin(ctx context.Context) (Transaction, error)
Begin starts a new database transaction.
func (*DB) DropAllTables ¶
DropAllTables drops all tables in the current database. It handles foreign key constraints across different dialects.
func (*DB) Exec ¶
Exec executes a query without returning any rows. It automatically detects if a transaction is present in the context and uses it.
func (*DB) Query ¶
Query executes a query that returns rows. It automatically detects if a transaction is present in the context and uses it.
func (*DB) QueryRow ¶
QueryRow executes a query that is expected to return at most one row. It automatically detects if a transaction is present in the context and uses it.
func (*DB) SetQueryHook ¶
SetQueryHook sets a hook that is called after every query.
func (*DB) Transaction ¶
Transaction executes a function within a transaction. It automatically rolls back on error or panic, and commits on success. Supports nested transactions using SAVEPOINTs. The transaction-aware DB instance is injected into the context passed to fn.
func (*DB) WithTx ¶
func (db *DB) WithTx(tx Transaction) *DB
WithTx returns a new DB that runs queries inside the provided transaction. Use with a deferred rollback pattern to ensure cleanup:
tx, err := db.Begin(ctx)
if err != nil { ... }
defer tx.Rollback()
txDB := db.WithTx(tx)
// ... use txDB ...
tx.Commit()
type Dialect ¶
type Dialect interface {
Name() string
Placeholder(n int) string // $1 (postgres) vs ? (mysql/sqlite)
QuoteIdentifier(name string) string // "name" vs `name`
SupportsReturning() bool
AutoIncrementDDL() string
LimitOffsetSQL(limit, offset int) string
UpsertSQL(table string, columns []string, conflict string) string
AdvisoryLock(id int64) string
AdvisoryUnlock(id int64) string
ConfigurePool(db *sql.DB)
}
Dialect provides database-specific SQL generation
func ResolveDialect ¶
ResolveDialect returns the appropriate dialect and driver name for a given connection string.
type Encrypted ¶
type Encrypted[T any] struct { Val T }
Encrypted wraps a value with transparent crypto. Use this for sensitive PII like email, phone, or credentials.
type FactoryBuilder ¶
type FactoryBuilder[T any] struct { // contains filtered or unexported fields }
FactoryBuilder builds factory instances
func Factory ¶
func Factory[T any](fn func(*FactoryDef[T])) *FactoryBuilder[T]
Factory creates a new factory for type T
func (*FactoryBuilder[T]) Create ¶
func (fb *FactoryBuilder[T]) Create(ctx context.Context, db ...*DB) (*T, error)
Create creates and persists an instance
func (*FactoryBuilder[T]) CreateMany ¶
CreateMany creates and persists multiple instances
func (*FactoryBuilder[T]) Make ¶
func (fb *FactoryBuilder[T]) Make() T
Make creates an instance without persisting
func (*FactoryBuilder[T]) State ¶
func (fb *FactoryBuilder[T]) State(name string) *FactoryBuilder[T]
State applies a named state to a builder
func (*FactoryBuilder[T]) WithDB ¶
func (fb *FactoryBuilder[T]) WithDB(db *DB) *FactoryBuilder[T]
WithDB associates the factory with a database for persisting
type FactoryDef ¶
type FactoryDef[T any] struct { // contains filtered or unexported fields }
FactoryDef defines how to build a model
func (*FactoryDef[T]) Set ¶
func (f *FactoryDef[T]) Set(field string, value any)
Set sets a field value in the factory definition
type ManyToMany ¶
ManyToMany represents a N-to-N relationship via a pivot table.
func (*ManyToMany[T]) All ¶
func (r *ManyToMany[T]) All() []T
type Model ¶
type Model struct {
ID uint `orm:"primary_key;auto_increment" json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
DeletedAt *time.Time `orm:"soft_delete" json:"deleted_at,omitempty" db:"deleted_at"`
}
Model is a base struct for all Astra models, providing ID and Timestamps.
type ModelMeta ¶
type ModelMeta struct {
Type reflect.Type
TableName string
Columns []ColumnMeta
ColumnByCol map[string]ColumnMeta
PK ColumnMeta
HasSoftDel bool
Relations []RelationMeta
}
ModelMeta holds pre-computed metadata for a model.
type MySQLDialect ¶
type MySQLDialect struct{}
MySQLDialect implementation for MySQL
func (MySQLDialect) AdvisoryLock ¶
func (d MySQLDialect) AdvisoryLock(id int64) string
func (MySQLDialect) AdvisoryUnlock ¶
func (d MySQLDialect) AdvisoryUnlock(id int64) string
func (MySQLDialect) AutoIncrementDDL ¶
func (d MySQLDialect) AutoIncrementDDL() string
func (MySQLDialect) ConfigurePool ¶
func (d MySQLDialect) ConfigurePool(db *sql.DB)
func (MySQLDialect) LimitOffsetSQL ¶
func (d MySQLDialect) LimitOffsetSQL(limit, offset int) string
func (MySQLDialect) Name ¶
func (d MySQLDialect) Name() string
func (MySQLDialect) Placeholder ¶
func (d MySQLDialect) Placeholder(n int) string
func (MySQLDialect) QuoteIdentifier ¶
func (d MySQLDialect) QuoteIdentifier(name string) string
func (MySQLDialect) SupportsReturning ¶
func (d MySQLDialect) SupportsReturning() bool
type NeonDialect ¶
type NeonDialect struct {
PostgresDialect
}
NeonDialect extends PostgresDialect with serverless-specific optimizations. It handles Neon's "cold start" behavior by implementing automatic retries for common network errors that occur when compute units spin up.
func (NeonDialect) ConfigurePool ¶
func (d NeonDialect) ConfigurePool(db *sql.DB)
ConfigurePool applies Neon-specific connection pool settings. It optimizes for serverless environments by limiting total connections and reducing idle time to facilitate scale-to-zero.
type Paginated ¶
type Paginated[T any] struct { Data []T `json:"data"` Total int `json:"total"` Page int `json:"page"` PerPage int `json:"per_page"` LastPage int `json:"last_page"` }
Paginated represents a paginated result set (Legacy compatibility naming)
type PaginationResult ¶
type PaginationResult[T any] struct { Data []T `json:"data"` Total int64 `json:"total"` PerPage int `json:"per_page"` CurrentPage int `json:"current_page"` LastPage int `json:"last_page"` From int `json:"from"` To int `json:"to"` Links map[string]string `json:"links,omitempty"` }
PaginationResult holds the results of a paginated query
type PostgresDialect ¶
type PostgresDialect struct{}
PostgresDialect implementation for PostgreSQL
func (PostgresDialect) AdvisoryLock ¶
func (d PostgresDialect) AdvisoryLock(id int64) string
func (PostgresDialect) AdvisoryUnlock ¶
func (d PostgresDialect) AdvisoryUnlock(id int64) string
func (PostgresDialect) AutoIncrementDDL ¶
func (d PostgresDialect) AutoIncrementDDL() string
func (PostgresDialect) ConfigurePool ¶
func (d PostgresDialect) ConfigurePool(db *sql.DB)
func (PostgresDialect) LimitOffsetSQL ¶
func (d PostgresDialect) LimitOffsetSQL(limit, offset int) string
func (PostgresDialect) Name ¶
func (d PostgresDialect) Name() string
func (PostgresDialect) Placeholder ¶
func (d PostgresDialect) Placeholder(n int) string
func (PostgresDialect) QuoteIdentifier ¶
func (d PostgresDialect) QuoteIdentifier(name string) string
func (PostgresDialect) SupportsReturning ¶
func (d PostgresDialect) SupportsReturning() bool
type QueryBuilder ¶
type QueryBuilder[T any] struct { // contains filtered or unexported fields }
QueryBuilder is a generic fluent query builder.
func NewQueryBuilder ¶
func NewQueryBuilder[T any](db *DB) *QueryBuilder[T]
NewQueryBuilder creates a new instance for a model type.
func Query ¶
func Query[T any](db *DB, ctx ...context.Context) *QueryBuilder[T]
Query is the public entry point for the ORM. It automatically detects if a transaction is present in the context and uses it.
func (*QueryBuilder[T]) All ¶
All returns an iterator over the query results. Go 1.23+ iter.Seq2 style.
func (*QueryBuilder[T]) AllSlice ¶
func (q *QueryBuilder[T]) AllSlice(ctx ...context.Context) ([]T, error)
func (*QueryBuilder[T]) ApplyScopes ¶
func (q *QueryBuilder[T]) ApplyScopes() *QueryBuilder[T]
ApplyScopes runs all registered scopes before SQL generation.
func (*QueryBuilder[T]) Count ¶
func (q *QueryBuilder[T]) Count(ctx ...context.Context) (int64, error)
func (*QueryBuilder[T]) Create ¶
func (q *QueryBuilder[T]) Create(model *T, ctx ...context.Context) (*T, error)
func (*QueryBuilder[T]) CursorPaginate ¶
func (q *QueryBuilder[T]) CursorPaginate(ctx context.Context, column, cursor string, perPage int) (*CursorPaginated[T], error)
CursorPaginate performs cursor-based pagination.
func (*QueryBuilder[T]) Each ¶
func (q *QueryBuilder[T]) Each(fn func(*T) error, ctx ...context.Context) error
Each iterates over each result using a callback.
func (*QueryBuilder[T]) Exists ¶
func (q *QueryBuilder[T]) Exists(ctx ...context.Context) (bool, error)
func (*QueryBuilder[T]) FindByID ¶
func (q *QueryBuilder[T]) FindByID(id any, ctx ...context.Context) (*T, error)
func (*QueryBuilder[T]) FirstOrCreate ¶
func (q *QueryBuilder[T]) FirstOrCreate(attributes *T, ctx ...context.Context) (*T, bool, error)
func (*QueryBuilder[T]) ForceDelete ¶
func (q *QueryBuilder[T]) ForceDelete(ctx ...context.Context) error
func (*QueryBuilder[T]) GlobalScope ¶
func (q *QueryBuilder[T]) GlobalScope(fn func(*QueryBuilder[T]) *QueryBuilder[T]) *QueryBuilder[T]
GlobalScope adds a scope that is applied to all terminal operations.
func (*QueryBuilder[T]) Limit ¶
func (q *QueryBuilder[T]) Limit(n int) *QueryBuilder[T]
func (*QueryBuilder[T]) LockForUpdate ¶
func (q *QueryBuilder[T]) LockForUpdate() *QueryBuilder[T]
func (*QueryBuilder[T]) Offset ¶
func (q *QueryBuilder[T]) Offset(n int) *QueryBuilder[T]
func (*QueryBuilder[T]) OrWhere ¶
func (q *QueryBuilder[T]) OrWhere(column, operator string, value any) *QueryBuilder[T]
func (*QueryBuilder[T]) OrderBy ¶
func (q *QueryBuilder[T]) OrderBy(column, direction string) *QueryBuilder[T]
func (*QueryBuilder[T]) Paginate ¶
func (q *QueryBuilder[T]) Paginate(page, perPage int, ctx ...context.Context) (*PaginationResult[T], error)
func (*QueryBuilder[T]) Save ¶
func (q *QueryBuilder[T]) Save(model *T, ctx ...context.Context) error
func (*QueryBuilder[T]) Scope ¶
func (q *QueryBuilder[T]) Scope(fn func(*QueryBuilder[T]) *QueryBuilder[T]) *QueryBuilder[T]
func (*QueryBuilder[T]) Table ¶
func (q *QueryBuilder[T]) Table(name string) *QueryBuilder[T]
func (*QueryBuilder[T]) ToSQL ¶
func (q *QueryBuilder[T]) ToSQL() (string, []any)
ToSQL returns the SELECT query string and bound arguments.
func (*QueryBuilder[T]) Where ¶
func (q *QueryBuilder[T]) Where(column, operator string, value any) *QueryBuilder[T]
func (*QueryBuilder[T]) WhereIn ¶
func (q *QueryBuilder[T]) WhereIn(column string, values []any) *QueryBuilder[T]
func (*QueryBuilder[T]) WhereNotNull ¶
func (q *QueryBuilder[T]) WhereNotNull(column string) *QueryBuilder[T]
func (*QueryBuilder[T]) WhereNull ¶
func (q *QueryBuilder[T]) WhereNull(column string) *QueryBuilder[T]
func (*QueryBuilder[T]) WhereRaw ¶
func (q *QueryBuilder[T]) WhereRaw(raw string, args ...any) *QueryBuilder[T]
func (*QueryBuilder[T]) With ¶
func (q *QueryBuilder[T]) With(relations ...string) *QueryBuilder[T]
func (*QueryBuilder[T]) WithBaseURL ¶
func (q *QueryBuilder[T]) WithBaseURL(url string) *QueryBuilder[T]
WithBaseURL sets the base URL for generating pagination links.
func (*QueryBuilder[T]) WithTrashed ¶
func (q *QueryBuilder[T]) WithTrashed() *QueryBuilder[T]
type QueryHook ¶
QueryHook is called after every SQL statement with the query, bound arguments, and how long the statement took to execute. It is safe to call from multiple goroutines concurrently.
Use this hook to feed the Astra Cockpit SQL Timeline panel without importing the core package from within the ORM:
cfg.QueryHook = func(sql string, args []any, d time.Duration) {
dashboard.TrackQuery(sql, args, d)
}
type RawQuery ¶
type RawQuery[T any] struct { // contains filtered or unexported fields }
func (*RawQuery[T]) Scan ¶
Scan scans raw SQL rows into dest. dest must be a *[]T where T is a struct, or a *T for a single row.
type Relation ¶
type Relation[T any] struct { // contains filtered or unexported fields }
Relation is the base for all relationship wrappers.
type RelationMeta ¶
type RelationMeta struct {
FieldName string
Type string // "has_one", "has_many", "belongs_to", "many_to_many"
Related reflect.Type
FK string
RelatedKey string // FK on the related side for many_to_many
Pivot string // pivot table name
MorphType string // for polymorphic relations
MorphID string // for polymorphic relations
}
RelationMeta holds metadata for a model relation.
type Repository ¶
type Repository[T any] interface { FindByID(ctx context.Context, id any) (*T, error) FindAll(ctx context.Context, page, perPage int) ([]T, error) Create(ctx context.Context, model *T) (*T, error) Update(ctx context.Context, model *T) error Delete(ctx context.Context, id any) error Count(ctx context.Context) (int64, error) }
Repository defines the standard interface for Astra repositories.
type SQLiteDialect ¶
type SQLiteDialect struct{}
SQLiteDialect implementation for SQLite
func (SQLiteDialect) AdvisoryLock ¶
func (d SQLiteDialect) AdvisoryLock(id int64) string
func (SQLiteDialect) AdvisoryUnlock ¶
func (d SQLiteDialect) AdvisoryUnlock(id int64) string
func (SQLiteDialect) AutoIncrementDDL ¶
func (d SQLiteDialect) AutoIncrementDDL() string
func (SQLiteDialect) ConfigurePool ¶
func (d SQLiteDialect) ConfigurePool(db *sql.DB)
func (SQLiteDialect) LimitOffsetSQL ¶
func (d SQLiteDialect) LimitOffsetSQL(limit, offset int) string
func (SQLiteDialect) Name ¶
func (d SQLiteDialect) Name() string
func (SQLiteDialect) Placeholder ¶
func (d SQLiteDialect) Placeholder(n int) string
func (SQLiteDialect) QuoteIdentifier ¶
func (d SQLiteDialect) QuoteIdentifier(name string) string
func (SQLiteDialect) SupportsReturning ¶
func (d SQLiteDialect) SupportsReturning() bool
type Seeder ¶
type Seeder interface {
// Name returns the unique name of this seeder (e.g. "01_users").
Name() string
// Run executes the seeder, inserting or upserting seed data.
Run(ctx context.Context, db *DB) error
}
Seeder defines the interface that all seeders must implement.
type SeederRunner ¶
type SeederRunner struct {
// contains filtered or unexported fields
}
SeederRunner manages and executes registered seeders.
func NewSeederRunner ¶
func NewSeederRunner() *SeederRunner
NewSeederRunner creates a new SeederRunner.
func (*SeederRunner) Names ¶
func (r *SeederRunner) Names() []string
Names returns all registered seeder names, sorted alphabetically.
func (*SeederRunner) Register ¶
func (r *SeederRunner) Register(seeders ...Seeder)
Register adds one or more seeders to the runner in order.
type TableNamer ¶
type TableNamer interface {
TableName() string
}
TableNamer allows models to specify a custom table name.
type Transaction ¶
type Transaction interface {
Connection
Commit() error
Rollback() error
}