@schemat/parser-drizzle
v0.2.1
Published
Drizzle ORM parser for Schemat (Drizzle schema -> IR, static AST).
Maintainers
Readme
@schemat/parser-drizzle
Drizzle ORM parser for Schemat: turns
a Drizzle schema into the canonical Schemat IR by
statically parsing the TypeScript (via ts-morph) — it never connects to a
database or runs your code.
Install
npm i @schemat/parser-drizzleUsage
import { drizzleParser } from "@schemat/parser-drizzle";
const ir = await drizzleParser.parse({ projectPath: "." });
// Or point at specific schema files:
const ir2 = await drizzleParser.parse({ projectPath: ".", files: ["src/db/schema.ts"] });drizzleParser implements the SchemaParser interface (detect + parse) from
@schemat/core and returns an
IRSchema.
Detection
Detects a Drizzle project when any of these hold:
- a
drizzle.config.ts/drizzle.config.jsonexists, or drizzle-ormis a dependency and a schema file is present, or- a conventional schema file exists (
src/schema.ts,src/db/schema.ts,db/schema.ts,drizzle/schema.ts,src/drizzle/schema.ts).
What it maps
pgTable/mysqlTable/sqliteTable→ tables (name from the first string arg; column name from the builder's first arg, else the property key)- Column builders (
serial,varchar,integer, …) → column types .primaryKey()/.notNull()/.unique()/.default(...)/.defaultNow()→ column flags and defaults.references(() => table.col)→ relations; cardinality isone-to-onewhen the owning column isunique/primaryKey, elseone-to-manypgEnum('name', [...])→ enums
Because it's a static parse, a syntactically broken file yields whatever tables resolved cleanly rather than throwing.
Limitations (v1)
- Column-level
.references()only. Table-level constraints declared in the thirdpgTable(...)argument — compositeprimaryKey(...),foreignKey(...), and table-levelunique(...)— are not yet mapped. Single-column FKs via.references()are fully supported. - Cross-file / unresolved references are skipped. A
.references(() => otherTable.id)whose target table isn't among the parsed files is dropped rather than emitted as a guessed edge, so relations never point at a table that doesn't exist in the diagram. Pass all schema files (or use a single schema file) to capture every relation. .array(),.generatedAlwaysAsIdentity()and similar modifiers are not reflected in the column type/default yet.
License
MIT © Ali Reza Hamid
