import fs from 'node:fs/promises'; import path from 'node:path'; import { createRequire } from 'node:module'; // Read the platform configured in THIS repository, never another application's database. // PostgreSQL enforces a read-only transaction; no schema change, import or seed runs here. const require = createRequire(path.resolve('package.json')); require('@next/env').loadEnvConfig(process.cwd(), false, { info() {}, error() {}, warn() {} }); const { PrismaClient } = require('@prisma/client'); const url = new URL(process.env.DATABASE_URL); const prisma = new PrismaClient({ log: [] }); const output = path.resolve(process.argv[2] || 'output/pi-compatibility-20261021/platform-catalog.json'); try { const value = await prisma.$transaction(async tx => { await tx.$executeRawUnsafe('SET TRANSACTION READ ONLY'); const shops = await tx.category.findMany({ select: { id: true, name: true }, orderBy: { name: 'asc' } }); const categories = await tx.productCategory.findMany({ select: { id: true, name: true }, orderBy: { name: 'asc' } }); return { shops, categories }; }, { maxWait: 5000, timeout: 15000 }); const snapshot = { source: 'this-repository-configured-platform-readonly', capturedAt: new Date().toISOString(), database: { host: url.hostname, name: url.pathname.slice(1) }, ...value }; await fs.mkdir(path.dirname(output), { recursive: true }); await fs.writeFile(output, JSON.stringify(snapshot, null, 2)); console.log(JSON.stringify(snapshot, null, 2)); } catch (error) { console.error(JSON.stringify({ error: '当前项目目录只读查询失败', code: error.code || error.name })); process.exitCode = 1; } finally { await prisma.$disconnect(); }