import type { Database } from './database.js'; import { ThenableQuery } from './thenable.js'; import { QueryParams, AlterDefinition, Condition, Ordering, Definition, Subquery, JoinType } from '../types.js'; /** 软删除查询模式 */ export type SoftDeleteMode = 'default' | 'withTrashed' | 'onlyTrashed'; export declare class Alteration, T extends keyof S, C = any, D = string> extends ThenableQuery { private readonly tableName; private readonly alterations; constructor(database: Database, tableName: T, alterations: AlterDefinition); protected getQueryParams(): QueryParams; } export declare class DroppingTable, T extends keyof S, C = any, D = string> extends ThenableQuery { private readonly tableName; private conditions; constructor(database: Database, tableName: T); where(query: Condition): this; protected getQueryParams(): QueryParams; } export declare class DroppingIndex, T extends keyof S, C = any, D = string> extends ThenableQuery { private readonly indexName; private readonly tableName; private conditions; constructor(database: Database, indexName: string, tableName: T); where(query: Condition): this; protected getQueryParams(): QueryParams; } export declare class Creation, T extends keyof S, C = any, D = string> extends ThenableQuery { private readonly tableName; private readonly definition; constructor(database: Database, tableName: T, definition: Definition); protected getQueryParams(): QueryParams; } /** * SELECT 查询类 * 实现 Subquery 以支持类型安全的子查询 * * @template S Schema 类型 * @template T 表名 * @template K 选择的字段 * @template C 连接类型 * @template D 方言类型 * * @example * ```ts * // 子查询类型推断示例 * interface Schema { * users: { id: number; name: string }; * orders: { id: number; userId: number; amount: number }; * } * * // ✅ 正确:userId 是 number,匹配 users.id * db.select('users', ['id']).where({ * id: { $in: db.select('orders', ['userId']) } * }); * * // ❌ 类型错误:name 是 string,不能与 number 的 id 匹配 * db.select('users', ['id']).where({ * id: { $in: db.select('users', ['name']) } // Type error! * }); * ``` */ export declare class Selection, T extends keyof S, K extends keyof S[T], C = any, D = string> extends ThenableQuery[], S, T, C, D> implements Subquery { protected readonly modelName: T; protected readonly fields: Array; readonly __isSubquery: true; readonly __returnType?: S[T][K]; protected conditions: Condition; protected groupings: (keyof S[T])[]; protected orderings: Ordering[]; protected limitCount?: number; protected offsetCount?: number; constructor(database: Database, modelName: T, fields: Array); where(query: Condition): this; groupBy(...fields: (keyof S[T])[]): this; orderBy(field: keyof S[T], direction?: "ASC" | "DESC"): this; limit(count: number): this; offset(count: number): this; /** * INNER JOIN - 只返回两表都有匹配的行 * 返回类型扩展为包含关联表字段 * * @example * ```ts * const result = await db.select('users', ['id', 'name']) * .join('orders', 'id', 'userId'); * // result 类型: { users: { id, name }, orders: { id, userId, amount } }[] * ``` */ join(table: J, leftField: keyof S[T], rightField: keyof S[J]): JoinedSelection; /** * LEFT JOIN - 返回左表所有行,右表无匹配则为 NULL */ leftJoin(table: J, leftField: keyof S[T], rightField: keyof S[J]): JoinedSelection; /** * RIGHT JOIN - 返回右表所有行,左表无匹配则为 NULL */ rightJoin(table: J, leftField: keyof S[T], rightField: keyof S[J]): JoinedSelection; /** * 转换为子查询 SQL * 用于 $in / $nin 等操作符中嵌套查询 */ toSQL(): { sql: string; params: any[]; }; protected getQueryParams(): QueryParams; } /** * JOIN 结果类型 - 根据表名命名空间化 */ type JoinResult, T extends keyof S, K extends keyof S[T], J extends keyof S, LeftNullable extends boolean = false, RightNullable extends boolean = false> = { [P in T]: LeftNullable extends true ? Partial> : Pick; } & { [P in J]: RightNullable extends true ? Partial | null : S[J]; }; /** * JOIN 查询类 - 支持类型安全的关联查询 * * @template S Schema 类型 * @template T 主表名 * @template K 主表选择的字段 * @template J 关联表名 * @template LeftNullable LEFT JOIN 时主表可能为 null * @template RightNullable RIGHT JOIN 时关联表可能为 null */ export declare class JoinedSelection, T extends keyof S, K extends keyof S[T], J extends keyof S, C = any, D = string, LeftNullable extends boolean = false, RightNullable extends boolean = false> extends ThenableQuery[], S, T, C, D> { private readonly modelName; private readonly fields; private conditions; private orderings; private groupings; private limitCount?; private offsetCount?; private joinClauses; constructor(database: Database, modelName: T, fields: Array, joinTable: J, joinType: JoinType, leftField: keyof S[T], rightField: keyof S[J], conditions?: Condition, orderings?: Ordering[], groupings?: (keyof S[T])[], limitCount?: number | undefined, offsetCount?: number | undefined); where(query: Condition): this; orderBy(field: keyof S[T], direction?: "ASC" | "DESC"): this; limit(count: number): this; offset(count: number): this; /** * 继续 JOIN 更多表 */ join(table: J2, leftField: keyof S[T], rightField: keyof S[J2]): this; leftJoin(table: J2, leftField: keyof S[T], rightField: keyof S[J2]): this; rightJoin(table: J2, leftField: keyof S[T], rightField: keyof S[J2]): this; protected getQueryParams(): QueryParams; } export declare class Insertion, T extends keyof S, C = any, D = string> extends ThenableQuery { private readonly modelName; private readonly data; constructor(database: Database, modelName: T, data: S[T]); protected getQueryParams(): QueryParams; } export declare class Updation, T extends keyof S, C = any, D = string> extends ThenableQuery { private readonly modelName; private readonly update; private conditions; constructor(database: Database, modelName: T, update: Partial); where(query: Condition): this; protected getQueryParams(): QueryParams; } export declare class Deletion, T extends keyof S, C = any, D = string> extends ThenableQuery { private readonly modelName; private conditions; constructor(database: Database, modelName: T); where(query: Condition): this; protected getQueryParams(): QueryParams; } /** * 批量插入查询类 */ export declare class BatchInsertion, T extends keyof S, C = any, D = string> extends ThenableQuery<{ affectedRows: number; insertIds?: (number | string)[]; }, S, T, C, D> { private readonly modelName; private readonly data; constructor(database: Database, modelName: T, data: S[T][]); then(onfulfilled?: ((value: { affectedRows: number; insertIds?: (number | string)[]; }) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null): Promise; protected getQueryParams(): QueryParams; } /** * 聚合查询结果类型 */ export interface AggregateResult { [key: string]: number | string | null; } /** * 聚合查询类 */ export declare class Aggregation, T extends keyof S, C = any, D = string> extends ThenableQuery { private readonly modelName; private conditions; private groupings; private havingConditions; private aggregates; constructor(database: Database, modelName: T); /** * COUNT 聚合 */ count(field?: keyof S[T] | '*', alias?: string): this; /** * SUM 聚合 */ sum(field: keyof S[T], alias?: string): this; /** * AVG 聚合 */ avg(field: keyof S[T], alias?: string): this; /** * MIN 聚合 */ min(field: keyof S[T], alias?: string): this; /** * MAX 聚合 */ max(field: keyof S[T], alias?: string): this; /** * WHERE 条件 */ where(query: Condition): this; /** * GROUP BY */ groupBy(...fields: (keyof S[T])[]): this; /** * HAVING 条件(用于聚合后的过滤) */ having(query: Condition): this; protected getQueryParams(): QueryParams; } export {}; //# sourceMappingURL=query-classes.d.ts.map