@schemat/parser-typeorm
v0.2.1
Published
TypeORM parser for Schemat (entity decorators -> IR, static AST).
Maintainers
Readme
@schemat/parser-typeorm
TypeORM parser for Schemat: turns
TypeORM entity classes 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-typeormUsage
import { typeormParser } from "@schemat/parser-typeorm";
const ir = await typeormParser.parse({ projectPath: "." });
// Or point at specific entity files:
const ir2 = await typeormParser.parse({ projectPath: ".", files: ["src/user.entity.ts"] });typeormParser implements the SchemaParser interface (detect + parse) from
@schemat/core and returns an
IRSchema.
Detection
Detects a TypeORM project when any of these hold:
typeormis a dependency inpackage.json, or- a
*.entity.tsfile exists, or - any
.tsfile contains an@Entity(decorator.
What it maps
@Entity('name')classes → tables (name from the decorator arg /nameoption, else the class name)@Column/@PrimaryColumn/@PrimaryGeneratedColumn/@CreateDateColumn/ … → columns; readsnullable,unique,default,type,name/fieldName,length@PrimaryColumn/@PrimaryGeneratedColumn→ primary keys (@PrimaryGeneratedColumn('uuid')→uuidtype)@Column({ type: 'enum', enum: … })→ enums (identifier enums are resolved fromenum X {}declarations; inline arrays get a synthetic<table>_<col>_enumname)- Relations →
@ManyToOne/@OneToOne= the FK-owning side (mapped toone-to-many/one-to-one);@ManyToMany+@JoinTable=many-to-many;@OneToManyis skipped (it's the inverse of a@ManyToOne, avoiding a duplicate edge). Relation targets referenced by class (() => User) are resolved to that entity's real table name.
Limitations (v1)
- Synthetic FK columns. TypeORM doesn't always name the join column in the
decorator, so a relation's
fromColumnsuses the<property>Idconvention andtoColumnsdefaults to['id']. Explicit@JoinColumn({ name, referencedColumnName }), non-idprimary keys, and composite keys aren't read yet. - Unresolved relation targets are skipped. A relation to a class that isn't a
parsed
@Entity(e.g. in a file outside the parsed set) is dropped rather than emitted as an edge to a nonexistent table. Pass all entity files (the default walk includes them) to capture every relation. - Enums referenced across files.
@Column({ type: 'enum', enum: SomeEnum })resolvesSomeEnum's values only when itsenumdeclaration is among the parsed files; otherwise the enum is emitted with its name and empty values. - Column type falls back to the property's TS type when no
typeoption is given (number,string,boolean,Date→timestamp).
License
MIT © Ali Reza Hamid
