Does NestJS Work With TypeORM?

Fully CompatibleLast verified: 2026-02-20

NestJS and TypeORM integrate seamlessly—NestJS provides first-class TypeORM support through its official @nestjs/typeorm package, making database integration intuitive and production-ready.

Quick Facts

Compatibility
full
Setup Difficulty
Easy
Official Integration
Yes ✓
Confidence
high
Minimum Versions
NestJS: 8.0.0
TypeORM: 0.2.30

How NestJS Works With TypeORM

NestJS has native TypeORM integration through the @nestjs/typeorm package, which handles database connection setup, entity registration, and dependency injection seamlessly. The framework's module system aligns perfectly with TypeORM's repository pattern—you define entities, inject repositories into services, and let NestJS manage the lifecycle. This reduces boilerplate significantly compared to Express + TypeORM setups.

The developer experience is smooth: you define TypeORM entities as decorated classes, configure the database connection in your root module, and immediately access repositories through constructor injection. NestJS handles connection pooling, transaction management, and middleware integration automatically. Migrations work through TypeORM's CLI unchanged. The architecture encourages clean separation between controllers, services, and data access layers, which TypeORM repositories facilitate naturally.

One important consideration: NestJS's dependency injection system means you typically inject Repository<Entity> or custom repositories into services rather than using TypeORM's DataSource directly. This keeps concerns separated and testing easier with mock repositories. Error handling, validation, and serialization integrate smoothly with NestJS pipes and interceptors.

Best Use Cases

Building REST APIs with complex relational databases and multiple entities with associations
Enterprise applications requiring migrations, transactions, and strict type safety across models
Real-time applications combining NestJS WebSockets with TypeORM database synchronization
Microservices where each service has its own database and needs reliable ORM abstraction

Quick Setup

bash
npm install @nestjs/typeorm typeorm mysql2
typescript
// app.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './users/user.entity';
import { UsersService } from './users/users.service';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'password',
      database: 'mydb',
      entities: [User],
      synchronize: true,
    }),
    TypeOrmModule.forFeature([User]),
  ],
  providers: [UsersService],
})
export class AppModule {}

// users.service.ts
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './user.entity';

@Injectable()
export class UsersService {
  constructor(
    @InjectRepository(User)
    private usersRepo: Repository<User>,
  ) {}

  async findAll() {
    return this.usersRepo.find();
  }
}

Known Issues & Gotchas

warning

Circular dependency when entities reference each other bidirectionally

Fix: Use @Inject() decorator with forward references or split entity registration across separate modules. Consider using lazy relations with QueryBuilder instead of eager loading.

warning

TypeORM migrations don't automatically run on production startup

Fix: Explicitly call migration.run() in your main.ts or use a separate migration service. Document your deployment pipeline clearly.

critical

Repository<Entity> injection fails silently if entity not registered in TypeOrmModule.forFeature()

Fix: Always explicitly list entities in forFeature() for each module. Errors appear at runtime, not compile time.

warning

QueryBuilder performance issues with large datasets and N+1 queries

Fix: Use leftJoinAndSelect, eager loading, or batch queries. Profile with explain() and monitor database logs.

Alternatives

  • NestJS + Prisma: Modern ORM with excellent DX, but less mature ecosystem for complex queries
  • Express.js + TypeORM: More flexible but requires manual setup; less opinionated than NestJS
  • NestJS + Sequelize: Strong for relational databases but heavier than TypeORM, less TypeScript-first design

Resources

Related Compatibility Guides

Explore more compatibility guides