/** * 模块 SQL 注释的请求级上下文(当前登录人 id)。 * 仅 Node:createDevModuleApp 写入。生产走 apps/api module-env,不经过本文件。 * 禁止从 @stago/sdk 根入口导出,否则浏览器会加载 node:async_hooks。 */ import { AsyncLocalStorage } from 'node:async_hooks'; export type SqlCommentRequestStore = { /** 当前登录员工 id(live.employees.id) */ userId?: number; }; const als = new AsyncLocalStorage(); export function runWithSqlCommentRequestContext( store: SqlCommentRequestStore, fn: () => T, ): T { return als.run(store, fn); } /** 当前请求上下文中的登录人 id;无上下文或未登录时返回 undefined */ export function getSqlCommentRequestUserId(): number | undefined { const id = als.getStore()?.userId; return typeof id === 'number' && Number.isFinite(id) ? id : undefined; }