import { Model } from '../../base/index.js'; import { RelatedDatabase } from './database.js'; import { Condition, ModelOptions, RelationDefinition } from '../../types.js'; /** * 关联查询构建器 * 支持链式调用预加载关联数据 */ export declare class RelationQueryBuilder = Record, T extends keyof S = keyof S> { private readonly model; private readonly relationNames; private conditions; private orderings; private limitCount?; private offsetCount?; private selectedFields?; constructor(model: RelatedModel, relationNames: string[]); /** * 选择字段 */ select(...fields: K[]): this; /** * 添加查询条件 */ where(condition: Condition): this; /** * 排序 */ orderBy(field: keyof S[T], direction?: 'ASC' | 'DESC'): this; /** * 限制数量 */ limit(count: number): this; /** * 偏移量 */ offset(count: number): this; /** * 执行查询并加载关联 */ then(onfulfilled?: (value: (S[T] & { [key: string]: any; })[]) => TResult | PromiseLike): Promise; } /** * 关系型模型类 * 继承自 BaseModel,提供关系型数据库特有的操作 * * @example * ```ts * // 创建带软删除的模型 * const userModel = new RelatedModel(db, 'users', { softDelete: true }); * * // 删除(实际执行 UPDATE SET deletedAt = NOW()) * await userModel.delete({ id: 1 }); * * // 查询(自动排除已删除) * await userModel.select('id', 'name'); * * // 查询包含已删除 * await userModel.selectWithTrashed('id', 'name'); * * // 恢复已删除 * await userModel.restore({ id: 1 }); * ``` */ export declare class RelatedModel = Record, T extends keyof S = keyof S> extends Model { /** 关系定义存储 */ private relations; constructor(database: RelatedDatabase, name: T, options?: ModelOptions); /** * 定义一对多关系 * @param targetModel 目标模型实例 * @param foreignKey 目标表中的外键字段 * @param localKey 本表的主键字段(默认 'id') * @example * ```ts * const userModel = db.model('users'); * const orderModel = db.model('orders'); * * // User hasMany Orders (orders.userId -> users.id) * userModel.hasMany(orderModel, 'userId'); * ``` */ hasMany(targetModel: RelatedModel, foreignKey: keyof S[To], localKey?: keyof S[T]): this; /** * 定义多对一关系 * @param targetModel 目标模型实例 * @param foreignKey 本表中的外键字段 * @param targetKey 目标表的主键字段(默认 'id') * @example * ```ts * const userModel = db.model('users'); * const orderModel = db.model('orders'); * * // Order belongsTo User (orders.userId -> users.id) * orderModel.belongsTo(userModel, 'userId'); * ``` */ belongsTo(targetModel: RelatedModel, foreignKey: keyof S[T], targetKey?: keyof S[To]): this; /** * 定义一对一关系 * @param targetModel 目标模型实例 * @param foreignKey 目标表中的外键字段 * @param localKey 本表的主键字段(默认 'id') * @example * ```ts * const userModel = db.model('users'); * const profileModel = db.model('profiles'); * * // User hasOne Profile (profiles.userId -> users.id) * userModel.hasOne(profileModel, 'userId'); * ``` */ hasOne(targetModel: RelatedModel, foreignKey: keyof S[To], localKey?: keyof S[T]): this; /** * 定义多对多关系 * @param targetModel 目标模型实例 * @param pivotTable 中间表名 * @param foreignPivotKey 中间表中指向本表的外键 * @param relatedPivotKey 中间表中指向目标表的外键 * @param localKey 本表的主键字段(默认 'id') * @param relatedKey 目标表的主键字段(默认 'id') * @example * ```ts * const userModel = db.model('users'); * const roleModel = db.model('roles'); * * // User belongsToMany Roles (通过 user_roles 中间表) * userModel.belongsToMany(roleModel, 'user_roles', 'user_id', 'role_id'); * * // 双向关系 * roleModel.belongsToMany(userModel, 'user_roles', 'role_id', 'user_id'); * ``` */ belongsToMany(targetModel: RelatedModel, pivotTable: string, foreignPivotKey: string, relatedPivotKey: string, localKey?: keyof S[T], relatedKey?: keyof S[To], pivotFields?: string[]): this; /** * 获取关系定义 */ getRelation(name: string): RelationDefinition | undefined; /** * 获取所有关系名称 */ getRelationNames(): string[]; /** * 加载单条记录的关联数据 * @example * ```ts * const user = await userModel.selectById(1); * const userWithPosts = await userModel.loadRelation(user, 'posts'); * // userWithPosts.posts = [{ id: 1, title: '...' }, ...] * ``` */ loadRelation(record: S[T], relationName: RelName): Promise; /** * 批量加载关联数据(预加载) * @example * ```ts * const users = await userModel.select('id', 'name'); * const usersWithPosts = await userModel.loadRelations(users, ['posts']); * ``` */ loadRelations(records: S[T][], relationNames: RelNames[]): Promise<(S[T] & { [K in RelNames]?: any; })[]>; /** * 带关联的查询(链式调用入口) * @example * ```ts * const users = await userModel.with('posts', 'profile') * .where({ status: 'active' }); * ``` */ with(...relationNames: string[]): RelationQueryBuilder; /** * 获取单条记录的关联数据 */ private fetchRelatedData; /** * 批量加载关联(优化 N+1 问题) */ private batchLoadRelation; /** * 创建数据(支持生命周期钩子) * @example * ```ts * userModel.addHook('beforeCreate', (ctx) => { * ctx.data.slug = slugify(ctx.data.name); * }); * const user = await userModel.create({ name: 'John' }); * ``` */ create(data: Partial): Promise; /** * 批量创建数据(每条数据都会触发钩子) */ createMany(data: Partial[]): Promise; /** * 查找单个数据(支持生命周期钩子) * @example * ```ts * userModel.addHook('afterFind', (ctx) => { * if (ctx.result) { * ctx.result.fullName = ctx.result.firstName + ' ' + ctx.result.lastName; * } * }); * const user = await userModel.findOne({ id: 1 }); * ``` */ findOne(query?: Condition): Promise; /** * 查找多条数据(支持生命周期钩子) */ findAll(query?: Condition): Promise; /** * selectOne 的别名(向后兼容) */ selectOne(query?: Condition): Promise; /** * 根据ID查找 */ selectById(id: any): Promise; /** * findById 别名 */ findById(id: any): Promise; /** * 更新数据(支持生命周期钩子) * @example * ```ts * userModel.addHook('beforeUpdate', (ctx) => { * ctx.data.updatedAt = new Date(); * }); * await userModel.updateWhere({ role: 'guest' }, { role: 'member' }); * ``` */ updateWhere(query: Condition, data: Partial): Promise; /** * 更新单个数据(向后兼容) */ updateOne(query: Condition, data: Partial): Promise; /** * 根据ID更新 */ updateById(id: any, data: Partial): Promise; /** * 删除数据(支持生命周期钩子和软删除) * @example * ```ts * userModel.addHook('beforeDelete', async (ctx) => { * // 删除前检查 * const user = await userModel.findOne(ctx.where); * if (user?.role === 'admin') return false; // 取消删除 * }); * await userModel.deleteWhere({ status: 'inactive' }); * ``` */ deleteWhere(query: Condition): Promise; /** * 根据ID删除(支持软删除和钩子) */ deleteById(id: any): Promise; /** * 根据ID强制删除(物理删除,忽略软删除设置,但仍触发钩子) */ forceDeleteById(id: any): Promise; /** * 根据ID恢复软删除的记录 */ restoreById(id: any): Promise; /** * 统计数量 */ count(query?: Condition): Promise; } //# sourceMappingURL=model.d.ts.map