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 Codable.

  • Initializes a new instance of the QueryEngine.

    Declaration

    Swift

    public init(
        collection: String,
        storage: StorageEngine,
        indexStats: [String: IndexStat],
        shardStats: [ShardStat]
    )

    Parameters

    collection

    The name of the collection to be queried.

    storage

    The storage engine responsible for managing data persistence.

    indexStats

    A dictionary containing statistics for each index, where the key is the index name and the value is an IndexStat object.

    shardStats

    An array of ShardStat objects 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

    keyPath

    A key path to the property of the model T that will be used for filtering.

    op

    The 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() -> ExecutionPlan

    Return Value

    An ExecutionPlan object representing the detailed execution strategy for the query.

  • execute() Asynchronous

    Executes 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 AsyncThrowingStream that allows asynchronous iteration over elements of type T retrieved from the provided StorageEngine.

    Throws

    An error if the stream encounters an issue during fetching.

    Declaration

    Swift

    public func fetchStream(from storage: StorageEngine) -> AsyncThrowingStream<T, Error>

    Parameters

    storage

    The StorageEngine instance from which the elements will be fetched.

    Return Value

    An AsyncThrowingStream of type T that can throw an error during iteration.