Performance: TypeORM module setup
MEDIUM IMPACT
This affects the initial page load speed and backend response time by controlling database connection setup and query efficiency.
TypeOrmModule.forRootAsync({
useFactory: () => ({
type: 'postgres',
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
entities: [User, Product],
synchronize: false,
keepConnectionAlive: true
})
})TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'user',
password: 'pass',
database: 'db',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
keepConnectionAlive: false
})| Pattern | DB Connections | Startup Delay | Schema Sync | Verdict |
|---|---|---|---|---|
| Basic forRoot with synchronize true | Multiple on reload | High (200-500ms) | Runs every startup | [X] Bad |
| forRootAsync with keepConnectionAlive and synchronize false | Single persistent | Lower (100-200ms) | Disabled in production | [OK] Good |