QueryContext class

The central hub for interacting with the ORM.

A QueryContext provides access to model definitions, database drivers, and query builders. It is typically created once per application and passed down to parts of the application that need to perform database operations.

Implemented types

Constructors

QueryContext({required ModelRegistry registry, required DriverAdapter driver, ValueCodecRegistry? codecRegistry, ScopeRegistry? scopeRegistry, EventBus? events, String? connectionName, String? connectionDatabase, String? connectionTablePrefix, QueryHook? beforeQueryHook, MutationHook? beforeMutationHook, TransactionHook? beforeTransactionHook, TransactionHook? afterTransactionHook, QueryLogHook? queryLogHook, bool pretendResolver()?})
The central hub for interacting with the ORM.

Properties

codecRegistry ValueCodecRegistry
The registry for custom field and value codecs.
final
connectionDatabase String?
The name of the database.
final
connectionName String?
The name of the database connection.
final
connectionTablePrefix String?
The prefix applied to table names.
final
driver DriverAdapter
The underlying database driver used by this context.
final
events EventBus
Event bus used for emitting model lifecycle events.
final
hashCode int
The hash code for this object.
no setterinherited
queryCache QueryCache
The query result cache.
final
queryCacheStats CacheStats
Get query cache statistics.
no setter
registry ModelRegistry
The model registry containing all model definitions.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scopeRegistry ScopeRegistry
The registry for global and local query scopes.
final

Methods

attachRuntimeMetadata(Object? model) → void
Attaches runtime metadata to a model instance.
beforeExecuting(ExecutingStatementCallback callback) → void Function()
Registers a callback that is invoked before any statement is executed.
describeMutation(MutationPlan plan) StatementPreview
Returns the statement preview for plan without mutating the database.
override
describeQuery(QueryPlan plan) StatementPreview
Returns the statement preview for plan without hitting the driver.
override
flushQueryCache() → void
Clear all cached query results.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onMutation(void listener(MutationEvent event)) → void
Registers a listener that receives MutationEvent payloads.
onQuery(void listener(QueryEvent event)) → void
Registers a listener that receives QueryEvent payloads.
query<T extends OrmEntity>() Query<T>
Returns a Query bound to the registered ModelDefinition for T.
queryFromDefinition<T extends OrmEntity>(ModelDefinition<T> definition, {String? table, String? schema, String? alias, List<String>? scopes}) Query<T>
Returns a Query for a given ModelDefinition.
registerGlobalScope<T extends OrmEntity>(String identifier, GlobalScopeCallback<T> scope) → void
Registers a global scope for models of type T.
registerGlobalScopePattern<T extends OrmEntity>(String identifier, GlobalScopeCallback<T> scope, {String pattern = '*'}) → void
Registers a global scope pattern for models of type T.
registerLocalScope<T extends OrmEntity>(String name, LocalScopeCallback<T> scope) → void
Registers a local scope for models of type T.
registerLocalScopePattern<T extends OrmEntity>(String name, LocalScopeCallback<T> scope, {String pattern = '*'}) → void
Registers a local scope pattern for models of type T.
registerMacro(String name, QueryMacroCallback macro) → void
Registers a query macro.
repository<T extends OrmEntity>() Repository<T>
Returns a Repository that emits events through this context.
runMutation(MutationPlan plan) Future<MutationResult>
Executes plan while emitting a MutationEvent.
override
runSelect(QueryPlan plan) Future<List<Map<String, Object?>>>
Executes plan while emitting a QueryEvent.
override
streamSelect(QueryPlan plan) Stream<Map<String, Object?>>
Streams rows for plan while emitting a QueryEvent.
table(String table, {String? as, String? schema, List<String>? scopes, List<AdHocColumn> columns = const []}) Query<AdHocRow>
Starts a query builder targeting a table by name.
threadCount() Future<int?>
Returns the number of open database connections, when supported by the driver.
toString() String
A string representation of this object.
inherited
transaction<R>(Future<R> callback()) Future<R>
Executes a database transaction.
vacuumQueryCache() → void
Remove expired cache entries (automatic cleanup).
whenQueryingForLongerThan(Duration threshold, LongRunningQueryCallback callback) → void Function()
Registers a callback that is invoked when a query runs for longer than threshold.

Operators

operator ==(Object other) bool
The equality operator.
inherited