@inso_web/els-mcp
Version:
MCP-сервер поверх INSO Error Logs Service. Read-only tools (search, analytics, fingerprinting, correlations) для подключения Claude Desktop/Code и ChatGPT к логам ошибок. Streamable HTTP transport + stdio для npx-запуска.
65 lines • 2.99 kB
JavaScript
/**
* Тонкая обёртка над Prisma client, который **опционально** доступен.
*
* Подход:
* - Импорт `@prisma/client` (path: ../../node_modules/.prisma/mcp) делается
* лениво и в try/catch — если пакет не сгенерирован, audit/billing
* модули переходят в no-op режим.
* - Не требуем live Postgres для тестов / dev — все `prisma.*`-операции
* в тестах подменяются через `vi.mock`.
*
* Контракт:
* - `getPrisma()` возвращает либо живой client, либо null (если MCP_DATABASE_URL
* не задан или пакет не сгенерирован).
* - Идемпотентен: повторные вызовы возвращают тот же instance.
*/
let cachedClient = null;
let attempted = false;
/**
* Lazy init. Если пакет недоступен — возвращает null, логирует warn.
*/
export async function getPrisma(opts = {}) {
if (cachedClient)
return cachedClient;
if (attempted)
return null;
attempted = true;
if (!opts.databaseUrl || opts.databaseUrl.length === 0) {
opts.log?.debug?.('MCP_DATABASE_URL not set; audit/billing in no-op mode');
return null;
}
try {
// Динамический импорт — не падаем, если @prisma/client не сгенерирован.
// Path указывает на custom-output из schema.prisma. Спецификатор —
// переменная (не литерал), чтобы TS не пытался резолвить путь во время
// компиляции (модуль может физически отсутствовать).
const segments = ['..', '..', 'node_modules', '.prisma', 'mcp', 'index.js'];
const modPath = segments.join('/');
const mod = (await import(modPath /* @vite-ignore */).catch(() => null));
if (!mod || typeof mod.PrismaClient !== 'function') {
opts.log?.warn?.('MCP Prisma client not generated (node_modules/.prisma/mcp). Run `npm run prisma:generate`. Audit/billing disabled.');
return null;
}
cachedClient = new mod.PrismaClient({
datasources: { db: { url: opts.databaseUrl } },
});
opts.log?.info?.('MCP Prisma client initialised');
return cachedClient;
}
catch (err) {
opts.log?.warn?.({ err }, 'Failed to init MCP Prisma client; audit/billing disabled');
return null;
}
}
/**
* Для тестов — позволяет инжектировать mock-client.
*/
export function setPrismaForTests(client) {
cachedClient = client;
attempted = true;
}
export function resetPrismaForTests() {
cachedClient = null;
attempted = false;
}
//# sourceMappingURL=prisma.js.map