Bird
Raised Fist0
NestJSframework~20 mins

Entity definition in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Entity Definition Master
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 NestJS entity definition?
Consider this NestJS entity using TypeORM decorators. What will be the type of the 'id' property in the database?
NestJS
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

@Entity()
export class User {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  name: string;
}
AA boolean column
BAn auto-incrementing integer column
CA plain string column without auto-generation
DA UUID string column generated automatically
Attempts:
2 left
💡 Hint
Look at the argument passed to @PrimaryGeneratedColumn decorator.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a nullable column in a NestJS entity?
You want to define a column 'description' that can be empty or null in the database. Which code snippet correctly achieves this?
NestJS
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Product {
  @PrimaryGeneratedColumn()
  id: number;

  // Your code here
}
A
@Column({ nullable: true })
description: string;
B
@Column({ optional: true })
description: string;
C
@Column({ null: true })
description: string;
D
@Column({ allowNull: true })
description: string;
Attempts:
2 left
💡 Hint
Check the official TypeORM column options for nullable fields.
🔧 Debug
advanced
2:00remaining
Why does this NestJS entity fail to save data?
Given this entity, why will saving a new instance cause an error?
NestJS
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

@Entity()
export class Order {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  total: number;

  @Column({ type: 'timestamp' })
  date: Date;
}
AThe 'date' column is missing a transformer to convert Date to database format
BThe entity is missing a constructor to initialize properties
CThe 'date' column needs the 'type' option set explicitly to 'timestamp' or 'date'
DThe 'date' property type should be 'string' instead of 'Date'
Attempts:
2 left
💡 Hint
TypeORM needs to know how to store Date objects in the database.
🧠 Conceptual
advanced
2:00remaining
What is the purpose of the @Entity decorator in NestJS with TypeORM?
Why do we use the @Entity decorator on a class in NestJS when working with TypeORM?
AIt registers the class as a service injectable in NestJS
BIt marks the class as a database table and enables ORM mapping
CIt automatically creates REST API endpoints for the class
DIt compiles the class into a standalone executable
Attempts:
2 left
💡 Hint
Think about how ORM knows which classes represent tables.
❓ state_output
expert
3:00remaining
What is the value of 'user.isActive' after saving and retrieving this entity?
Consider this entity and the following code snippet. What will be the value of 'isActive' after saving and retrieving the user from the database?
NestJS
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ default: true })
  isActive: boolean;
}

// Usage:
const user = new User();
await repository.save(user);
const savedUser = await repository.findOneBy({ id: user.id });
const result = savedUser?.isActive;
Atrue
Bfalse
Cundefined
Dnull
Attempts:
2 left
💡 Hint
Check the default value set in the @Column decorator and what happens when no value is assigned before saving.

Practice

(1/5)
1. What is the purpose of the @Entity() decorator in a NestJS application using TypeORM?
easy
A. It marks a class as a database table representation.
B. It defines a method to fetch data from the database.
C. It creates a new database connection.
D. It styles the output of a component.

Solution

  1. Step 1: Understand the role of @Entity()

    The @Entity() decorator tells NestJS and TypeORM that the class represents a database table.
  2. Step 2: Differentiate from other decorators

    Other decorators like @Column() define table columns, but @Entity() marks the whole class as a table.
  3. Final Answer:

    It marks a class as a database table representation. -> Option A
  4. Quick Check:

    @Entity() = Table marker [OK]
Hint: Remember: @Entity() means 'this class is a table' [OK]
Common Mistakes:
  • Confusing @Entity() with database connection setup
  • Thinking @Entity() defines methods or styles
  • Mixing up @Entity() with @Column()
2. Which of the following is the correct way to define a string column named title in a NestJS entity using TypeORM?
easy
A. @Column({ type: 'number' }) title: string;
B. @Column('boolean') title: string;
C. @Column() title: string;
D. @Column({ length: 255 }) title: number;

Solution

  1. Step 1: Identify the correct @Column() usage for a string

    By default, @Column() without options assumes a string type in TypeORM.
  2. Step 2: Check type consistency

    The property type should be string to match the column type.
  3. Final Answer:

    @Column() title: string; -> Option C
  4. Quick Check:

    Default @Column() = string column [OK]
Hint: Use @Column() alone for simple string columns [OK]
Common Mistakes:
  • Setting wrong type in @Column() options
  • Mismatching property type and column type
  • Using number type for string properties
3. Given the following entity code, what will be the type of the age column in the database?
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column({ type: 'int' })
  age: number;
}
medium
A. INTEGER (number)
B. VARCHAR (string)
C. BOOLEAN
D. DATE

Solution

  1. Step 1: Check the @Column() decorator on age

    The decorator specifies { type: 'int' }, which means an integer column in the database.
  2. Step 2: Match the property type

    The property age is a number, matching the integer database type.
  3. Final Answer:

    INTEGER (number) -> Option A
  4. Quick Check:

    { type: 'int' } means integer column [OK]
Hint: Look for { type: 'int' } to identify integer columns [OK]
Common Mistakes:
  • Assuming default string type without checking options
  • Confusing int with boolean or date
  • Ignoring the explicit type option in @Column()
4. Identify the error in the following entity definition:
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Product {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ type: 'varchar', length: 100 })
  name: number;
}
medium
A. Missing @Entity() decorator
B. Property type number does not match column type varchar
C. Primary key column missing
D. Incorrect import statement

Solution

  1. Step 1: Check property and column type consistency

    The name property is typed as number but the column type is varchar, which expects a string.
  2. Step 2: Confirm other parts are correct

    @Entity() is present, primary key is defined, and imports are correct.
  3. Final Answer:

    Property type number does not match column type varchar -> Option B
  4. Quick Check:

    Property type must match column type [OK]
Hint: Match property type with @Column() type options [OK]
Common Mistakes:
  • Using wrong property type for string columns
  • Forgetting to add @Entity()
  • Missing primary key decorator
5. You want to define an entity Order with an auto-generated primary key id, a string customerName limited to 150 characters, and an optional numeric discount that can be null. Which is the correct entity definition?
hard
A. import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class Order { @PrimaryGeneratedColumn() id: number; @Column({ length: 150 }) customerName: string; @Column({ type: 'int', nullable: false }) discount: number; }
B. import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class Order { @PrimaryGeneratedColumn() id: string; @Column({ length: 150 }) customerName: string; @Column({ type: 'float' }) discount: number; }
C. import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class Order { @PrimaryGeneratedColumn() id: number; @Column({ type: 'varchar', length: 150 }) customerName: number; @Column({ type: 'float', nullable: true }) discount: number; }
D. import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class Order { @PrimaryGeneratedColumn() id: number; @Column({ length: 150 }) customerName: string; @Column({ type: 'float', nullable: true }) discount?: number; }

Solution

  1. Step 1: Verify primary key definition

    @PrimaryGeneratedColumn() on id: number creates an auto-incrementing integer primary key. A string type would be incorrect without additional configuration like UUID.
  2. Step 2: Check customerName

    @Column({ length: 150 }) with string type creates a VARCHAR(150) column. A number type mismatches.
  3. Step 3: Confirm discount

    @Column({ type: 'float', nullable: true }) with optional discount?: number allows null values. Missing nullable: true, wrong type like 'int', or non-optional fails.
  4. Final Answer:

    The entity with number id, string customerName({length:150}), optional nullable float discount -> Option D
  5. Quick Check:

    Match types, nullable, and optional properties carefully [OK]
Hint: Use nullable: true and optional property for nullable columns [OK]
Common Mistakes:
  • Mismatching property types and column types
  • Forgetting to mark nullable columns as optional
  • Using wrong primary key type
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