Skip to content

Formatters & Code

JSON to TypeORM Entity

TypeORM @Entity class with decorators and a generated id.

Runs in your browser
JSON · source
lines: 17chars: 261size: 261 B
TypeORM entity · result
lines: 31chars: 545size: 545 B
live

Understanding JSON → TypeORM

Decorators, classes, and a repository.

Why TypeORM looks like Hibernate translated to TypeScript, and where it sits among the modern ORM choices.

Active record meets data mapper.

TypeORM offers two patterns. Active record — entity classes have methods like save(), remove() — feels like Ruby on Rails. Data mapper — a separate Repository<User> handles persistence — feels like Hibernate / Doctrine. The same entity class works with either pattern; pick the one that fits your project's style. Modern codebases tend toward data mapper for testability.

Decorators do the schema.

Every column, primary key, relation and index is a decorator on a class property. @Entity() marks the class as a table; @PrimaryGeneratedColumn() gives you an auto-increment id; @Column() with options handles the rest; @OneToMany / @ManyToOne wire relations. The experimental decorators feature is required in tsconfig.json experimentalDecorators: true plus emitDecoratorMetadata: true.

A worked example.

From { "id": 7, "email": "a@b.com", "name": "Q", "createdAt": "2026-..." }: import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn } from "typeorm"; @Entity() export class User { @PrimaryGeneratedColumn() id!: number; @Column({ unique: true }) email!: string; @Column() name!: string; @CreateDateColumn() createdAt!: Date; } @CreateDateColumn is the convention shortcut for "set this to the current timestamp on insert"; the underlying SQL is DEFAULT CURRENT_TIMESTAMP.

Decorator equivalents

@PrimaryGeneratedColumn, @CreateDateColumn

Conventional shortcuts replace verbose @Column option blocks.

Each decorator declares table column + type + default

= Migration-ready entity

Relations need both sides.

A @ManyToOne on the child entity needs a matching @OneToMany on the parent. TypeORM uses both sides to generate the right SQL and to enable eager / lazy loading. Forgetting the inverse side is a common bug: the relation works for one direction and silently fails for the other. Codegen tools that detect a nested object should emit both sides.

Migrations are explicit.

Unlike Prisma's migration auto-generation, TypeORM offers two modes: synchronize: true (the schema auto-updates on app start — for development only) and explicit migrations generated with typeorm migration:generate. Production deployments must use the explicit form; synchronize on prod has lost real data more than once. The inferred entity is the starting point; the migration captures the diff.

TypeORM vs the alternatives.

TypeORM is the right choice for projects that came from a Java/Hibernate or Doctrine background and want a familiar style. It's a worse choice for greenfield TS projects in 2026 — Drizzle is closer to SQL, Prisma is closer to ergonomic ORMs, both have more momentum. TypeORM's metadata-reflection mechanism has had recurring rough edges and the project's maintenance pace has been uneven. Still shippable, still maintained; just no longer the default.

Frequently asked questions

Quick answers.

How does the tool determine data types?

It inspects the values in your JSON to map strings to `string`, numbers to `number`, and booleans to `boolean`. For complex objects or arrays, it generates relational skeletons or `jsonb` column types.

Are the generated decorators customisable?

The tool uses standard TypeORM defaults for decorators. You can manually adjust the generated `varchar` lengths or column names once the code is in your IDE.

Does this support nested objects?

Yes, nested objects are detected and can be represented as embedded entities or separate classes depending on your requirements. You may need to manually add `@OneToOne` or `@ManyToOne` decorators for complex relationships.

Is my JSON data secure?

The conversion logic is executed entirely via client-side JavaScript. Your JSON input and the resulting TypeScript output are never sent to a server.

People also search for

Related tools

More in this room.

See all in Formatters & Code