NestJS
Database Setup
Configuring a database connection in NestJS (e.g., using TypeORM with PostgreSQL).
Install Packages: Install TypeORM and the database driver (PostgreSQL in this example).
terminal
bash
npm install @nestjs/typeorm typeorm pg
App Module Configuration: Configure TypeOrmModule in the root application module.
src/app.module.ts
typescript
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'db_user',
password: 'db_password',
database: 'my_database',
autoLoadEntities: true,
synchronize: true, // Set to false in production
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}