NestJS NestJS Database Integration & ORMs 1 — Questions and Answers
Question 1: Which NestJS package provides first-class TypeORM integration?
- @nestjs/database
- @nestjs/typeorm (Correct answer)
- @nestjs/orm
- @nestjs/entity
Correct answer: @nestjs/typeorm
@nestjs/typeorm provides TypeOrmModule with forRoot() and forFeature() methods for integrating TypeORM into the NestJS DI system.
Question 2: What does `TypeOrmModule.forFeature([Entity])` do in a feature module?
- Creates the database table for the entity
- Registers the entity's repository so it can be injected in that module (Correct answer)
- Runs migrations for the specified entity
- Exports the entity to other modules
Correct answer: Registers the entity's repository so it can be injected in that module
forFeature() registers the specified entities and makes their TypeORM repositories available for injection via @InjectRepository() within that module.
Question 3: How do you inject a TypeORM repository into a NestJS service?
- @Inject(Repository<User>) private repo: Repository<User>
- @InjectRepository(User) private repo: Repository<User> (Correct answer)
- @Repository(User) private repo
- @TypeOrm(User) private repo: Repository<User>
Correct answer: @InjectRepository(User) private repo: Repository<User>
@InjectRepository(Entity) is the TypeORM-specific injection decorator that retrieves the entity's repository from the DI container.
Question 4: Which TypeORM method should be used to run database transactions in a NestJS service?
- repository.transaction()
- dataSource.transaction() or EntityManager in a transaction callback (Correct answer)
- typeOrmService.beginTransaction()
- @Transactional() decorator
Correct answer: dataSource.transaction() or EntityManager in a transaction callback
dataSource.transaction() or injecting EntityManager inside a transaction callback ensures all operations use the same database transaction.
Question 5: What is the purpose of TypeORM migrations in a NestJS production application?
- To seed the database with test data
- To safely evolve the database schema in a controlled, versioned manner (Correct answer)
- To synchronize entity definitions automatically on startup
- To back up the database before schema changes
Correct answer: To safely evolve the database schema in a controlled, versioned manner
Migrations provide versioned, reversible SQL scripts that alter the database schema without losing data — essential in production where `synchronize: true` is dangerous.
Question 6: Which NestJS-compatible ORM uses a code-first approach with a schema builder instead of decorators?
- TypeORM
- Sequelize
- Prisma (Correct answer)
- MikroORM
Correct answer: Prisma
Prisma uses a declarative schema file (schema.prisma) and generates a type-safe client, making it code-first without requiring entity class decorators.
Which NestJS package provides first-class TypeORM integration?