Bird
Raised Fist0
NestJSframework~20 mins

TypeORM module setup in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
TypeORM Mastery in NestJS
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this TypeORM connection setup in NestJS?
Consider this NestJS module setup using TypeORM. What will be the behavior when the application starts?
NestJS
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './user.entity';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'sqlite',
      database: ':memory:',
      entities: [User],
      synchronize: true,
    }),
  ],
})
export class AppModule {}
AThe app connects to an in-memory SQLite database and auto-creates the User table on startup.
BThe app throws a runtime error because ':memory:' is not a valid database path.
CThe app connects but does not create any tables because synchronize is false by default.
DThe app connects to a file-based SQLite database named ':memory:' on disk.
Attempts:
2 left
💡 Hint
Check the meaning of 'synchronize' and the special SQLite ':memory:' database.
📝 Syntax
intermediate
1:30remaining
Which option correctly imports TypeORM entities in NestJS module?
You want to register multiple entities in your TypeORM module. Which import syntax is correct?
NestJS
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './user.entity';
import { Post } from './post.entity';

@Module({
  imports: [
    TypeOrmModule.forFeature(_____)
  ],
})
export class AppModule {}
A[User; Post]
B{User, Post}
C[User, Post]
DUser, Post
Attempts:
2 left
💡 Hint
Remember how arrays are declared in JavaScript/TypeScript.
🔧 Debug
advanced
2:30remaining
Why does this TypeORM module setup cause a runtime error?
Examine the following NestJS TypeORM configuration. Why will it fail at runtime?
NestJS
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './user.entity';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'user',
      password: 'pass',
      database: 'testdb',
      entities: [User],
      synchronize: true,
    }),
  ],
})
export class AppModule {}
AThe 'entities' array must contain strings with file paths, not classes.
BThe app will fail if the PostgreSQL server is not running or credentials are wrong.
CThe app will fail because the 'password' field is missing in the config.
DThe 'synchronize' option cannot be true for PostgreSQL databases.
Attempts:
2 left
💡 Hint
Check the database server status and credentials correctness.
❓ state_output
advanced
2:00remaining
What is the value of 'connection.isConnected' after this setup?
Given this NestJS TypeORM setup, what will be the value of connection.isConnected after app initialization?
NestJS
import { Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';

@Injectable()
export class DatabaseService {
  constructor(private dataSource: DataSource) {}

  async checkConnection() {
    return this.dataSource.isInitialized && this.dataSource.isConnected;
  }
}

// AppModule setup
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { DatabaseService } from './database.service';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'sqlite',
      database: ':memory:',
      entities: [],
      synchronize: true,
    }),
  ],
  providers: [DatabaseService],
})
export class AppModule {}
Atrue
Bfalse
Cundefined
Dthrows an error
Attempts:
2 left
💡 Hint
Consider what happens when TypeORM finishes initialization successfully.
🧠 Conceptual
expert
3:00remaining
Which statement about TypeORM module setup in NestJS is correct?
Select the one correct statement about configuring TypeORM in a NestJS application.
AThe synchronize option should always be set to false in development to avoid data loss.
BTypeOrmModule.forFeature() is used to configure global database connection options.
CEntities must be registered only in forFeature(), not in forRoot().
DTypeOrmModule.forRoot() must be called only once in the root module to establish a single database connection.
Attempts:
2 left
💡 Hint
Think about the purpose of forRoot() vs forFeature() methods.

Practice

(1/5)
1. What is the main purpose of using TypeOrmModule.forRoot() in a NestJS application?
easy
A. To handle user authentication
B. To configure the database connection and entities for TypeORM
C. To create REST API endpoints automatically
D. To manage application state globally

Solution

  1. Step 1: Understand the role of TypeOrmModule.forRoot()

    This method sets up the database connection and registers entities for TypeORM in NestJS.
  2. Step 2: Identify what it does not do

    It does not create REST endpoints, handle authentication, or manage global state.
  3. Final Answer:

    To configure the database connection and entities for TypeORM -> Option B
  4. Quick Check:

    TypeOrmModule.forRoot() = Database setup [OK]
Hint: Remember: forRoot sets up DB connection and entities [OK]
Common Mistakes:
  • Thinking it creates API routes
  • Confusing it with authentication modules
  • Assuming it manages app state
2. Which of the following is the correct way to import and configure TypeORM for PostgreSQL in a NestJS module?
easy
A. import { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [TypeOrmModule.forRoot({ type: 'sqlite', database: ':memory:', entities: [User], synchronize: false, })], })
B. 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, })], })
C. 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, }), })], })
D. 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, })], })

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]
Hint: 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
3. Given this TypeORM configuration snippet in a NestJS app:
TypeOrmModule.forRoot({
  type: 'sqlite',
  database: ':memory:',
  entities: [User],
  synchronize: true,
})
What will happen when the app starts?
medium
A. An in-memory SQLite database is created, tables for User entity are auto-created
B. A persistent SQLite file database is created on disk
C. The app throws an error because ':memory:' is invalid
D. The User entity tables are not created automatically

Solution

  1. Step 1: Understand ':memory:' in SQLite config

    Using ':memory:' creates a temporary in-memory database that exists only while the app runs.
  2. Step 2: Effect of synchronize: true

    With synchronize: true, TypeORM automatically creates tables for all entities (like User) on app start.
  3. Final Answer:

    An in-memory SQLite database is created, tables for User entity are auto-created -> Option A
  4. Quick Check:

    ':memory:' + synchronize: true = auto in-memory tables [OK]
Hint: ':memory:' means temporary DB; synchronize true creates tables [OK]
Common Mistakes:
  • Thinking ':memory:' creates a file on disk
  • Assuming tables need manual creation
  • Believing ':memory:' is invalid syntax
4. You wrote this TypeORM setup in your NestJS app:
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?
medium
A. The User entity class is not imported or registered correctly in entities array
B. The database connection details are incorrect
C. synchronize: true is not allowed with Postgres
D. TypeOrmModule.forRoot() must be called inside a service, not a module

Solution

  1. Step 1: Analyze the error message

    "Entity metadata not found for User" means TypeORM cannot find the User entity definition.
  2. Step 2: Check entities registration

    This usually happens if the User entity class is not imported or not included properly in the entities array of the config.
  3. Final Answer:

    The User entity class is not imported or registered correctly in entities array -> Option A
  4. Quick Check:

    Missing entity import = metadata error [OK]
Hint: Always import and add entities to entities array [OK]
Common Mistakes:
  • Forgetting to import entity classes
  • Misplacing entities array outside forRoot config
  • Assuming synchronize causes this error
5. You want to set up TypeORM in a NestJS app to connect to a MySQL database, but only synchronize the schema during development, not in production. Which setup correctly achieves this?
hard
A. TypeOrmModule.forRoot({ type: 'mysql', host: 'localhost', port: 3306, username: 'root', password: 'pass', database: 'appdb', entities: [User], synchronize: false, })
B. TypeOrmModule.forRoot({ type: 'mysql', host: 'localhost', port: 3306, username: 'root', password: 'pass', database: 'appdb', entities: [User], synchronize: true, })
C. TypeOrmModule.forRoot({ type: 'mysql', host: 'localhost', port: 3306, username: 'root', password: 'pass', database: 'appdb', entities: [User], synchronize: process.env.NODE_ENV === 'development', })
D. TypeOrmModule.forRootAsync({ useFactory: () => ({ type: 'mysql', host: 'localhost', port: 3306, username: 'root', password: 'pass', database: 'appdb', entities: [User], synchronize: false, }), })

Solution

  1. Step 1: Understand conditional synchronize setting

    To synchronize only in development, synchronize should be true only when NODE_ENV is 'development'.
  2. Step 2: Check options for conditional synchronize

    TypeOrmModule.forRoot({ type: 'mysql', host: 'localhost', port: 3306, username: 'root', password: 'pass', database: 'appdb', entities: [User], synchronize: process.env.NODE_ENV === 'development', }) uses synchronize: process.env.NODE_ENV === 'development', which correctly sets synchronize true only in development.
  3. Step 3: Review other options

    TypeOrmModule.forRoot({ type: 'mysql', host: 'localhost', port: 3306, username: 'root', password: 'pass', database: 'appdb', entities: [User], synchronize: true, }) always synchronizes (bad for production). TypeOrmModule.forRoot({ type: 'mysql', host: 'localhost', port: 3306, username: 'root', password: 'pass', database: 'appdb', entities: [User], synchronize: false, }) never synchronizes (bad for development). TypeOrmModule.forRootAsync({ useFactory: () => ({ type: 'mysql', host: 'localhost', port: 3306, username: 'root', password: 'pass', database: 'appdb', entities: [User], synchronize: false, }), }) uses forRootAsync but sets synchronize false always, so no conditional sync.
  4. Final Answer:

    synchronize: process.env.NODE_ENV === 'development' -> Option C
  5. Quick Check:

    Conditional synchronize = process.env.NODE_ENV check [OK]
Hint: Use environment variable to toggle synchronize true/false [OK]
Common Mistakes:
  • Always setting synchronize true in production
  • Never enabling synchronize during development
  • Not using environment variables for config
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard