/** * Implements a fluent query API for querying entities in a database. * * @packageDocumentation */ import { Newable } from "ts-essentials"; import { QueryCache } from "../uow.js"; import { Brand } from "../utils/utils.js"; import { Entities, From, QueryOptions } from "../verse.js"; import { QueryCompiler } from "./compiler.js"; import { Expression } from "./parser.js"; declare global { interface String { /** * Query expression analog of the SQL LIKE operator. * * @param pattern The pattern to match. * @returns `true` if the string matches the pattern, otherwise `false`. * * @example * const q = db.from.albums.where(a => a.title.like("M%")); */ like(pattern: string): boolean; } interface Array { /** * Used to specify eager loading of related entities in queries. * * @param navigation The navigation property to be loaded. * @returns The queryable result of the eager loading operation. * * @example * const q = * db.from.genres * .with(a => a.tracks.with(t => t.album)) * .where(g => g.name === "Jazz"); */ with(navigation: (obj: T) => S): S; } } /** * Represents a grouping of objects based on a key within a query expression. * * See {@link Queryable.groupBy} for more information. * * @template K - The type of the key. * @template T - The type of the objects being grouped. */ export interface Grouping { /** * The key of the grouping. */ readonly key: K; /** * Returns the count of elements in the group. * * @return The count of elements in the group. */ count(): number; /** * Returns the minimum value of the specified expression for the group. * * @param expr The expression to be evaluated. * @return The minimum value of the specified expression for the group. */ min(expr: Expr<(obj: T) => number>): number; /** * Returns the maximum value of the specified expression for the group. * * @param expr The expression to be evaluated. * @return The maximum value of the specified expression for the group. */ max(expr: Expr<(obj: T) => number>): number; /** * Returns the sum of the specified expression for the group. * * @param expr The expression to be evaluated. * @return The sum of the specified expression for the group. */ sum(expr: Expr<(obj: T) => number>): number; /** * Returns the average value of the specified expression for the group. * * @param expr The expression to be evaluated. * @return The average value of the specified expression for the group. */ avg(expr: Expr<(obj: T) => number>): number; /** * Returns an array of the objects in the group. * * @return An array of the objects in the group. */ array(expr?: Expr<(obj: T) => S>): S[]; } /** @ignore */ export declare const __expr: unique symbol; /** * Represents an asynchronous sequence of elements. * * @template T The type of elements in the sequence. */ export interface AsyncSequence extends AsyncIterable { /** * Buffers the sequence into an array of type T. * * @return A promise that resolves with an array of type T. */ toArray(): Promise; } type Expr = E extends (...args: readonly [infer H, ...infer T]) => infer R ? H extends JoinResult ? (...args: readonly [...K, ...T]) => R : E : E; /** * Represents the result of a join operation. */ export type JoinResult = Brand; /** * Represents a join condition used when joining two sequences. */ export type JoinCondition = Expr<(left: T, right: S) => boolean>; /** * Represents a predicate expression used to filter a sequence. */ export type AsyncPredicateExpr = Expr<(obj: T, ...args: readonly [...A, From]) => boolean>; export type PredicateExpr = Expr<(obj: T, ...args: A) => boolean>; /** * Represents a numeric expression used in an aggregate query. */ export type NumericExpr = Expr<(obj: T) => number>; /** * Fluent query operators used when constructing compiled queries. * * A compiled query can be created using the {@link Verse.compile} or {@link Verse.compileUow} methods. * * @template T The type of the entity being queried. * * @example * const query = db.compile((from, $name: string) => * from.customers.where(c => c.name === $name).single()); * * const customer = await query("ACME Corp."); */ export declare class Queryable { /** * Projects each element of a sequence into a new form. * * @param projector A function that transforms the input element. * @returns The queryable result of the select operation. */ select

unknown>>(projector: P): Queryable>; /** * Used to specify eager loading of related entities in queries. * * @param navigation The navigation property to be loaded. * @returns The queryable result of the with operation. */ with(navigation: Expr<(obj: T) => S>): this; /** * Removes duplicate elements from the sequence. * * @returns The queryable result of the distinct operation. */ distinct(): this; /** * Joins the sequence with another sequence based on a condition. * * @param entityOrQuery The entity or query to join with. * @param condition The join condition. * @returns The queryable result of the join operation. */ join>(entityOrQuery: Newable | Queryable, condition: C): Queryable>>; /** * Left joins the sequence with another sequence based on a condition. * * @param entityOrQuery The entity or query to join with. * @param condition The join condition. * @returns The queryable result of the join operation. */ leftJoin>(entityOrQuery: Newable | Queryable, condition: C): Queryable>>; /** * Filters a sequence of values based on a predicate. * * @param predicate A function to test each element for a condition. * @returns The queryable result of the where operation. */ where

>(predicate: P): this; /** * Sorts the elements of a sequence in ascending order. * * @param expr A function to extract a sort key from an element. * @returns The queryable result of the orderBy operation. */ orderBy unknown>>(expr: E): this; /** * Sorts the elements of a sequence in descending order. * * @param expr A function to extract a sort key from an element. * @returns The queryable result of the orderByDesc operation. */ orderByDesc unknown>>(expr: E): this; /** * Groups the elements of a sequence according to a specified key selector function. * * @param key A function to extract the key for each element. * @returns The result of the groupBy operation. */ groupBy unknown>>(key: K): Queryable<{ key: ReturnType; items: T[]; }>; /** * Groups the elements of a sequence according to a specified key selector function and * creates a result value from each group. * * @param key A function to extract the key for each element. * @param result A function to create a result value from each group. * @returns The queryable result of the groupBy operation. */ groupBy unknown>, R>(key: K, result: (param: Grouping, T>) => R): Queryable; /** * Returns a specified number of contiguous elements from the start of a sequence. * * @param limit The number of elements to return. * @returns The queryable result of the limit operation. */ limit(limit: number): this; /** * Skips a specified number of elements in a sequence and then returns the remaining elements. * * @param offset The number of elements to skip. * @returns The queryable result of the offset operation. */ offset(offset: number): this; /** * Determines whether any element of a sequence satisfies a condition. * * @param predicate A function to test each element for a condition. * @returns `true` if any elements in the sequence satisfy the condition, otherwise `false`. */ any

>(predicate?: P): boolean; /** * Determines whether all elements of a sequence satisfy a condition. * * @param predicate A function to test each element for a condition. * @returns `true` if all elements in the sequence satisfy the condition, otherwise `false`. */ all

>(predicate: P): boolean; /** * Returns the first element of a sequence that satisfies a specified condition. * * @param predicate A function to test each element for a condition. * @returns The first element in the sequence that satisfies the condition. * @throws Error, if no such element is found. */ first

>(predicate?: P): T; /** * Returns the first element of a sequence that satisfies a specified condition, or `undefined` * if no such element is found. * * @param predicate A function to test each element for a condition. */ maybeFirst

>(predicate?: P): T | undefined; /** * Returns the only element of a sequence that satisfies a specified condition. * * @param predicate A function to test each element for a condition. * @returns The only element in the sequence that satisfies the condition. * @throws Error, if no such element is found, or multiple elements satisfy the condition. */ single

>(predicate?: P): T; /** * Returns the only element of a sequence that satisfies a specified condition, or `undefined` * if no such element is found. * * @param predicate A function to test each element for a condition. * @returns The only element in the sequence that satisfies the condition, or `undefined` if no such element is found. * @throws Error, if multiple elements satisfy the condition. */ maybeSingle

>(predicate?: P): T | undefined; /** * Returns the number of elements in the sequence. * * @returns The number of elements in the sequence. */ count(): number; /** * Returns the minimum value of the specified numerical expression for the sequence. * @param expr The numerical expression to be evaluated. * @returns The minimum value of the specified numerical expression for the sequence. */ min(expr?: NumericExpr): number; /** * Returns the maximum value of the specified numerical expression for the sequence. * @param expr The numerical expression to be evaluated. * @returns The maximum value of the specified numerical expression for the sequence. */ max(expr?: NumericExpr): number; /** * Returns the sum of the specified numerical expression for the sequence. * @param expr The numerical expression to be evaluated. * @returns The sum of the specified numerical expression for the sequence. */ sum(expr?: NumericExpr): number; /** * Returns the average value of the specified numerical expression for the sequence. * @param expr The numerical expression to be evaluated. * @returns The average value of the specified numerical expression for the sequence. */ avg(expr?: NumericExpr): number; /** * Aggregates the elements of the sequence into an array. * @returns An array of the elements in the sequence. */ array(): Queryable; } /** * Fluent query operators that must appear at the root of a compiled query expression. * * @template T The type of the entity being queried. */ export declare class QueryableRoot extends Queryable { /** * Inject a raw SQL query using tagged template literals. The returned queryable may be further * composed with additional query operators. * * @example * const query = db.compile(from => from.albums.sql`select * from "Album"`); * const albums = await query.toArray() * * @param strings The SQL query template string. * @param values The values to be inserted into the query. * @returns The queryable result of the injected SQL query. */ sql(strings: TemplateStringsArray, ...values: readonly any[]): this; /** * Set query options that affect the behaviour of the query. * * @example * const query = * db.compile(from => * from.albums.options({ disabledConditions: ["soft delete"] })); * * @param options The query options. * @returns The queryable with the specified options applied. */ options(options: QueryOptions): this; } /** * Fluent query operators used when constructing queries. * * A query can be created by accessing entity sets exposed by the {@link Verse.from} property. * * @template T The type of the entity being queried. * * @example * const customer = * await db.from.customers * .where(c => c.name === "ACME Corp.") * .single(); */ export declare class AsyncQueryable implements AsyncSequence { #private; /** @ignore */ constructor(entity: string, compiler: QueryCompiler); /** * Projects each element of a sequence into a new form. * * @param projector A function that transforms the input element. * @param args Additional arguments to be passed to the projector. * @returns The queryable result of the select operation. */ select

]) => unknown>, A extends unknown[]>(projector: P, ...args: A): AsyncQueryable, E>; /** * Used to specify eager loading of related entities in queries. * * @param navigation The navigation property to be loaded. * @returns The queryable result of the with operation. */ with(navigation: Expr<(obj: T) => S>): this; /** * Removes duplicate elements from the sequence. * * @returns The queryable result of the distinct operation. */ distinct(): this; /** * Joins the sequence with another sequence based on a condition. * * @param entityOrQuery The entity or query to join with. * @param condition The join condition. * @returns The queryable result of the join operation. */ join>(entityOrQuery: Newable | AsyncQueryable, condition: C): AsyncQueryable>, E>; /** * Left joins the sequence with another sequence based on a condition. * * @param entityOrQuery The entity or query to join with. * @param condition The join condition. * @returns The queryable result of the join operation. */ leftJoin>(entityOrQuery: Newable | AsyncQueryable, condition: C): AsyncQueryable>, E>; /** * Filters a sequence of values based on a predicate. * * @param predicate A function to test each element for a condition. * @param args Additional arguments to be passed to the predicate. * @returns The queryable result of the where operation. */ where

, A extends unknown[]>(predicate: P, ...args: A): this; /** * Sorts the elements of a sequence in ascending order. * * @param expr A function to extract a sort key from an element. * @param args Additional arguments to be passed to the expression. * @returns The queryable result of the orderBy operation. */ orderBy unknown>, A extends unknown[]>(expr: E, ...args: A): this; /** * Sorts the elements of a sequence in descending order. * * @param expr A function to extract a sort key from an element. * @param args Additional arguments to be passed to the expression. * @returns The queryable result of the orderByDesc operation. */ orderByDesc unknown>, A extends unknown[]>(expr: E, ...args: A): this; /** * Groups the elements of a sequence according to a specified key selector function. * * @param key A function to extract the key for each element. * @returns The result of the groupBy operation. */ groupBy unknown>>(key: K): AsyncQueryable<{ key: ReturnType; items: T[]; }, E>; /** * Groups the elements of a sequence according to a specified key selector function and * creates a result value from each group. * * @param key A function to extract the key for each element. * @param result A function to create a result value from each group. * @returns The queryable result of the groupBy operation. */ groupBy unknown>, R>(key: K, result: (param: Grouping, T>) => R): AsyncQueryable; /** * Returns a specified number of contiguous elements from the start of a sequence. * * @param limit The number of elements to return. * @returns The queryable result of the limit operation. */ limit(limit: number): this; /** * Skips a specified number of elements in a sequence and then returns the remaining elements. * * @param offset The number of elements to skip. * @returns The queryable result of the offset operation. */ offset(offset: number): this; /** * Determines whether a sequence contains any elements. * * @returns A promise that resolves with `true` if the sequence contains any elements, otherwise `false`. */ any(): Promise; /** * Determines whether any element of a sequence satisfies a condition. * * @param predicate A function to test each element for a condition. * @param args Additional arguments to be passed to the predicate. * @returns A promise that resolves with `true` if any elements in the sequence satisfy the condition, otherwise `false`. */ any

, A extends unknown[]>(predicate: P, ...args: A): Promise; /** * Determines whether all elements of a sequence satisfy a condition. * * @param predicate A function to test each element for a condition. * @param args Additional arguments to be passed to the predicate. * @returns A promise that resolves with `true` if all elements in the sequence satisfy the condition, otherwise `false`. */ all

, A extends unknown[]>(predicate: P, ...args: A): Promise; /** * Returns the first element of a sequence. * * @returns A promise that resolves with the first element in the sequence. * @throws Error, if no such element is found. */ first(): Promise; /** * Returns the first element of a sequence that satisfies a specified condition. * * @param predicate A function to test each element for a condition. * @param args Additional arguments to be passed to the predicate. * @returns A promise that resolves with the first element in the sequence that satisfies the condition. * @throws Error, if no such element is found. */ first

, A extends unknown[]>(predicate?: P, ...args: A): Promise; /** * Returns the first element of a sequence, or `undefined` if no such element is found. * * @returns A promise that resolves with the first element in the sequence, * or `undefined` if no such element is found. */ maybeFirst(): Promise; /** * Returns the first element of a sequence that satisfies a specified condition, or `undefined` * if no such element is found. * * @param predicate A function to test each element for a condition. * @param args Additional arguments to be passed to the predicate. * @returns A promise that resolves with the first element in the sequence that satisfies the condition, * or `undefined` if no such element is found. */ maybeFirst

, A extends unknown[]>(predicate?: P, ...args: A): Promise; /** * Returns the only element of a sequence. * * @returns A promise that resolves with the only element in the sequence. * @throws Error, if no such element is found, or there are multiple elements in the sequence. */ single(): Promise; /** * Returns the only element of a sequence that satisfies a specified condition. * * @param predicate A function to test each element for a condition. * @param args Additional arguments to be passed to the predicate. * @returns A promise that resolves with the only element in the sequence that satisfies the condition. * @throws Error, if no such element is found, or multiple elements satisfy the condition. */ single

, A extends unknown[]>(predicate?: P, ...args: A): Promise; /** * Returns the only element of a sequence, or `undefined` if no such element is found. * * @returns A promise that resolves with the only element in the sequence, or `undefined` * if no such element is found. * @throws Error, if multiple elements satisfy the condition. */ maybeSingle(): Promise; /** * Returns the only element of a sequence that satisfies a specified condition, or `undefined` * if no such element is found. * * @param predicate A function to test each element for a condition. * @param args Additional arguments to be passed to the predicate. * @returns A promise that resolves with the only element in the sequence that satisfies the condition, or `undefined` if no such element is found. * @throws Error, if multiple elements satisfy the condition. */ maybeSingle

, A extends unknown[]>(predicate?: P, ...args: A): Promise; /** * Returns the number of elements in the sequence. * * @returns A promise that resolves with the number of elements in the sequence. */ count(): Promise; /** * Returns the minimum value of the specified numerical expression for the sequence. * @param expr The numerical expression to be evaluated. * @returns A promise that resolves with the minimum value of the specified numerical expression for the sequence. */ min(expr?: NumericExpr): Promise; /** * Returns the maximum value of the specified numerical expression for the sequence. * @param expr The numerical expression to be evaluated. * @returns A promise that resolves with the maximum value of the specified numerical expression for the sequence. */ max(expr?: NumericExpr): Promise; /** * Returns the sum of the specified numerical expression for the sequence. * @param expr The numerical expression to be evaluated. * @returns A promise that resolves with the sum of the specified numerical expression for the sequence. */ sum(expr?: NumericExpr): Promise; /** * Returns the average value of the specified numerical expression for the sequence. * @param expr The numerical expression to be evaluated. * @returns A promise that resolves with the average value of the specified numerical expression for the sequence. */ avg(expr?: NumericExpr): Promise; /** * Aggregates the elements of the sequence into an array. * @returns The queryable result of the array operation. */ array(): AsyncQueryable; /** * Buffers the sequence into an array of type T. * * @return A promise that resolves with an array of type T. */ toArray(): Promise; /** @ignore */ [__expr](): Expression; /** @ignore */ protected op(name: string, expressions?: Expression | Expression[], args?: readonly unknown[]): void; /** @ignore */ [Symbol.asyncIterator](): AsyncIterator; protected execute(cache?: QueryCache): Promise | { readonly iterable: AsyncIterable; toArray(): Promise; [Symbol.asyncIterator](): AsyncIterator; }; } /** * Fluent query operators that must appear at the root of a query expression. * * @template T The type of the entity being queried. */ export declare class AsyncQueryableRoot extends AsyncQueryable { /** * @ignore */ constructor(entity: string, compiler: QueryCompiler); /** * Inject a raw SQL query using tagged template literals. The returned queryable may be further * composed with additional query operators. * * @example * const id = 1; * const album = * db.from.albums * .sql`select * from "Album" where "AlbumId" = ${id}` * .single(); * * @param strings The SQL query template string. * @param values The values to be inserted into the query. * @returns The queryable result of the injected SQL query. */ sql(strings: TemplateStringsArray, ...values: readonly any[]): this; /** * Set query options that affect the behaviour of the query. * * @example * const q = * db.from.albums.options({ disabledConditions: ["soft delete"] }); * * @param options The query options. * @returns The queryable with the specified options applied. */ options(options: QueryOptions): this; } export {};