import type { Database } from './database.js'; import type { Dialect } from './dialect.js'; import { AlterDefinition, Condition, ModelOptions, HookName, HookFn, HookContext, HooksConfig } from '../types.js'; import * as QueryClasses from './query-classes.js'; /** * 基础模型抽象类 * 定义所有模型类型的通用接口和行为 * 支持软删除、自动时间戳和生命周期钩子 */ export declare abstract class Model = Record, Q = string, T extends keyof S = keyof S> { readonly database: Database; readonly name: T; /** 模型配置选项 */ readonly options: ModelOptions; /** 生命周期钩子注册表 */ private readonly hooks; constructor(database: Database, name: T, options?: ModelOptions); /** * 注册生命周期钩子 * @param hookName 钩子名称 * @param fn 钩子函数 * @returns this(支持链式调用) * * @example * ```ts * userModel * .addHook('beforeCreate', (ctx) => { * ctx.data.createdBy = 'system'; * }) * .addHook('afterDelete', async (ctx) => { * await logService.log('User deleted', ctx.result); * }); * ``` */ addHook(hookName: HookName, fn: HookFn): this; /** * addHook 的别名 */ on(hookName: HookName, fn: HookFn): this; /** * 批量注册钩子 * @example * ```ts * userModel.registerHooks({ * beforeCreate: (ctx) => { ... }, * afterUpdate: [(ctx) => { ... }, (ctx) => { ... }] * }); * ``` */ registerHooks(hooks: HooksConfig): this; /** * 移除指定钩子 */ removeHook(hookName: HookName, fn?: HookFn): this; /** * 清除所有钩子 */ clearHooks(): this; /** * 触发钩子 * @returns 如果任何 before 钩子返回 false,则返回 false */ protected runHooks(hookName: HookName, context: HookContext): Promise; /** * 创建钩子上下文 */ protected createHookContext(options?: Partial>): HookContext; /** * 获取数据库方言 */ get dialect(): Dialect; /** * 获取数据库是否已启动 */ get isStarted(): boolean; /** * 获取模型名称 */ get modelName(): T; /** * 是否启用软删除 */ get isSoftDelete(): boolean; /** * 软删除字段名 */ get deletedAtField(): string; alter(alterations: AlterDefinition): QueryClasses.Alteration; /** * 查询(软删除模式下自动排除已删除记录) */ select(...fields: Array): QueryClasses.Selection; /** * 查询(包含已删除的记录) */ selectWithTrashed(...fields: Array): QueryClasses.Selection; /** * 仅查询已删除的记录 */ selectOnlyTrashed(...fields: Array): QueryClasses.Selection; insert(data: S[T]): QueryClasses.Insertion; update(update: Partial): QueryClasses.Updation; /** * 删除(软删除模式下执行软删除) */ delete(condition: Condition): QueryClasses.Deletion | QueryClasses.Updation; /** * 强制删除(忽略软删除,直接物理删除) */ forceDelete(condition: Condition): QueryClasses.Deletion; /** * 恢复软删除的记录 */ restore(condition: Condition): QueryClasses.Updation; /** * 批量插入 */ insertMany(data: S[T][]): QueryClasses.BatchInsertion; /** * 聚合查询 */ aggregate(): QueryClasses.Aggregation; /** * 验证查询条件 */ protected validateQuery(query: any): boolean; /** * 验证数据 */ protected validateData(data: any): boolean; /** * 处理错误 */ protected handleError(error: Error, operation: string): never; } /** * 模型信息接口 */ export interface ModelInfo { name: string; databaseName: string; dialectName: string; isStarted: boolean; modelCount: number; } //# sourceMappingURL=model.d.ts.map