0
0
NestJSframework~20 mins

Relations (OneToMany, ManyToOne, ManyToMany) in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Relations 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 NestJS entity relation setup?

Given these two entities in NestJS using TypeORM, what will be the result of fetching a Post with its comments?

NestJS
import { Entity, PrimaryGeneratedColumn, Column, OneToMany, ManyToOne } from 'typeorm';

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

  @Column()
  title: string;

  @OneToMany(() => Comment, comment => comment.post)
  comments: Comment[];
}

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

  @Column()
  text: string;

  @ManyToOne(() => Post, post => post.comments)
  post: Post;
}

// Assume a Post with id=1 has 3 comments linked.
// What will be the value of post.comments after fetching Post with id=1 including relations?
AUndefined, because OneToMany does not load relations automatically
BAn empty array because relations are not set up correctly
CA single Comment object instead of an array
DAn array of 3 Comment objects linked to the Post
Attempts:
2 left
💡 Hint

Remember that OneToMany defines a collection of related entities.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a ManyToMany relation in NestJS with TypeORM?

Choose the correct syntax to define a ManyToMany relation between User and Role entities with a join table.

A
@ManyToMany(Role)
@JoinTable
roles: Role[];
B
@ManyToMany(() => Role, role => role.users)
@JoinTable()
roles: Role[];
C
@ManyToMany(() => Role, role => role.users)
roles: Role[];
D
@ManyToMany(() => Role)
@JoinTable()
roles: Role[];
Attempts:
2 left
💡 Hint

Remember to use a function returning the class and to add @JoinTable() on one side.

🔧 Debug
advanced
2:00remaining
Why does this ManyToOne relation cause a runtime error?

Consider this entity code snippet:

import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm';
import { Category } from './category.entity';

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

  @Column()
  name: string;

  @ManyToOne(Category)
  category: Category;
}

When running the app, it throws an error about missing function in relation definition. What is the cause?

AThe @Column decorator is missing on the category property
BThe Category entity is missing a primary key
CThe relation decorator should use a function returning the class: @ManyToOne(() => Category)
DThe Product entity must import Category with a relative path
Attempts:
2 left
💡 Hint

Check how the relation decorator references the related class.

state_output
advanced
2:00remaining
What is the state of the database after saving these entities with ManyToMany relation?

Given these entities and code:

import { Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable } from 'typeorm';

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

  @Column()
  name: string;

  @ManyToMany(() => Course, course => course.students)
  @JoinTable()
  courses: Course[];
}

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

  @Column()
  title: string;

  @ManyToMany(() => Student, student => student.courses)
  students: Student[];
}

// Code snippet:
const student = new Student();
student.name = 'Alice';

const course1 = new Course();
course1.title = 'Math';

const course2 = new Course();
course2.title = 'Science';

student.courses = [course1, course2];

await studentRepository.save(student);

What will be saved in the database?

AOnly the student 'Alice' is saved; courses are not saved automatically
BStudent 'Alice' saved with two new courses 'Math' and 'Science' and join table entries linking them
CCourses are saved but the join table linking student and courses is empty
DAn error occurs because courses are not saved before assigning
Attempts:
2 left
💡 Hint

TypeORM does not cascade saves by default for relations.

🧠 Conceptual
expert
3:00remaining
Which statement best describes the difference between OneToMany and ManyToOne in NestJS TypeORM?

Choose the most accurate explanation of how OneToMany and ManyToOne decorators relate entities in NestJS with TypeORM.

A<code>ManyToOne</code> defines the owning side and stores the foreign key column, while <code>OneToMany</code> is inverse and does not store the key.
B<code>OneToMany</code> and <code>ManyToOne</code> are interchangeable and both store foreign keys.
CBoth <code>OneToMany</code> and <code>ManyToOne</code> store foreign keys independently in their tables.
D<code>OneToMany</code> defines the owning side of the relation and stores the foreign key, while <code>ManyToOne</code> is inverse and does not store the key.
Attempts:
2 left
💡 Hint

Think about which side owns the foreign key column in the database.