Documentation
¶
Overview ¶
Package orm provides the runtime ORM for querying and mutating application models.
Index ¶
- Variables
- type AccessPolicyEvent
- type AccessPolicyEventKind
- type AfterCreateHook
- type AfterDeleteHook
- type AfterFindHook
- type AfterUpdateHook
- type Attribute
- type AuditAction
- type BeforeCreateHook
- type BeforeDeleteHook
- type BeforeFindHook
- type BeforeUpdateHook
- type Config
- type Counter
- type DB
- func (db *DB) Begin(ctx context.Context) (*DB, error)
- func (db *DB) Close() error
- func (db *DB) Commit() error
- func (db *DB) Count(model any, opts ...QueryOption) (int64, error)
- func (db *DB) Create(model any) error
- func (db *DB) CreateMany(ctx context.Context, models any) error
- func (db *DB) Delete(model any) error
- func (db *DB) DeleteMany(ctx context.Context, models any) error
- func (db *DB) Dialect() dialect.Dialect
- func (db *DB) DriverName() string
- func (db *DB) DryRun() *DryRunSession
- func (db *DB) Exists(model any, opts ...QueryOption) (bool, error)
- func (db *DB) Find(dest any, opts ...QueryOption) error
- func (db *DB) FindOne(dest any, opts ...QueryOption) error
- func (db *DB) Observability() ObservabilityConfig
- func (db *DB) Ping(ctx context.Context) error
- func (db *DB) PingContext(ctx context.Context) error
- func (db *DB) Raw(ctx context.Context, query string, args ...any) *RawBuilder
- func (db *DB) Rollback() error
- func (db *DB) SQLDB() *sql.DB
- func (db *DB) SoftDelete(model any) error
- func (db *DB) Stats() sql.DBStats
- func (db *DB) Transaction(ctx context.Context, fn func(*DB) error) error
- func (db *DB) Tx(ctx context.Context, fn func(*Session) error) error
- func (db *DB) Update(model any) error
- func (db *DB) UpdateMany(ctx context.Context, models any) error
- func (db *DB) UpdateWhere(model any, opts ...QueryOption) error
- func (db *DB) Upsert(model any) error
- func (db *DB) WithContext(ctx context.Context) *Session
- func (db *DB) WithPolicy(policy access.Policy) *Session
- type DryRunSession
- func (d *DryRunSession) Count(ctx context.Context, model any, opts ...QueryOption) (ExecutionReport, int64, error)
- func (d *DryRunSession) Create(ctx context.Context, model any) (ExecutionReport, error)
- func (d *DryRunSession) Delete(ctx context.Context, model any) (ExecutionReport, error)
- func (d *DryRunSession) Find(ctx context.Context, dest any, opts ...QueryOption) (ExecutionReport, error)
- func (d *DryRunSession) FindOne(ctx context.Context, dest any, opts ...QueryOption) (ExecutionReport, error)
- func (d *DryRunSession) SoftDelete(ctx context.Context, model any) (ExecutionReport, error)
- func (d *DryRunSession) Update(ctx context.Context, model any) (ExecutionReport, error)
- func (d *DryRunSession) UpdateWhere(ctx context.Context, model any, opts ...QueryOption) (ExecutionReport, error)
- func (d *DryRunSession) Upsert(ctx context.Context, model any) (ExecutionReport, error)
- type ExecutionReport
- type ExecutionStatement
- type ExecutionStatus
- type Histogram
- type LifecycleHookEvent
- type Logger
- type Meter
- type MeterProvider
- type Observability
- type ObservabilityConfig
- type ObservabilityLogger
- type OptimisticLockingInfo
- type QueryAdvisor
- type QueryAdvisorFinding
- type QueryAdvisorInput
- type QueryAdvisorReport
- type QueryOption
- func CrossJoin(table string) QueryOption
- func Distinct() QueryOption
- func GroupBy(expr string) QueryOption
- func Having(expr string, args ...any) QueryOption
- func InnerJoin(table, on string, args ...any) QueryOption
- func LeftJoin(table, on string, args ...any) QueryOption
- func Limit(n int) QueryOption
- func Offset(n int) QueryOption
- func OrderBy(expr string) QueryOption
- func RightJoin(table, on string, args ...any) QueryOption
- func Select(expr string) QueryOption
- func Where(expr string, args ...any) QueryOption
- type RawBuilder
- type SQLLogEntry
- type SQLLogMode
- type Session
- func (s *Session) Count(model any, opts ...QueryOption) (int64, error)
- func (s *Session) Create(model any) error
- func (s *Session) CreateMany(models any) error
- func (s *Session) Delete(model any) error
- func (s *Session) DeleteMany(models any) error
- func (s *Session) Dialect() dialect.Dialect
- func (s *Session) DriverName() string
- func (s *Session) DryRun() *DryRunSession
- func (s *Session) Exists(model any, opts ...QueryOption) (bool, error)
- func (s *Session) Find(dest any, opts ...QueryOption) error
- func (s *Session) FindOne(dest any, opts ...QueryOption) error
- func (s *Session) Load(dest any, relation string) error
- func (s *Session) Observability() ObservabilityConfig
- func (s *Session) Preload(dest any, relations ...string) error
- func (s *Session) Raw(ctx context.Context, query string, args ...any) *RawBuilder
- func (s *Session) SoftDelete(model any) error
- func (s *Session) Tx(fn func(*Session) error) error
- func (s *Session) Update(model any) error
- func (s *Session) UpdateMany(models any) error
- func (s *Session) UpdateWhere(model any, opts ...QueryOption) error
- func (s *Session) Upsert(model any) error
- func (s *Session) WithContext(ctx context.Context) *Session
- func (s *Session) WithDeleted() *Session
- func (s *Session) WithPolicy(policy access.Policy) *Session
- type Span
- type SpanOption
- type TraceSQLMode
- type Tracer
- type TracerProvider
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = dormerrors.ErrNotFound
Functions ¶
This section is empty.
Types ¶
type AccessPolicyEvent ¶ added in v0.4.0
type AccessPolicyEvent struct {
Kind AccessPolicyEventKind
Field string
SQL string
Arguments []any
Policy string
Description string
}
AccessPolicyEvent describes a security-related policy action.
type AccessPolicyEventKind ¶ added in v0.4.0
type AccessPolicyEventKind string
AccessPolicyEventKind classifies an access-policy action in the execution report.
const ( AccessPolicyEventInjectedPredicate AccessPolicyEventKind = "injected_predicate" AccessPolicyEventInjectedField AccessPolicyEventKind = "injected_field" AccessPolicyEventInheritedPolicy AccessPolicyEventKind = "inherited_policy" AccessPolicyEventPolicyOverride AccessPolicyEventKind = "policy_override" AccessPolicyEventSoftDelete AccessPolicyEventKind = "soft_delete" )
type AfterCreateHook ¶ added in v0.1.1
type AfterDeleteHook ¶ added in v0.1.1
type AfterFindHook ¶ added in v0.1.1
type AfterUpdateHook ¶ added in v0.1.1
type AuditAction ¶ added in v0.4.0
AuditAction describes an automatic audit field assignment.
type BeforeCreateHook ¶ added in v0.1.1
type BeforeDeleteHook ¶ added in v0.1.1
type BeforeFindHook ¶ added in v0.1.1
type BeforeUpdateHook ¶ added in v0.1.1
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
func (*DB) Count ¶ added in v0.3.0
func (db *DB) Count(model any, opts ...QueryOption) (int64, error)
func (*DB) CreateMany ¶ added in v0.3.1
func (*DB) DeleteMany ¶ added in v0.3.1
func (*DB) DriverName ¶ added in v0.4.4
DriverName returns the configured driver name.
func (*DB) DryRun ¶ added in v0.4.0
func (db *DB) DryRun() *DryRunSession
func (*DB) Exists ¶ added in v0.4.4
func (db *DB) Exists(model any, opts ...QueryOption) (bool, error)
func (*DB) Observability ¶ added in v0.4.4
func (db *DB) Observability() ObservabilityConfig
Observability returns the current observability configuration.
func (*DB) SoftDelete ¶ added in v0.3.0
func (*DB) Transaction ¶ added in v0.3.0
func (*DB) UpdateMany ¶ added in v0.3.1
func (*DB) UpdateWhere ¶ added in v0.3.0
func (db *DB) UpdateWhere(model any, opts ...QueryOption) error
type DryRunSession ¶ added in v0.4.0
type DryRunSession struct {
// contains filtered or unexported fields
}
func (*DryRunSession) Count ¶ added in v0.4.0
func (d *DryRunSession) Count(ctx context.Context, model any, opts ...QueryOption) (ExecutionReport, int64, error)
func (*DryRunSession) Create ¶ added in v0.4.0
func (d *DryRunSession) Create(ctx context.Context, model any) (ExecutionReport, error)
func (*DryRunSession) Delete ¶ added in v0.4.0
func (d *DryRunSession) Delete(ctx context.Context, model any) (ExecutionReport, error)
func (*DryRunSession) Find ¶ added in v0.4.0
func (d *DryRunSession) Find(ctx context.Context, dest any, opts ...QueryOption) (ExecutionReport, error)
func (*DryRunSession) FindOne ¶ added in v0.4.0
func (d *DryRunSession) FindOne(ctx context.Context, dest any, opts ...QueryOption) (ExecutionReport, error)
func (*DryRunSession) SoftDelete ¶ added in v0.4.0
func (d *DryRunSession) SoftDelete(ctx context.Context, model any) (ExecutionReport, error)
func (*DryRunSession) Update ¶ added in v0.4.0
func (d *DryRunSession) Update(ctx context.Context, model any) (ExecutionReport, error)
func (*DryRunSession) UpdateWhere ¶ added in v0.4.0
func (d *DryRunSession) UpdateWhere(ctx context.Context, model any, opts ...QueryOption) (ExecutionReport, error)
func (*DryRunSession) Upsert ¶ added in v0.4.0
func (d *DryRunSession) Upsert(ctx context.Context, model any) (ExecutionReport, error)
type ExecutionReport ¶ added in v0.4.0
type ExecutionReport struct {
Version int
Operation string
Table string
SQL string
Parameters []any
Statements []ExecutionStatement
AccessPolicies []AccessPolicyEvent
AuditActions []AuditAction
LifecycleHooks []LifecycleHookEvent
QueryAdvisor []QueryAdvisorFinding
OptimisticLocking *OptimisticLockingInfo
ExecutionStatus ExecutionStatus
Metadata map[string]any
}
ExecutionReport captures the observable behavior of an ORM operation.
type ExecutionStatement ¶ added in v0.4.0
ExecutionStatement captures a single generated SQL statement.
type ExecutionStatus ¶ added in v0.4.0
type ExecutionStatus string
ExecutionStatus describes what happened at the terminal execution step.
const ( // ExecutionStatusSkipped indicates that SQL execution was intentionally skipped. ExecutionStatusSkipped ExecutionStatus = "Skipped" )
type LifecycleHookEvent ¶ added in v0.4.0
LifecycleHookEvent describes a lifecycle hook invocation.
type MeterProvider ¶
MeterProvider creates named meters.
type Observability ¶ added in v0.1.10
type Observability = ObservabilityConfig
Observability is an alias kept for the public API documented in ADR-026.
type ObservabilityConfig ¶
type ObservabilityConfig struct {
Tracing bool
Metrics bool
TraceSQL TraceSQLMode
SQLLogging SQLLogMode
SlowQueryThreshold time.Duration
MaskParameters bool
MaskedFields []string
Logger ObservabilityLogger
TracerProvider TracerProvider
MeterProvider MeterProvider
}
ObservabilityConfig configures tracing, metrics, and SQL visibility.
func DefaultObservabilityConfig ¶
func DefaultObservabilityConfig() ObservabilityConfig
DefaultObservabilityConfig returns the default observability configuration.
func (ObservabilityConfig) Enabled ¶
func (c ObservabilityConfig) Enabled() bool
Enabled reports whether any observability feature is active.
func (ObservabilityConfig) Normalized ¶
func (c ObservabilityConfig) Normalized() ObservabilityConfig
Normalized returns a copy with defaults applied.
func (ObservabilityConfig) ShouldMask ¶
func (c ObservabilityConfig) ShouldMask(field string) bool
ShouldMask reports whether a field name should be redacted in logs.
func (ObservabilityConfig) Validate ¶
func (c ObservabilityConfig) Validate() error
Validate checks that the configuration is internally consistent.
type ObservabilityLogger ¶
type ObservabilityLogger interface {
LogSQL(context.Context, SQLLogEntry)
LogError(context.Context, error)
}
ObservabilityLogger receives SQL logs and error events.
type OptimisticLockingInfo ¶ added in v0.4.1
OptimisticLockingInfo describes optimistic-lock metadata for inspection and tracing.
type QueryAdvisor ¶ added in v0.4.0
type QueryAdvisor interface {
Inspect(context.Context, QueryAdvisorInput) (QueryAdvisorReport, error)
}
QueryAdvisor evaluates a generated statement and returns advisory findings.
type QueryAdvisorFinding ¶ added in v0.4.0
type QueryAdvisorFinding struct {
Code string
Rule string
Severity string
Title string
Details string
Recommendation string
Table string
Columns []string
}
QueryAdvisorFinding describes a recommendation from the query advisor.
type QueryAdvisorInput ¶ added in v0.4.0
QueryAdvisorInput describes a query advisor request.
type QueryAdvisorReport ¶ added in v0.4.0
type QueryAdvisorReport struct {
Table string
SQL string
Kind string
Findings []QueryAdvisorFinding
}
QueryAdvisorReport captures query advisor output in a neutral form.
type QueryOption ¶
type QueryOption func(*queryState)
func CrossJoin ¶ added in v0.4.4
func CrossJoin(table string) QueryOption
func Distinct ¶ added in v0.4.4
func Distinct() QueryOption
func GroupBy ¶ added in v0.4.4
func GroupBy(expr string) QueryOption
func Having ¶ added in v0.4.4
func Having(expr string, args ...any) QueryOption
func InnerJoin ¶ added in v0.4.4
func InnerJoin(table, on string, args ...any) QueryOption
func LeftJoin ¶ added in v0.4.4
func LeftJoin(table, on string, args ...any) QueryOption
func Limit ¶
func Limit(n int) QueryOption
func Offset ¶
func Offset(n int) QueryOption
func OrderBy ¶
func OrderBy(expr string) QueryOption
func RightJoin ¶ added in v0.4.4
func RightJoin(table, on string, args ...any) QueryOption
func Select ¶ added in v0.4.4
func Select(expr string) QueryOption
func Where ¶
func Where(expr string, args ...any) QueryOption
type RawBuilder ¶ added in v0.3.2
type RawBuilder struct {
// contains filtered or unexported fields
}
RawBuilder executes explicit SQL with an opt-in policy bypass.
func (*RawBuilder) Exec ¶ added in v0.3.2
func (r *RawBuilder) Exec() (sql.Result, error)
Exec executes a Raw SQL statement and returns the driver result.
func (*RawBuilder) Scan ¶ added in v0.3.2
func (r *RawBuilder) Scan(dest any) error
Scan executes a Raw SQL query and scans the result into a destination slice.
func (*RawBuilder) WithoutPolicy ¶ added in v0.3.2
func (r *RawBuilder) WithoutPolicy() *RawBuilder
WithoutPolicy explicitly acknowledges that Raw SQL bypasses access policy.
type SQLLogEntry ¶
type SQLLogEntry struct {
SQL string
Args []any
Duration time.Duration
AffectedRows int64
Timestamp time.Time
Err error
Slow bool
Visibility TraceSQLMode
Driver string
Dialect string
Operation string
Table string
}
SQLLogEntry captures a single SQL log event.
type SQLLogMode ¶
type SQLLogMode string
SQLLogMode controls how SQL statements are reported by the legacy logger path.
const ( SQLLogDisabled SQLLogMode = "disabled" SQLLogErrorsOnly SQLLogMode = "errors_only" SQLLogSlow SQLLogMode = "slow_queries" SQLLogDebug SQLLogMode = "debug" SQLLogTrace SQLLogMode = "trace" )
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
func (*Session) Count ¶ added in v0.1.8
func (s *Session) Count(model any, opts ...QueryOption) (int64, error)
Count returns the number of rows matching the model type and query options.
func (*Session) CreateMany ¶ added in v0.3.1
func (*Session) DeleteMany ¶ added in v0.3.1
func (*Session) DriverName ¶ added in v0.4.4
DriverName returns the configured driver name.
func (*Session) DryRun ¶ added in v0.4.0
func (s *Session) DryRun() *DryRunSession
func (*Session) Exists ¶ added in v0.4.4
func (s *Session) Exists(model any, opts ...QueryOption) (bool, error)
Exists reports whether any row matches the query options.
func (*Session) Observability ¶ added in v0.4.4
func (s *Session) Observability() ObservabilityConfig
Observability returns the underlying database observability configuration.
func (*Session) SoftDelete ¶
func (*Session) UpdateMany ¶ added in v0.3.1
func (*Session) UpdateWhere ¶ added in v0.1.9
func (s *Session) UpdateWhere(model any, opts ...QueryOption) error
UpdateWhere updates rows matching explicit query filters.
func (*Session) WithContext ¶ added in v0.1.1
func (*Session) WithDeleted ¶
type SpanOption ¶
type SpanOption interface {
// contains filtered or unexported methods
}
SpanOption configures span creation.
type TraceSQLMode ¶ added in v0.1.10
type TraceSQLMode string
TraceSQLMode controls how much SQL detail is exposed through traces.
const ( TraceSQLDisabled TraceSQLMode = "disabled" TraceSQLMetadata TraceSQLMode = "metadata" TraceSQLStatement TraceSQLMode = "statement" TraceSQLStatementWithArgs TraceSQLMode = "statement_with_args" )
type TracerProvider ¶
TracerProvider creates named tracers.