import { Dict } from 'cosmokit'; import { Driver } from './driver.ts'; import { Eval } from './eval.ts'; import { Model } from './model.ts'; import { Query } from './query.ts'; import { FlatKeys, FlatPick, Flatten, Keys, Row } from './utils.ts'; declare module './eval.ts' { namespace Eval { interface Static { exec(value: Executable): Expr; } } } export type Direction = 'asc' | 'desc'; export interface Modifier { limit: number; offset: number; sort: [Eval.Expr, Direction][]; group?: string[]; having: Eval.Expr; fields?: Dict; optional: Dict; } declare namespace Executable { type Action = 'get' | 'set' | 'remove' | 'create' | 'upsert' | 'eval'; interface Payload { type: Action; table: string | Selection | Dict; ref: string; query: Query.Expr; args: any[]; } } interface Executable extends Executable.Payload { } declare class Executable { readonly row: Row; readonly model: Model; readonly driver: Driver; constructor(driver: Driver, payload: Executable.Payload); protected isCallaback(query: any): query is Selection.Callback; protected resolveQuery(query?: Query): Query.Expr; protected resolveField(field: FieldLike | Eval.Expr): Eval.Expr; protected resolveFields(fields: string | string[] | Dict> | FieldCallback): any; execute(): Promise; } type FieldLike = FlatKeys | Selection.Callback; type FieldType> = T extends FlatKeys ? Flatten[T] : T extends Selection.Callback ? Eval> : never; type FieldMap>> = { [K in keyof M]: FieldType; }; type FieldCallback> = any> = (row: Row) => M; type EvalMap>> = { [K in keyof M]: Eval; }; export declare namespace Selection { type Callback = (row: Row) => Eval.Expr; interface Immutable extends Executable, Executable.Payload { tables: Dict; } interface Mutable extends Executable, Executable.Payload { tables: Dict; table: string; } } export interface Selection extends Executable.Payload { args: [Modifier]; } export declare class Selection extends Executable { tables: Dict; constructor(driver: Driver, table: string | Selection | Dict, query?: Query); where(query: Query): this; limit(limit: number): this; limit(offset: number, limit: number): this; offset(offset: number): this; orderBy(field: FieldLike, direction?: Direction): this; groupBy>(fields: K | readonly K[], query?: Selection.Callback): Selection>; groupBy, U extends Dict>>(fields: K | K[], extra?: U, query?: Selection.Callback): Selection & FieldMap>; groupBy, U extends object>(fields: K | K[], extra?: FieldCallback, query?: Selection.Callback): Selection & EvalMap>; groupBy>>(fields: K, query?: Selection.Callback): Selection>; groupBy>, U extends Dict>>(fields: K, extra?: U, query?: Selection.Callback): Selection>; groupBy>, U extends object>(fields: K, extra?: FieldCallback, query?: Selection.Callback): Selection & EvalMap>; having(query: Selection.Callback): this; project>(fields: K | readonly K[]): Selection>; project>>(fields: U): Selection>; project(fields: FieldCallback): Selection>; join(name: K, selection: Selection, callback?: (self: Row, other: Row) => Eval.Expr, optional?: boolean): Selection; _action(type: Executable.Action, ...args: any[]): Executable; evaluate(callback: Selection.Callback): Eval.Expr; evaluate>(field: K): Eval.Expr; evaluate>(field: K[]): Eval.Expr; evaluate(): Eval.Expr; execute(): Promise; execute = any>(cursor?: Driver.Cursor): Promise[]>; execute(callback: Selection.Callback): Promise; } export declare namespace Selection { function is(sel: any): sel is Selection; } export declare function executeSort(data: any[], modifier: Modifier, name: string): any[]; export {};