Generates a JSON Schema (and registers it via registerEntityJsonSchema)
directly from a Postgres CREATE TABLE statement. Lets a consumer keep
latest-data/supabase/migrations/*.sql as the single source of truth for
column shapes — no hand-maintained TypeScript schema duplicates.
Deliberately regex-based: a real SQL parser is overkill for the slice of
DDL we care about, and would add a non-trivial runtime dep.
required is populated from NOT NULL columns that lack a DEFAULT.
Callers can pass overrides to deep-merge a partial config (typically to
narrow a JSONB column to an array, or to add enum/format hints).
schema-from-sql.ts
Generates a JSON Schema (and registers it via
registerEntityJsonSchema) directly from a PostgresCREATE TABLEstatement. Lets a consumer keeplatest-data/supabase/migrations/*.sqlas the single source of truth for column shapes — no hand-maintained TypeScript schema duplicates.Deliberately regex-based: a real SQL parser is overkill for the slice of DDL we care about, and would add a non-trivial runtime dep.
Type mapping
UUID, TEXT, VARCHAR() -> { type: "string" } INTEGER, INT, BIGINT, SMALLINT -> { type: "integer" } NUMERIC(), DECIMAL(*) -> { type: "number" } BOOLEAN, BOOL -> { type: "boolean" } TIMESTAMPTZ, TIMESTAMP -> { type: "string", format: "date-time" } DATE -> { type: "string", format: "date" } JSONB, JSON -> { type: "object" } TEXT[] -> { type: "array", items: { type: "string" } } anything else -> { type: "string" } + "x-warning"
requiredis populated fromNOT NULLcolumns that lack aDEFAULT. Callers can passoverridesto deep-merge a partial config (typically to narrow a JSONB column to an array, or to addenum/formathints).