import PocketBase, { RecordModel } from "pocketbase" import { Logger } from "winston" import { AppInfoModel } from "../../constant/config" import PbToolBase, { WithExpand } from "../base" /** API密钥集合的名称常量 */ const DB_NAME = "apiKey" /** * API密钥基础数据结构接口 * 定义了API密钥的基本字段 */ export interface ApiKey { /** API密钥名称 */ name: string /** API密钥拥有者标识符 */ owner: string /** 关联的应用程序标识符 */ app: string } /** * API密钥模型类型,结合基础接口和PocketBase的记录模型 */ export type ApiKeyModel = ApiKey & RecordModel /** * 扩展了应用信息的API密钥模型类型 * 包含完整的应用程序信息 */ export type ApiKeyModelWithApp = ApiKeyModel & WithExpand<{ app: AppInfoModel }> /** * API密钥数据库操作类 * 提供API密钥相关的特定查询和操作方法 * * @extends PbToolBase 基础PocketBase操作类 */ class ApiKeyDB extends PbToolBase { /** * 创建API密钥数据库操作实例 * * @param pbClient - PocketBase客户端实例 * @param logger - 日志记录器实例 */ constructor(pbClient: PocketBase, logger: Logger) { super(DB_NAME, pbClient, logger) } /** * 获取带有扩展应用信息的API密钥 * * @param id - API密钥记录的ID * @returns 成功时返回包含应用信息的API密钥记录,失败时返回null * * @example * ```typescript * const apiKeyWithApp = await apiKeyDB.getWithApp("api_key_123"); * if (apiKeyWithApp) { * console.log(`应用名称: ${apiKeyWithApp.expand.app.name}`); * } * ``` */ public getWithApp = (id: string) => { return this.get(id, { expand: "app" }) } } export default ApiKeyDB