declaredTables
Tables the codebase declares: their definition, row schema, the row type's fields (columns), and which are unique and indexed.
import type { Codebase } from "#codebase/tcmts/cli/codebase"; import { schemaText } from "#codebase/tcmts/core/schema"; export function declaredTables(cb: Codebase): Map< string, { path: string; hash: string; schema: string; fields: { name: string; type: string }[]; unique: string[]; index: string[]; } > { const out = new Map< string, { path: string; hash: string; schema: string; fields: { name: string; type: string }[]; unique: string[]; index: string[]; } >(); for (const n of cb.store.names()) { const t = n.kind === "term" ? cb.store.getDef(n.hash)?.table : undefined; if (!t?.name) continue; const row = t.s.t === "ref" ? t.defs[t.s.id] : t.s; const fields = row?.t === "object" ? Object.entries(row.props).map(([name, p]) => ({ name, // (optional: `string?`, not `undefined | string?`) type: p.optional ? `${schemaText(p.s, t.defs).replace(/^undefined \| | \| undefined$/, "")}?` : schemaText(p.s, t.defs), })) : []; out.set(t.name, { path: n.path, hash: n.hash, schema: t.hash, fields, unique: t.unique ?? [], index: t.index ?? [], }); } return out; }