db/fact_ddl.ts

Fact + memo PG schema.

Three tables:

  • fact — content-addressed bytes. hash = 'blake3:<hex64>'. Either embedded (bytes) or referenced (external_url); the CHECK constraint enforces exactly one populated. Idempotent: same bytes always produce the same hash, so INSERT … ON CONFLICT DO NOTHING is the put primitive.
  • fact_ref — declared dependency edges (source fact → target fact). target_hash is intentionally not a foreign key: in federation a reference may target a fact stored on another instance.
  • memo(fn_id, input_hash) → output_hash for memoized computations.
view source

Declarations
#

8 declarations

FACT_DROP_TABLES
#

FACT_MIGRATION_NAMESPACE
#

db/fact_ddl.ts view source

"fuz_facts" import {FACT_MIGRATION_NAMESPACE} from '@fuzdev/fuz_app/db/fact_ddl.js';

Namespace identifier for fact + memo migrations.

FACT_MIGRATION_NS
#

FACT_MIGRATIONS
#

db/fact_ddl.ts view source

Migration[] import {FACT_MIGRATIONS} from '@fuzdev/fuz_app/db/fact_ddl.js';

Fact + memo migrations.

FACT_REFS_SCHEMA
#

db/fact_ddl.ts view source

"\nCREATE TABLE IF NOT EXISTS fact_ref (\n\tsource_hash TEXT NOT NULL REFERENCES fact(hash) ON DELETE CASCADE,\n\ttarget_hash TEXT NOT NULL,\n\tPRIMARY KEY (source_hash, target_hash)\n)" import {FACT_REFS_SCHEMA} from '@fuzdev/fuz_app/db/fact_ddl.js';

fact_ref table — declared dependency edges between facts.

target_hash is not a foreign key (federation: target may live remotely).

FACT_REFS_TARGET_INDEX
#

db/fact_ddl.ts view source

"\nCREATE INDEX IF NOT EXISTS idx_fact_ref_target ON fact_ref(target_hash)" import {FACT_REFS_TARGET_INDEX} from '@fuzdev/fuz_app/db/fact_ddl.js';

Reverse lookup: which facts reference a given target?

FACTS_SCHEMA
#

db/fact_ddl.ts view source

"\nCREATE TABLE IF NOT EXISTS fact (\n\thash TEXT PRIMARY KEY,\n\tbytes BYTEA,\n\texternal_url TEXT,\n\tcontent_type TEXT,\n\tsize BIGINT NOT NULL,\n\tcreated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),\n\tCONSTRAINT fact_storage_present CHECK (bytes IS NOT NULL OR external_url IS NOT NULL)\n)" import {FACTS_SCHEMA} from '@fuzdev/fuz_app/db/fact_ddl.js';

fact table — content-addressed byte store.

MEMOS_SCHEMA
#

db/fact_ddl.ts view source

"\nCREATE TABLE IF NOT EXISTS memo (\n\tfn_id TEXT NOT NULL,\n\tinput_hash TEXT NOT NULL,\n\toutput_hash TEXT NOT NULL,\n\tcreated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),\n\tPRIMARY KEY (fn_id, input_hash)\n)" import {MEMOS_SCHEMA} from '@fuzdev/fuz_app/db/fact_ddl.js';

memo table — (fn_id, input_hash) → output_hash for memoized computations.

Depends on
#