typeorm-entity-schema-renderer
v0.1.0
Published
Render a TypeORM EntitySchema back to a fully-decorated TypeScript entity class source
Maintainers
Readme
typeorm-entity-schema-renderer
Render a TypeORM EntitySchema back into a fully-decorated TypeScript entity class source.
Takes a new EntitySchema({...}) (or a plain options object) and produces a printable .ts source with:
@Entity/@ViewEntity/@ChildEntityclass decorators@Tree,@TableInheritance- Class-level
@Index/@Unique/@Check/@Exclusion @Column,@PrimaryColumn,@PrimaryGeneratedColumn,@CreateDateColumn,@UpdateDateColumn,@DeleteDateColumn,@VersionColumn,@Generated@ViewColumn@OneToOne/@OneToMany/@ManyToOne/@ManyToMany/@TreeParent/@TreeChildrenwith@JoinColumn/@JoinColumns/@JoinTable@RelationId- Correct TS types for fields:
Relation<T>(non-lazy),Promise<T>(lazy),T[](*-to-many),T | null(nullable), enum union, redundant column-type omission - External
import { ... } from '...'lines driven by a pluggableimportResolver
Install
npm install typeorm-entity-schema-renderertypeorm is an optional peer dependency — it is only needed if you construct input via new EntitySchema(...); the library itself has no runtime dependency on TypeORM.
Usage
import { EntitySchema } from "typeorm"
import { EntitySchemaRenderer } from "typeorm-entity-schema-renderer"
const UserSchema = new EntitySchema({
name: "User",
tableName: "users",
columns: {
id: { type: Number, primary: true, generated: "increment" },
email: { type: "varchar", length: 255, unique: true },
createdAt: { type: "timestamp", createDate: true },
},
})
const renderer = new EntitySchemaRenderer()
console.log(renderer.render(UserSchema))Output:
import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn } from 'typeorm'
@Entity({ name: "users" })
export class User {
@PrimaryGeneratedColumn("increment")
readonly id!: number;
@Column({ length: 255, unique: true })
email!: string;
@CreateDateColumn({ type: "timestamp" })
readonly createdAt!: Date;
}Custom importResolver
Control where external references (relations, embeddeds, enum classes) are imported from:
import { EntitySchemaRenderer, ImportResolver } from "typeorm-entity-schema-renderer"
const resolver: ImportResolver = (name, { kind }) => {
switch (kind) {
case "embedded":
return { path: `@/embeddeds/${name}`, name }
case "enum":
return { path: `@/enums/${name}`, name }
case "entity":
case "relation":
default:
return { path: `@/entities/${name}`, name }
}
}
const renderer = new EntitySchemaRenderer({ importResolver: resolver })ImportKind values:
| Kind | Source |
|------|--------|
| entity | parent class referenced via base |
| relation | targets of @OneToOne / @OneToMany / @ManyToOne / @ManyToMany / @TreeParent / @TreeChildren |
| embedded | targets of @Column(() => X) embeddeds |
| enum | enum classes referenced by enum columns |
The resolver may return:
{ path, name }— explicit import path and symbol name- a
string— treated as the path; the original name is kept undefined— skip the import
API
new EntitySchemaRenderer(options?)
| Option | Type | Description |
|--------|------|-------------|
| importResolver | ImportResolver | Resolves external reference names to import paths. Defaults to ./<Name> (same directory) for every kind. |
.render(schema) => string
Accepts an EntitySchema instance or a plain options object. Throws TypeError on invalid input.
.buildExternalImports(schema, options?) => string
Returns just the import { ... } from '...' block for external references. Useful if you assemble the output yourself.
License
MIT
