#!/usr/bin/env node import pg from 'pg'; import dotenv from 'dotenv'; import path from 'path'; import { fileURLToPath } from 'url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); dotenv.config({ path: path.join(__dirname, '..', '.env') }); const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL }); async function q(label, sql) { console.log(`\n--- ${label} ---`); console.log(JSON.stringify((await pool.query(sql)).rows, null, 2)); } async function main() { await q('demo customers usage', `SELECT c.customer_code, c.company_name, p.username, (SELECT count(*)::int FROM orders o WHERE o.customer_code=c.customer_code) AS order_count FROM customers c LEFT JOIN customer_portal_users cpu ON cpu.customer_code=c.customer_code LEFT JOIN profiles p ON p.id=cpu.user_id WHERE c.customer_code IN ('CUST001','CUST002') OR c.company_name LIKE '%(客户)%'`); await q('exception tickets', `SELECT * FROM exception_tickets LIMIT 5`); await q('E2E test profiles', `SELECT id, username FROM profiles WHERE id::text ~ '^(a0000000|b0000000)-'`); await q('orphan purchase no order', `SELECT count(*)::int c FROM purchase_orders po WHERE po.order_code IS NOT NULL AND NOT EXISTS (SELECT 1 FROM orders o WHERE o.order_code=po.order_code)`); await q('orphan prep no order', `SELECT count(*)::int c FROM supplier_preparations sp WHERE sp.order_code IS NOT NULL AND NOT EXISTS (SELECT 1 FROM orders o WHERE o.order_code=sp.order_code)`); await q('inventory without inspection', `SELECT count(*)::int c FROM inventory i WHERE NOT EXISTS (SELECT 1 FROM inspections ins WHERE ins.id=i.source_id) AND i.source_type='inspection'`); await q('BR001 business rows', `SELECT 'customers' t, count(*)::int c FROM customers WHERE branch_code='BR001' UNION ALL SELECT 'orders', count(*)::int FROM orders WHERE branch_code='BR001' UNION ALL SELECT 'categories', count(*)::int FROM categories WHERE branch_code='BR001'`); await q('notifications count', `SELECT count(*)::int c FROM notifications`); await q('operation_logs recent', `SELECT count(*)::int c FROM operation_logs WHERE created_at > NOW() - INTERVAL '30 days'`); await pool.end(); } main().catch((e) => { console.error(e); process.exit(1); });