TypeOrmModule
Integrates TypeORM, providing repositories and connection management.
Syntax
TypeOrmModule.forRoot(options) | TypeOrmModule.forFeature([Entity]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
options | TypeOrmModuleOptions | No | DataSource options such as type, url, entities, and synchronize. |
Returns
DynamicModule — A module wiring the data source or entity repositories.
Examples
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
url: process.env.DATABASE_URL,
autoLoadEntities: true,
synchronize: false,
}),
],
})
export class AppModule {}
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Cat } from './cat.entity';
@Injectable()
export class CatsService {
constructor(
@InjectRepository(Cat) private repo: Repository<Cat>,
) {}
findAll() {
return this.repo.find();
}
}
Notes
Install @nestjs/typeorm and typeorm. Use forRoot() once for the
connection and forFeature() per module to register entity repositories.
Keep `synchronize: false` in production and use migrations.