Below are the entities I have defined. The Student
entity can subscribe to multiple Teacher
s, and vice versa - a Teacher
can have many Student
s.
import { PrimaryGeneratedColumn, Column, BeforeInsert, BeforeUpdate } from "typeorm"
/*
* Adhering to the Data Mapper pattern: https://typeorm.io/active-record-data-mapper
*/
export abstract class EntityBase {
@PrimaryGeneratedColumn()
public id: number
@Column()
public created: Date
@Column()
public modified: Date
@BeforeInsert()
updateDates () {
this.created = new Date();
this.modified = new Date();
}
@BeforeUpdate()
updateModifiedDate () {
this.modified = new Date();
}
}
import { Entity, Column, ManyToMany, JoinTable } from "typeorm"
import { EntityBase } from "./EntityBase"
import { Teacher } from "./Teacher"
@Entity()
export class Student extends EntityBase {
@Column({ length: 256 })
public firstName: string
@Column({ length: 256 })
public lastName: string
@Column({ unique: true, length: 256 })
public email: string
@Column()
public isSuspended: boolean
@ManyToMany((type) => Teacher, (teacher) => teacher.students)
@JoinTable()
public teachers: Teacher[]
constructor(first: string, last: string, email: string, isSuspended?: boolean) {
super();
this.firstName = first;
this.lastName = last;
this.email = email;
this.isSuspended = isSuspended ?? false;
this.teachers = [];
}
}
import { Entity, Column, JoinTable, ManyToMany } from "typeorm"
import { EntityBase } from "./EntityBase"
import { Student } from "./Student"
@Entity()
export class Teacher extends EntityBase {
@Column({ length: 256 })
public firstName: string
@Column({ length: 256 })
public lastName: string
@Column({ unique: true, length: 256 })
public email: string
@ManyToMany((type) => Student, (student) => student.teachers)
@JoinTable()
public students: Student[]
constructor(first: string, last: string, email: string) {
super();
this.firstName = first;
this.lastName = last;
this.email = email;
this.students = [];
}
}
Could this potentially lead to circular loading? What strategies can be implemented to enhance or avoid this scenario?