Query
public struct Query<T> where T : Decodable, T : Encodable
A generic structure representing a query for a specific type of data.
This structure is used to build and execute queries on a collection of data stored in a database. It supports filtering data using predicates and key path predicates, and relies on a storage engine and query planner for execution.
Properties:
- collection: The name of the collection being queried.
- predicates: A list of field-based predicates used to filter the data.
- storage: The storage engine responsible for managing the data.
- planner: The query planner responsible for optimizing and executing the query.
- keyPathPredicates: A list of key path-based predicates used to filter the data.
Parameters
T
|
The type of the data being queried. Must conform to |
-
Initializes a new instance of the
QueryEngine.Declaration
Swift
public init( collection: String, storage: StorageEngine, indexStats: [String: IndexStat], shardStats: [ShardStat] )Parameters
collectionThe name of the collection to be queried.
storageThe storage engine responsible for managing data persistence.
indexStatsA dictionary containing statistics for each index, where the key is the index name and the value is an
IndexStatobject.shardStatsAn array of
ShardStatobjects representing statistics for each shard. -
Filters the query results based on the specified key path and query operator.
Note
This method is mutating, meaning it modifies the state of the query engine.
Declaration
Swift
public mutating func `where`<V: Hashable>( _ keyPath: KeyPath<T, V>, _ op: QueryOperator )Parameters
keyPathA key path to the property of the model
Tthat will be used for filtering.opThe query operator that defines the condition to apply to the specified key path.
-
Generates and returns an execution plan for the current query.
This method provides a detailed explanation of how the query will be executed, including information about the steps involved and any optimizations applied.
Declaration
Swift
public func explain() -> ExecutionPlanReturn Value
An
ExecutionPlanobject representing the detailed execution strategy for the query. -
execute()AsynchronousExecutes the query asynchronously and returns an array of results.
Throws
An error if the query execution fails.Note
This method is asynchronous and must be awaited.Declaration
Swift
public func execute() async throws -> [T]Return Value
An array of results of type
T. -
Fetches a stream of elements from the specified storage engine.
This method returns an
AsyncThrowingStreamthat allows asynchronous iteration over elements of typeTretrieved from the providedStorageEngine.Throws
An error if the stream encounters an issue during fetching.Declaration
Swift
public func fetchStream(from storage: StorageEngine) -> AsyncThrowingStream<T, Error>Parameters
storageThe
StorageEngineinstance from which the elements will be fetched.Return Value
An
AsyncThrowingStreamof typeTthat can throw an error during iteration.
View on GitHub