0
0
NestJSframework~20 mins

TypeORM module setup in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 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.