/** * MonSQLize Model 自动加载器 * * 支持两种 Model 来源(可同时使用): * 1. 共享 Model 包(@project/models)— 微服务场景下多服务共享 Model 定义 * 2. 本地 models/ 目录(src/models/*.ts)— 项目本地 Model 定义 * * 加载顺序:先 shared 包 → 再本地目录(本地可覆盖 shared)。 * * 文件名推断 Model 名称规则(deriveModelName): * - user.ts → 'User' * - order-item.ts → 'OrderItem' * - admin/role.ts → 'AdminRole' * - billing/invoice.ts → 'BillingInvoice' * * 排除规则: * - 以 _ 开头的文件(如 _base.ts) * - .d.ts 声明文件 * - .test. / .spec. 测试文件 * * @module lib/plugins/monsqlize/model-loader * @see 13-monsqlize-plugin.md §2.5(Model 自动加载) */ import type { MonSQLize } from "monsqlize"; import type { VextPluginContext } from "../../../types/plugin.js"; import type { MonSQLizeDatabaseConfig } from "./types.js"; /** * 加载 Model 定义 * * 支持两种来源(可同时使用): * 1. 共享 Model 包(config.sharedPackage) * 2. 本地 models/ 目录(config.dir,默认 'models') * * @param monsqlize MonSQLize 实例(已连接) * @param modelsConfig Model 配置(来自 app.config.database.models) * @param app 插件上下文(用于日志) * @param srcDir src/ 目录的绝对路径(用于定位 models/ 目录) */ export declare function loadModels(monsqlize: MonSQLize, modelsConfig: MonSQLizeDatabaseConfig["models"] | undefined, app: VextPluginContext, srcDir: string): Promise; /** * 从文件路径推断 Model 名称 * * 规则: * - 去除扩展名 * - 按目录分隔符拆分 * - 每段按 - 或 _ 拆分,首字母大写后拼接 * - 所有段拼接(PascalCase) * * @example * deriveModelName('user.ts') → 'User' * deriveModelName('order-item.ts') → 'OrderItem' * deriveModelName('admin/role.ts') → 'AdminRole' * deriveModelName('billing/invoice.ts') → 'BillingInvoice' * deriveModelName('user_profile.ts') → 'UserProfile' */ export declare function deriveModelName(filePath: string): string; /** * resolveModelEntry — 计算 Model 注册信息 * * 根据文件相对路径和定义对象,返回: * - registryKey:注册到 MonSQLize 的键名(用于 app.db.model()) * - finalDef:最终定义对象(深度 >= 1 时自动注入 name / connection) * - depth:目录深度(文件名以外的路径段数) * * **深度规则:** * * | 深度 | 示例 | 行为 | * |------|------------------------|----------------------------------------------| * | 0 | `order.ts` | 行为不变,registry key = def.collection ?? def.name ?? PascalCase(file) | * | 1 | `a/order.ts` | key = 'AOrder',自动注入 name:'order', connection:{database:'a'} | * | 2 | `c/a/order.ts` | key = 'CAOrder',自动注入 name:'order', connection:{pool:'c',database:'a'} | * | >= 3 | `x/c/a/order.ts` | 返回 null(调用方应发出警告并跳过) | * * **优先级:** * - 用户在定义对象中显式设置的 `collection` / `name` / `connection` 字段均会覆盖自动推断值。 * * @param file 相对于 models/ 目录的文件路径,如 'a/order.ts' 或 'order.ts' * @param def Model 定义对象(来自文件的 export default) * @returns 注册信息,或 null(深度超限时) */ export declare function resolveModelEntry(file: string, def: Record): { registryKey: string; finalDef: Record; depth: number; } | null;