import { Pool } from 'pg'; import * as elysia from 'elysia'; import { Elysia } from 'elysia'; /** * Get development database connection pool (singleton) * Reads configuration from environment variables: * - DB_HOST (default: localhost) * - DB_PORT (default: 5432) * - DB_NAME * - DB_USER * - DB_PASSWORD */ declare function getDevDbPool(): Pool; /** * Close database connection pool */ declare function closeDevDbPool(): Promise; type DbQueryFn = (sql: string, params?: unknown[]) => Promise<{ rows: unknown[]; }>; type DbQueryExecutor = (sql: string, params?: unknown[]) => Promise<{ rows: unknown[]; } | unknown[]>; /** 结构化 SQL 日志事件(平台 logger 消费) */ interface SqlLogEvent { sql: string; /** 参数内联后的可执行 SQL(开发日志 / 平台 logger 优先展示) */ runnableSql?: string; params?: unknown[]; durationMs: number; rowCount: number; tag?: string; } interface LoggedDbQueryOptions { /** * 是否打印 SQL * - 未设置:开发环境默认 true,生产环境默认 false * - DB_LOG_SQL / STAGO_LOG_SQL:true/false/1/0 显式控制 */ logSql?: boolean; /** 日志前缀,如 @ops/catindex@0.3.14 */ tag?: string; /** 结构化 SQL 日志(生产推荐) */ logEvent?: (event: SqlLogEvent) => void; /** 纯文本 SQL 日志(开发兜底) */ log?: (message: string) => void; } /** 是否启用 SQL 日志(DB_LOG_SQL / STAGO_LOG_SQL) */ declare function isSqlLogEnabled(): boolean; /** * 包装任意 db 执行函数,按环境变量打印 SQL、参数、耗时与行数。 */ declare function createLoggedDbQuery(execute: DbQueryExecutor, options?: LoggedDbQueryOptions): DbQueryFn; /** 包装 pg Pool.query */ declare function createLoggedPoolQuery(pool: Pool, options?: LoggedDbQueryOptions): DbQueryFn; /** 网关透传给模块 API 的用户;字段与 live.employees 一一对应:id / phone / name */ interface ModuleRequestUser { id: number; phone: string; name: string | null; roles: string[]; } type AuditJsonValue = null | boolean | number | string | AuditJsonValue[] | { [key: string]: AuditJsonValue; }; interface BusinessAuditEvent { action?: string; summary: string; target: { type: string; id: string | number; name?: string; }; changes?: Record; metadata?: Record; } interface BusinessAuditRecord extends BusinessAuditEvent { action?: string; actorId: number; requestId: string; sourceIp?: string; userAgent?: string; } interface BusinessAuditSink { write(record: BusinessAuditRecord): Promise<{ id: string; }>; } /** 模块可用的 scoped 数据库客户端 */ interface ModuleDb { query(sql: string, params?: unknown[]): Promise<{ rows: unknown[]; }>; /** * 在单一连接上执行事务。回调内必须使用 `tx.query`,不要再用外层 `db.query`。 * 不支持嵌套。默认 statement_timeout 30s;`options.timeoutMs` 可覆盖但钳制在 * 1s–120s,非法值(≤0/NaN/Infinity)回落默认,无法关闭。事务空转由 * idle_in_transaction_session_timeout(=上限)兜底断开。 */ transaction(fn: (tx: ModuleDb) => Promise, options?: { timeoutMs?: number; }): Promise; } /** 模块日志接口 */ interface ModuleLogger { info(message: string, data?: unknown): void; warn(message: string, data?: unknown): void; error(message: string, data?: unknown): void; } /** * 平台注入给模块的依赖(由 @stago/api 在 load 时构造) */ interface ModuleDeps { moduleName: string; version: string; /** module.json `api.prefix`,如 `/insight` */ mountPrefix: string; /** 完整 API 路由前缀,如 `/insight/api` */ routeBase: string; db: ModuleDb; log: ModuleLogger; /** 平台提供的业务审计持久化端;模块通过请求上下文中的 audit.record() 调用。 */ auditSink?: BusinessAuditSink; /** * 平台注入的模块专属配置(来自 stago.module_env / 本地 createDevModuleDeps)。 * 业务密钥只应从这里读取,不要读 process.env 中的平台机密。 */ env: Readonly>; } /** 模块 API 入口:export default (deps) => new Elysia(...) * BasePath 显式声明为 string(而非默认的字面量 ''),因为模块通常会用 * deps.routeBase(动态 string)作为 prefix,返回类型的字面量 prefix 会被推断为 string。 */ type ModulePluginFactory = (deps: ModuleDeps) => elysia.Elysia; interface DevModuleDepsOptions { moduleName: string; moduleVersion: string; mountPrefix?: string; logSql?: boolean; logEvent?: (event: SqlLogEvent) => void; log?: (message: string) => void; /** 本地模拟 deps.env;默认 {} */ env?: Record; } declare function createDevModuleDeps(options: DevModuleDepsOptions): ModuleDeps; /** * 从 ModulePluginFactory 创建开发用 Elysia app。 * 包装 fetch:把 x-stago-user 中的登录人 id 写入 SQL 注释请求上下文。 */ declare function createDevModuleApp(factory: ModulePluginFactory, deps: ModuleDeps): Elysia<"", { decorator: {}; store: {}; derive: {}; resolve: {}; }, { typebox: {}; error: {}; }, { schema: {}; standaloneSchema: {}; macro: {}; macroFn: {}; parser: {}; response: {}; }, {}, { derive: {}; resolve: {}; schema: {}; standaloneSchema: {}; response: {}; }, { derive: {}; resolve: {}; schema: {}; standaloneSchema: {}; response: { 200: { success: boolean; error: { code: string; message: string; }; }; }; } & { derive: {}; resolve: {}; schema: {}; standaloneSchema: {}; response: {}; }>; /** @deprecated 使用 createDevModuleApp */ declare function createDevApiServer(factory: ModulePluginFactory, options: DevModuleDepsOptions): Elysia<"", { decorator: {}; store: {}; derive: {}; resolve: {}; }, { typebox: {}; error: {}; }, { schema: {}; standaloneSchema: {}; macro: {}; macroFn: {}; parser: {}; response: {}; }, {}, { derive: {}; resolve: {}; schema: {}; standaloneSchema: {}; response: {}; }, { derive: {}; resolve: {}; schema: {}; standaloneSchema: {}; response: { 200: { success: boolean; error: { code: string; message: string; }; }; }; } & { derive: {}; resolve: {}; schema: {}; standaloneSchema: {}; response: {}; }>; type DevApiServerOptions = DevModuleDepsOptions; /** 可选常量:需显式传给 stagoDevApiPlugin({ devUser }) 才会注入,插件不会自动使用 */ declare const DEFAULT_DEV_MODULE_REQUEST_USER: ModuleRequestUser; /** * 若请求未带 x-stago-user,则写入给定用户;已有头时原样返回。 * 调用方必须显式传入 user(不会隐式使用默认用户)。 */ declare function ensureDevModuleUserRequest(request: Request, user: ModuleRequestUser): Request; /** 开发环境终端日志着色(ANSI),生产结构化日志不走此处 */ declare function isDevLogColorEnabled(stream?: { isTTY?: boolean; }): boolean; declare function colorizeHttpStatus(status: number): string; interface DevSqlLogParts { tag?: string; durationMs: number; rowCount: number; runnableSql: string; } declare function formatDevSqlLog(parts: DevSqlLogParts): string; interface DevApiLogParts { method: string; path: string; status: number; durationMs: number; } declare function formatDevApiLog(parts: DevApiLogParts): string; declare function formatDevBanner(message: string): string; type DevLogLevel = 'info' | 'warn' | 'error'; declare function formatDevAppLog(level: DevLogLevel, message: string, data?: unknown): string; export { DEFAULT_DEV_MODULE_REQUEST_USER, type DbQueryExecutor, type DbQueryFn, type DevApiLogParts, type DevApiServerOptions, type DevLogLevel, type DevModuleDepsOptions, type DevSqlLogParts, type LoggedDbQueryOptions, type SqlLogEvent, closeDevDbPool, colorizeHttpStatus, createDevApiServer, createDevModuleApp, createDevModuleDeps, createLoggedDbQuery, createLoggedPoolQuery, ensureDevModuleUserRequest, formatDevApiLog, formatDevAppLog, formatDevBanner, formatDevSqlLog, getDevDbPool, isDevLogColorEnabled, isSqlLogEnabled };