9ffe734275
Deploy / deploy (push) Successful in 7m40s
- /api/articles[/slug]: GET/PUT/POST/DELETE, article JSON stored in PostgreSQL (schema analysis) - /api/tool-taxonomy: categories (tree), per-paper tools, each tool in one or more categories - /contents/tools-of-agents: category table with roll-up and links to the papers; removed "By kind of contribution" and "Recent papers" - content moves out of the repo: removed content/articles, load-articles.js, agent-tools.ts Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FbMbJPadPzVwvt9rU9GBNo
47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
import { Pool, types } from 'pg';
|
|
|
|
// COUNT()/SUM() come back as bigint (type 20) and numeric (1700): hand them to JS as numbers.
|
|
types.setTypeParser(20, (v) => parseInt(v, 10));
|
|
types.setTypeParser(1700, (v) => parseFloat(v));
|
|
|
|
const g = globalThis as unknown as { __pgPool?: Pool };
|
|
|
|
function pool(): Pool {
|
|
if (!g.__pgPool) {
|
|
if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is not set');
|
|
// scripts/load-pg.js publishes each build as schema "live"
|
|
g.__pgPool = new Pool({ connectionString: process.env.DATABASE_URL, max: 8, options: '-c search_path=live' });
|
|
}
|
|
return g.__pgPool;
|
|
}
|
|
|
|
/** Run SQL written with `?` placeholders (rewritten to $1, $2, ...). */
|
|
export async function query<T = any>(sql: string, params: unknown[] = []): Promise<T[]> {
|
|
let i = 0;
|
|
const text = sql.replace(/\?/g, () => `$${++i}`);
|
|
return (await pool().query(text, params as any[])).rows as T[];
|
|
}
|
|
|
|
export async function one<T = any>(sql: string, params: unknown[] = []): Promise<T | undefined> {
|
|
return (await query<T>(sql, params))[0];
|
|
}
|
|
|
|
/** Run several statements in one transaction; `q` takes `?` placeholders like query(). */
|
|
export async function tx<T>(fn: (q: <R = any>(sql: string, params?: unknown[]) => Promise<R[]>) => Promise<T>): Promise<T> {
|
|
const client = await pool().connect();
|
|
try {
|
|
await client.query('BEGIN');
|
|
const out = await fn(async (sql: string, params: unknown[] = []) => {
|
|
let i = 0;
|
|
return (await client.query(sql.replace(/\?/g, () => `$${++i}`), params as any[])).rows;
|
|
});
|
|
await client.query('COMMIT');
|
|
return out;
|
|
} catch (e) {
|
|
await client.query('ROLLBACK').catch(() => {});
|
|
throw e;
|
|
} finally {
|
|
client.release();
|
|
}
|
|
}
|