Challenge - 5 Problems
Entity Definition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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; }
Attempts:
2 left
💡 Hint
Look at the argument passed to @PrimaryGeneratedColumn decorator.
✗ Incorrect
The decorator @PrimaryGeneratedColumn('uuid') tells TypeORM to generate a UUID string automatically for the 'id' field.
📝 Syntax
intermediate2: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 }
Attempts:
2 left
💡 Hint
Check the official TypeORM column options for nullable fields.
✗ Incorrect
The correct option is 'nullable: true' to allow null values in the database column.
🔧 Debug
advanced2: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; }
Attempts:
2 left
💡 Hint
TypeORM needs to know how to store Date objects in the database.
✗ Incorrect
Without specifying the column type, TypeORM may not map the Date property correctly, causing errors on save.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Think about how ORM knows which classes represent tables.
✗ Incorrect
The @Entity decorator tells TypeORM that this class maps to a database table.
❓ state_output
expert3: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;
Attempts:
2 left
💡 Hint
Check the default value set in the @Column decorator and what happens when no value is assigned before saving.
✗ Incorrect
The 'isActive' column has a default value of true, so when a new User is saved without setting 'isActive', the database assigns true.