Lesson 15 / 34
Database Integration
Connect NestJS to a relational database with @nestjs/typeorm and configure the connection in the root module.
TypeORM setup
Install @nestjs/typeorm and typeorm. Configure in your root module.
npm install @nestjs/typeorm typeorm pgRegister with NestJS
Use TypeOrmModule.forRoot() to configure the connection.
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'user',
password: 'password',
database: 'mydb',
entities: [User, Post],
synchronize: true
})
]
})
export class AppModule {}