0
0
NestJSframework~20 mins

Entity definition in NestJS - Practice Problems & Coding Challenges

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