Bird
0
0

Which of the following is the correct way to import and configure TypeORM for PostgreSQL in a NestJS module?

easy📝 Syntax Q12 of 15
NestJS - Database with TypeORM
Which of the following is the correct way to import and configure TypeORM for PostgreSQL in a NestJS module?
Aimport { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [TypeOrmModule.forRoot({ type: 'sqlite', database: ':memory:', entities: [User], synchronize: false, })], })
Bimport { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [TypeOrmModule.forFeature({ type: 'postgres', host: 'localhost', port: 5432, username: 'user', password: 'pass', database: 'testdb', entities: [User], synchronize: true, })], })
Cimport { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [TypeOrmModule.forRootAsync({ useFactory: () => ({ type: 'mysql', host: 'localhost', port: 3306, username: 'user', password: 'pass', database: 'testdb', entities: [User], synchronize: true, }), })], })
Dimport { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [TypeOrmModule.forRoot({ type: 'postgres', host: 'localhost', port: 5432, username: 'user', password: 'pass', database: 'testdb', entities: [User], synchronize: true, })], })
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct method for root configuration

    TypeOrmModule.forRoot() is used to configure the main database connection with options like type, host, port, entities, and synchronize.
  2. Step 2: Check the options and syntax

    import { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [TypeOrmModule.forRoot({ type: 'postgres', host: 'localhost', port: 5432, username: 'user', password: 'pass', database: 'testdb', entities: [User], synchronize: true, })], }) correctly uses forRoot with proper PostgreSQL database config and entities array. import { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [TypeOrmModule.forFeature({ type: 'postgres', host: 'localhost', port: 5432, username: 'user', password: 'pass', database: 'testdb', entities: [User], synchronize: true, })], }) incorrectly uses forFeature for root config. import { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [TypeOrmModule.forRootAsync({ useFactory: () => ({ type: 'mysql', host: 'localhost', port: 3306, username: 'user', password: 'pass', database: 'testdb', entities: [User], synchronize: true, }), })], }) uses forRootAsync with MySQL config instead of PostgreSQL. import { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [TypeOrmModule.forRoot({ type: 'sqlite', database: ':memory:', entities: [User], synchronize: false, })], }) uses SQLite config instead of PostgreSQL.
  3. Final Answer:

    The PostgreSQL configuration using TypeOrmModule.forRoot() -> Option D
  4. Quick Check:

    forRoot() = main DB config [OK]
Quick Trick: Use forRoot() for main DB setup, forFeature() for repositories [OK]
Common Mistakes:
  • Using forFeature() instead of forRoot() for DB config
  • Confusing forRootAsync with forRoot without async needs
  • Setting synchronize false during development

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes