schema.ts25 lines · main
| 1 | import { isIdentifier, type TableDef } from './table.js'; |
| 2 | |
| 3 | export interface SchemaDef { |
| 4 | readonly version: 1; |
| 5 | readonly tables: Readonly<Record<string, TableDef>>; |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * Declare the root schema. Keys are table names as they will appear in |
| 10 | * Postgres. Per CLAUDE.md §6.1, DB table names are snake_case and plural — |
| 11 | * we validate identifier shape but leave pluralisation as a user concern. |
| 12 | */ |
| 13 | export function schema(tables: Record<string, TableDef>): SchemaDef { |
| 14 | const out: Record<string, TableDef> = {}; |
| 15 | for (const [name, def] of Object.entries(tables)) { |
| 16 | if (!isIdentifier(name)) { |
| 17 | throw new Error(`invalid table name: ${JSON.stringify(name)}`); |
| 18 | } |
| 19 | if (name.startsWith('_briven_')) { |
| 20 | throw new Error(`table name '${name}' collides with the reserved '_briven_' prefix`); |
| 21 | } |
| 22 | out[name] = def; |
| 23 | } |
| 24 | return Object.freeze({ version: 1, tables: Object.freeze(out) }); |
| 25 | } |