NestJS NestJS Database Integration & ORMs 2 — Questions and Answers
Question 1: How do you configure TypeORM for asynchronous configuration (e.g., from ConfigService) in NestJS?
- TypeOrmModule.forRoot() with a factory
- TypeOrmModule.forRootAsync() with useFactory (Correct answer)
- TypeOrmModule.configure() with async option
- DatabaseModule.withConfig() factory
Correct answer: TypeOrmModule.forRootAsync() with useFactory
TypeOrmModule.forRootAsync() accepts a useFactory (or useClass/useExisting) that can inject ConfigService and return the database options asynchronously.
Question 2: What does the `synchronize: true` TypeORM option do and why is it dangerous in production?
- It syncs multiple databases together
- It automatically alters the DB schema to match entities — risking data loss on production (Correct answer)
- It synchronizes database replicas
- It keeps entity files in sync with migration files
Correct answer: It automatically alters the DB schema to match entities — risking data loss on production
`synchronize: true` auto-runs ALTER TABLE statements on startup to match entity definitions, which can drop columns or tables with production data.
Question 3: Which Prisma CLI command generates the Prisma Client after schema changes?
- prisma db push
- prisma generate (Correct answer)
- prisma migrate dev
- prisma client build
Correct answer: prisma generate
`prisma generate` reads the schema.prisma file and generates the type-safe Prisma Client that your NestJS services use.
Question 4: How do you implement soft deletes in TypeORM within a NestJS application?
- Add `deleted: boolean` and filter manually in every query
- Use the @DeleteDateColumn() decorator and call repository.softDelete() (Correct answer)
- Set `softDelete: true` in TypeOrmModule.forRoot()
- Use a TypeORM AfterRemove subscriber
Correct answer: Use the @DeleteDateColumn() decorator and call repository.softDelete()
@DeleteDateColumn() adds a nullable timestamp column; repository.softDelete() sets it instead of removing the row, and find queries automatically exclude soft-deleted records.
Question 5: What is the N+1 query problem in TypeORM, and how is it solved?
- Running N queries then 1 aggregate — solved with GROUP BY
- Loading N related entities with N separate queries instead of 1 JOIN — solved with eager loading or QueryBuilder joins (Correct answer)
- Having N connections open — solved with connection pooling
- Calling findOne() N+1 times — solved with caching
Correct answer: Loading N related entities with N separate queries instead of 1 JOIN — solved with eager loading or QueryBuilder joins
N+1 occurs when fetching a list of N entities and then loading each one's relation separately; use eager relations or explicit JOIN in QueryBuilder to fetch everything in one query.
Question 6: Which method in a TypeORM QueryBuilder is used to add a WHERE clause with a named parameter?
- .filter('condition', params)
- .where('entity.field = :value', { value }) (Correct answer)
- .condition('field', 'value')
- .addFilter('field = ?', [value])
Correct answer: .where('entity.field = :value', { value })
.where() (or .andWhere()) accepts a string condition with named `:param` placeholders and a params object to safely parameterize queries.
How do you configure TypeORM for asynchronous configuration (e.g., from ConfigService) in NestJS?