Performance: TypeORM module setup
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 |
TypeOrmModule.forRoot() in a NestJS application?TypeOrmModule.forRoot({
type: 'sqlite',
database: ':memory:',
entities: [User],
synchronize: true,
})
What will happen when the app starts?TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'user',
password: 'pass',
database: 'testdb',
entities: [User],
synchronize: true
})
But the app crashes with an error: "Entity metadata not found for User". What is the likely cause?Pass the test and this pattern moves unknown