Files
ai-taxonomy-literature-anal…/lib/analysis-schema.ts
T
claude-bot 9ffe734275
Deploy / deploy (push) Successful in 7m40s
Write APIs for articles and tool taxonomy; taxonomy table replaces the tools table
- /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
2026-10-23 19:55:21 +08:00

88 lines
3.5 KiB
TypeScript

import { query } from './db';
// Schema "analysis": persistent, written through the API (pages/api/*), never touched by the data rebuild
// (which replaces schema "live" on every deploy). Created on first use.
const DDL = `
CREATE SCHEMA IF NOT EXISTS analysis;
-- Articles: one per (paper, topic); each has ordered, nestable sections; each section is an ordered list of typed blocks.
CREATE TABLE IF NOT EXISTS analysis.article (
id bigserial PRIMARY KEY,
slug text NOT NULL UNIQUE,
subject_conf text NOT NULL, -- the paper this is about, by its stable key (conf, ext_id):
subject_ext_id text NOT NULL, -- papers.id is not stable across data rebuilds
title text NOT NULL,
summary text,
revision integer NOT NULL DEFAULT 1,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_article_subject ON analysis.article (subject_conf, subject_ext_id);
CREATE TABLE IF NOT EXISTS analysis.section (
id bigserial PRIMARY KEY,
article_id bigint NOT NULL REFERENCES analysis.article(id) ON DELETE CASCADE,
parent_id bigint REFERENCES analysis.section(id) ON DELETE CASCADE,
key text NOT NULL,
title text NOT NULL,
level integer NOT NULL DEFAULT 2,
position integer NOT NULL,
UNIQUE (article_id, key)
);
CREATE TABLE IF NOT EXISTS analysis.block (
id bigserial PRIMARY KEY,
section_id bigint NOT NULL REFERENCES analysis.section(id) ON DELETE CASCADE,
position integer NOT NULL,
type text NOT NULL CHECK (type IN ('markdown','list','callout','table','chart','mermaid','figure','code')),
data jsonb NOT NULL,
UNIQUE (section_id, position)
);
CREATE TABLE IF NOT EXISTS analysis.link (
id bigserial PRIMARY KEY,
article_id bigint NOT NULL REFERENCES analysis.article(id) ON DELETE CASCADE,
section_id bigint REFERENCES analysis.section(id) ON DELETE SET NULL,
position integer NOT NULL,
kind text NOT NULL CHECK (kind IN ('paper','code','project','demo','dataset','related','other')),
title text NOT NULL,
url text NOT NULL,
note text
);
-- Tool taxonomy: a tree of categories, and the tools that papers designed, each in one or more categories.
CREATE TABLE IF NOT EXISTS analysis.tool_category (
slug text PRIMARY KEY,
title text NOT NULL,
parent_slug text REFERENCES analysis.tool_category(slug) DEFERRABLE INITIALLY DEFERRED,
descr text,
position integer NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS analysis.paper_tool (
id bigserial PRIMARY KEY,
conf text NOT NULL, -- the paper, by its stable key (conf, ext_id)
ext_id text NOT NULL,
tool text NOT NULL,
descr text,
built_on text,
ref text, -- where in the paper the tool is described
position integer NOT NULL DEFAULT 0,
UNIQUE (conf, ext_id, tool)
);
CREATE TABLE IF NOT EXISTS analysis.paper_tool_category (
tool_id bigint NOT NULL REFERENCES analysis.paper_tool(id) ON DELETE CASCADE,
category_slug text NOT NULL REFERENCES analysis.tool_category(slug) ON DELETE CASCADE,
PRIMARY KEY (tool_id, category_slug)
);
`;
let ready: Promise<unknown> | null = null;
/** Create the analysis schema if it is missing (cached per process). */
export function ensureAnalysisSchema(): Promise<unknown> {
if (!ready) ready = query(DDL).catch((e) => { ready = null; throw e; });
return ready;
}