Given these two entities in NestJS using TypeORM, what will be the result of fetching a Post with its comments?
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?
Remember that OneToMany defines a collection of related entities.
The OneToMany decorator creates a property that holds an array of related entities. When fetching a Post with its comments relation loaded, post.comments will be an array containing all linked Comment objects.
Choose the correct syntax to define a ManyToMany relation between User and Role entities with a join table.
Remember to use a function returning the class and to add @JoinTable() on one side.
Option B correctly uses a function returning the Role class, defines the inverse side, and includes @JoinTable() to create the join table. Option B misses the inverse side, B has wrong syntax, and C misses @JoinTable().
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?
Check how the relation decorator references the related class.
TypeORM requires the relation decorator to receive a function returning the class, not the class directly. Using @ManyToOne(Category) causes a runtime error because it expects a function like @ManyToOne(() => Category).
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?
TypeORM does not cascade saves by default for relations.
Since no cascade: true is specified on the ManyToMany relation, calling save(student) only persists the Student entity. The new Course instances are not saved, and thus no join table entries are created.
Choose the most accurate explanation of how OneToMany and ManyToOne decorators relate entities in NestJS with TypeORM.
Think about which side owns the foreign key column in the database.
In TypeORM, the ManyToOne side is the owning side and stores the foreign key column in its table. The OneToMany side is inverse and does not store the foreign key. This is important for how relations are saved and queried.