1001Ferramentas
๐Ÿงฌ Generators

TypeORM Entity Generator from Table

Generates a TypeORM (TypeScript) class with @Entity, @PrimaryGeneratedColumn and basic columns from a table name.

โ€”

TypeORM entities: decorators, relations, and migrations

TypeORM is a TypeScript / JavaScript ORM created by Umed Khudoiberdiev in 2016 and adopted widely by NestJS, Adonis, and many enterprise Node back-ends. Its signature feature is decorator-based entity definitions: classes annotated with @Entity, @Column, and relation decorators double as runtime models, migration sources, and type definitions. Because the same class drives the database schema and the application code, the type system catches the kind of mismatches that plague hand-written SQL plus model classes.

This generator scaffolds a basic entity for a single table; the reference below covers the full decorator catalogue, the Active Record vs Data Mapper trade-off, the query builder, migrations, soft deletes, and how TypeORM compares to Prisma, Sequelize, and the newer alternatives gaining momentum.

Decorators and a complete example

import {
  Entity, PrimaryGeneratedColumn, Column,
  OneToMany, ManyToOne, CreateDateColumn,
  UpdateDateColumn, DeleteDateColumn, Index,
} from 'typeorm';

@Entity()
@Index(['email'], { unique: true })
export class User {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ length: 100 })
  name: string;

  @Column({ unique: true })
  email: string;

  @OneToMany(() => Post, post => post.author)
  posts: Post[];

  @CreateDateColumn() createdAt: Date;
  @UpdateDateColumn() updatedAt: Date;
  @DeleteDateColumn() deletedAt: Date | null;
}

The main decorators: @Entity, @Column, @PrimaryGeneratedColumn('uuid' | 'increment' | 'rowid'), @OneToOne, @OneToMany, @ManyToOne, @ManyToMany + @JoinTable, @Index, @Unique, @Check, @CreateDateColumn, @UpdateDateColumn, @DeleteDateColumn (soft delete), and @VersionColumn for optimistic locking.

Active Record vs Data Mapper, repositories, and query builder

TypeORM is one of the few ORMs that supports both patterns. Active Record attaches persistence methods to the entity (user.save(), User.findOne()) โ€” terse, but couples models to the database. Data Mapper separates entities from repositories (userRepo.save(user)) โ€” more boilerplate, but easier to test and the default in NestJS. The QueryBuilder gives a type-safe alternative to raw SQL for complex queries:

const result = await userRepo
  .createQueryBuilder('user')
  .leftJoinAndSelect('user.posts', 'post')
  .where('user.createdAt > :since', { since })
  .orderBy('user.createdAt', 'DESC')
  .take(50)
  .getMany();

Migrations and soft delete

typeorm migration:generate diffs your entity classes against the current database schema and emits a TypeScript migration with up() and down() methods; typeorm migration:run applies pending ones in order. The metadata is stored in a migrations table. Soft delete uses @DeleteDateColumn: calling repo.softDelete(id) fills the column with the current timestamp, and subsequent find()s exclude soft-deleted rows automatically. To recover a row, call repo.restore(id); to include trashed rows, use withDeleted: true.

Database support and ecosystem

  • Supported drivers: PostgreSQL, MySQL, MariaDB, SQLite, Oracle, MS SQL Server, CockroachDB, and partial MongoDB.
  • NestJS ships @nestjs/typeorm with module-level injection of repositories โ€” the traditional default before Prisma rose.
  • vs Prisma: Prisma is schema-first (one Prisma file generates a fully typed client). TypeORM is class-first. Prisma's DX and TS inference are sharper; TypeORM is more flexible for legacy schemas and ad-hoc queries.
  • vs Sequelize: Sequelize predates TS and bolted types on later; TypeORM is TS-native. Sequelize is more battle-tested for raw throughput.
  • Alternatives gaining traction: MikroORM (similar decorator approach plus Unit of Work and Identity Map), Drizzle (a lean SQL-builder with TS inference), and Kysely (typed query builder without an ORM layer).

FAQ

Did Prisma replace TypeORM? No โ€” they coexist. Greenfield projects often pick Prisma for the DX; existing TypeORM codebases stay productive, especially with complex joins or legacy schemas. Both are valid choices in 2026.

Is it NestJS-friendly? Very. @nestjs/typeorm integrates entities into modules and injects repositories into services with one decorator. Many NestJS tutorials still default to TypeORM.

How do I soft delete? Add a @DeleteDateColumn to the entity and call repo.softDelete() instead of delete(). To list trashed rows, set withDeleted: true on the find options.

Should I worry about momentum? Maintenance slowed in 2022โ€“2023 after the original maintainer stepped back, but the project is still active and widely deployed. If you're starting fresh and want the most active ecosystem, look at Prisma, Drizzle, or MikroORM; if you already have a TypeORM codebase, there's no rush to migrate.

Related Tools