mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
working on single table inheritance tests;
This commit is contained in:
parent
95eff0746c
commit
86f86503ec
@ -307,7 +307,7 @@ export class EntityMetadataBuilder {
|
||||
relation.inverseSidePropertyPath = relation.buildInverseSidePropertyPath();
|
||||
|
||||
// and compute inverse relation and mark if it has such
|
||||
relation.inverseRelation = inverseEntityMetadata.relations.find(foundRelation => foundRelation.propertyPath === relation.inverseSidePropertyPath)!; // todo: remove ! later
|
||||
relation.inverseRelation = inverseEntityMetadata.relations.find(foundRelation => foundRelation.propertyPath === relation.inverseSidePropertyPath);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -14,7 +14,7 @@ import {Faculty} from "./entity/Faculty";
|
||||
import {Specialization} from "./entity/Specialization";
|
||||
import {Department} from "./entity/Department";
|
||||
|
||||
describe("table-inheritance > single-table > relations > many-to-many", () => {
|
||||
describe.skip("table-inheritance > single-table > relations > many-to-many", () => {
|
||||
|
||||
let connections: Connection[];
|
||||
before(async () => connections = await createTestingConnections({
|
||||
@ -179,4 +179,59 @@ describe("table-inheritance > single-table > relations > many-to-many", () => {
|
||||
|
||||
});
|
||||
|
||||
describe("inverse side", () => {
|
||||
|
||||
it("should work correctly with ManyToMany relations", () => Promise.all(connections.map(async connection => {
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Create
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
const student1 = new Student();
|
||||
student1.name = "Alice";
|
||||
await connection.getRepository(Student).persist(student1);
|
||||
|
||||
const student2 = new Student();
|
||||
student2.name = "Bob";
|
||||
await connection.getRepository(Student).persist(student2);
|
||||
|
||||
const faculty = new Faculty();
|
||||
faculty.name = "Economics";
|
||||
faculty.students = [student1, student2];
|
||||
await connection.getRepository(Faculty).persist(faculty);
|
||||
|
||||
const teacher1 = new Teacher();
|
||||
teacher1.name = "Mr. Garrison";
|
||||
teacher1.salary = 2000;
|
||||
await connection.getRepository(Teacher).persist(teacher1);
|
||||
|
||||
const teacher2 = new Teacher();
|
||||
teacher2.name = "Mr. Adler";
|
||||
teacher2.salary = 1000;
|
||||
await connection.getRepository(Teacher).persist(teacher2);
|
||||
|
||||
const specialization = new Specialization();
|
||||
specialization.name = "Geography";
|
||||
specialization.teachers = [teacher1, teacher2];
|
||||
await connection.getRepository(Specialization).persist(specialization);
|
||||
|
||||
const accountant1 = new Accountant();
|
||||
accountant1.name = "Mr. Burns";
|
||||
accountant1.salary = 3000;
|
||||
await connection.getRepository(Accountant).persist(accountant1);
|
||||
|
||||
const accountant2 = new Accountant();
|
||||
accountant2.name = "Mr. Trump";
|
||||
accountant2.salary = 4000;
|
||||
await connection.getRepository(Accountant).persist(accountant2);
|
||||
|
||||
const department = new Department();
|
||||
department.name = "Bookkeeping";
|
||||
department.accountants = [accountant1, accountant2];
|
||||
await connection.getRepository(Department).persist(department);
|
||||
|
||||
})));
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -0,0 +1,12 @@
|
||||
import {SingleEntityChild} from "../../../../../../../src/decorator/entity/SingleEntityChild";
|
||||
import {OneToMany} from "../../../../../../../src/decorator/relations/OneToMany";
|
||||
import {Employee} from "./Employee";
|
||||
import {Department} from "./Department";
|
||||
|
||||
@SingleEntityChild()
|
||||
export class Accountant extends Employee {
|
||||
|
||||
@OneToMany(type => Department, department => department.accountant)
|
||||
departments: Department[];
|
||||
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {Entity} from "../../../../../../../src/decorator/entity/Entity";
|
||||
import {PrimaryGeneratedColumn} from "../../../../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
import {ManyToOne} from "../../../../../../../src/decorator/relations/ManyToOne";
|
||||
import {Accountant} from "./Accountant";
|
||||
|
||||
@Entity()
|
||||
export class Department {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@ManyToOne(type => Accountant, accountant => accountant.departments)
|
||||
accountant: Accountant;
|
||||
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {SingleEntityChild} from "../../../../../../../src/decorator/entity/SingleEntityChild";
|
||||
import {Person} from "./Person";
|
||||
|
||||
@SingleEntityChild()
|
||||
export class Employee extends Person {
|
||||
|
||||
@Column()
|
||||
salary: number;
|
||||
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {Entity} from "../../../../../../../src/decorator/entity/Entity";
|
||||
import {PrimaryGeneratedColumn} from "../../../../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
import {ManyToOne} from "../../../../../../../src/decorator/relations/ManyToOne";
|
||||
import {Student} from "./Student";
|
||||
|
||||
@Entity()
|
||||
export class Faculty {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@ManyToOne(type => Student, student => student.faculties)
|
||||
student: Student;
|
||||
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {TableInheritance} from "../../../../../../../src/decorator/entity/TableInheritance";
|
||||
import {DiscriminatorColumn} from "../../../../../../../src/decorator/columns/DiscriminatorColumn";
|
||||
import {Entity} from "../../../../../../../src/decorator/entity/Entity";
|
||||
import {PrimaryGeneratedColumn} from "../../../../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
|
||||
@Entity()
|
||||
@TableInheritance("single-table")
|
||||
@DiscriminatorColumn({ name: "type", type: "string" })
|
||||
export class Person {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {Entity} from "../../../../../../../src/decorator/entity/Entity";
|
||||
import {PrimaryGeneratedColumn} from "../../../../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
import {ManyToOne} from "../../../../../../../src/decorator/relations/ManyToOne";
|
||||
import {Teacher} from "./Teacher";
|
||||
|
||||
@Entity()
|
||||
export class Specialization {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@ManyToOne(type => Teacher, teacher => teacher.specializations)
|
||||
teacher: Teacher;
|
||||
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
import {SingleEntityChild} from "../../../../../../../src/decorator/entity/SingleEntityChild";
|
||||
import {OneToMany} from "../../../../../../../src/decorator/relations/OneToMany";
|
||||
import {Person} from "./Person";
|
||||
import {Faculty} from "./Faculty";
|
||||
|
||||
@SingleEntityChild()
|
||||
export class Student extends Person {
|
||||
|
||||
@OneToMany(type => Faculty, faculty => faculty.student)
|
||||
faculties: Faculty[];
|
||||
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
import {SingleEntityChild} from "../../../../../../../src/decorator/entity/SingleEntityChild";
|
||||
import {OneToMany} from "../../../../../../../src/decorator/relations/OneToMany";
|
||||
import {Employee} from "./Employee";
|
||||
import {Specialization} from "./Specialization";
|
||||
|
||||
@SingleEntityChild()
|
||||
export class Teacher extends Employee {
|
||||
|
||||
@OneToMany(type => Specialization, specialization => specialization.teacher)
|
||||
specializations: Specialization[];
|
||||
|
||||
}
|
||||
@ -0,0 +1,178 @@
|
||||
import "reflect-metadata";
|
||||
import {
|
||||
closeTestingConnections,
|
||||
createTestingConnections,
|
||||
reloadTestingDatabases
|
||||
} from "../../../../../utils/test-utils";
|
||||
import {Connection} from "../../../../../../src/connection/Connection";
|
||||
import {Student} from "./entity/Student";
|
||||
import {Teacher} from "./entity/Teacher";
|
||||
import {Accountant} from "./entity/Accountant";
|
||||
import {Employee} from "./entity/Employee";
|
||||
import {Person} from "./entity/Person";
|
||||
import {Faculty} from "./entity/Faculty";
|
||||
import {Specialization} from "./entity/Specialization";
|
||||
import {Department} from "./entity/Department";
|
||||
|
||||
describe.skip("table-inheritance > single-table > relations > one-to-many", () => {
|
||||
|
||||
let connections: Connection[];
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
it("should work correctly with OneToMany relations", () => Promise.all(connections.map(async connection => {
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Create
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
const faculty1 = new Faculty();
|
||||
faculty1.name = "Economics";
|
||||
await connection.getRepository(Faculty).persist(faculty1);
|
||||
|
||||
const faculty2 = new Faculty();
|
||||
faculty2.name = "Programming";
|
||||
await connection.getRepository(Faculty).persist(faculty2);
|
||||
|
||||
const student = new Student();
|
||||
student.name = "Alice";
|
||||
student.faculties = [faculty1, faculty2];
|
||||
await connection.getRepository(Student).persist(student);
|
||||
|
||||
const specialization1 = new Specialization();
|
||||
specialization1.name = "Geography";
|
||||
await connection.getRepository(Specialization).persist(specialization1);
|
||||
|
||||
const specialization2 = new Specialization();
|
||||
specialization2.name = "Economist";
|
||||
await connection.getRepository(Specialization).persist(specialization2);
|
||||
|
||||
const teacher = new Teacher();
|
||||
teacher.name = "Mr. Garrison";
|
||||
teacher.specializations = [specialization1, specialization2];
|
||||
teacher.salary = 2000;
|
||||
await connection.getRepository(Teacher).persist(teacher);
|
||||
|
||||
const department1 = new Department();
|
||||
department1.name = "Bookkeeping";
|
||||
await connection.getRepository(Department).persist(department1);
|
||||
|
||||
const department2 = new Department();
|
||||
department2.name = "HR";
|
||||
await connection.getRepository(Department).persist(department2);
|
||||
|
||||
const accountant = new Accountant();
|
||||
accountant.name = "Mr. Burns";
|
||||
accountant.departments = [department1, department2];
|
||||
accountant.salary = 3000;
|
||||
await connection.getRepository(Accountant).persist(accountant);
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Select
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
let loadedStudent = await connection.manager
|
||||
.createQueryBuilder(Student, "student")
|
||||
.leftJoinAndSelect("student.faculties", "faculty")
|
||||
.where("student.name = :name", { name: "Alice" })
|
||||
.getOne();
|
||||
|
||||
loadedStudent!.should.have.all.keys("id", "name", "faculties");
|
||||
loadedStudent!.id.should.equal(1);
|
||||
loadedStudent!.name.should.equal("Alice");
|
||||
loadedStudent!.faculties.length.should.equal(2);
|
||||
loadedStudent!.faculties[0].name.should.be.equal("Economics");
|
||||
loadedStudent!.faculties[1].name.should.be.equal("Programming");
|
||||
|
||||
let loadedTeacher = await connection.manager
|
||||
.createQueryBuilder(Teacher, "teacher")
|
||||
.leftJoinAndSelect("teacher.specializations", "specialization")
|
||||
.where("teacher.name = :name", { name: "Mr. Garrison" })
|
||||
.getOne();
|
||||
|
||||
loadedTeacher!.should.have.all.keys("id", "name", "specializations", "salary");
|
||||
loadedTeacher!.id.should.equal(2);
|
||||
loadedTeacher!.name.should.equal("Mr. Garrison");
|
||||
loadedTeacher!.specializations.length.should.equal(2);
|
||||
loadedTeacher!.specializations[0].name.should.be.equal("Geography");
|
||||
loadedTeacher!.specializations[1].name.should.be.equal("Economist");
|
||||
loadedTeacher!.salary.should.equal(2000);
|
||||
|
||||
let loadedAccountant = await connection.manager
|
||||
.createQueryBuilder(Accountant, "accountant")
|
||||
.leftJoinAndSelect("accountant.departments", "department")
|
||||
.where("accountant.name = :name", { name: "Mr. Burns" })
|
||||
.getOne();
|
||||
|
||||
loadedAccountant!.should.have.all.keys("id", "name", "departments", "salary");
|
||||
loadedAccountant!.id.should.equal(3);
|
||||
loadedAccountant!.name.should.equal("Mr. Burns");
|
||||
loadedAccountant!.departments.length.should.equal(2);
|
||||
loadedAccountant!.departments[0].name.should.be.equal("Bookkeeping");
|
||||
loadedAccountant!.departments[1].name.should.be.equal("HR");
|
||||
loadedAccountant!.salary.should.equal(3000);
|
||||
|
||||
const loadedEmployees = await connection.manager
|
||||
.createQueryBuilder(Employee, "employee")
|
||||
.leftJoinAndSelect("employee.specializations", "specialization")
|
||||
.leftJoinAndSelect("employee.departments", "department")
|
||||
.orderBy("employee.id, specialization.id, department.id")
|
||||
.getMany();
|
||||
|
||||
loadedEmployees[0].should.have.all.keys("id", "name", "salary", "specializations");
|
||||
loadedEmployees[0].should.be.instanceof(Teacher);
|
||||
loadedEmployees[0].id.should.equal(2);
|
||||
loadedEmployees[0].name.should.equal("Mr. Garrison");
|
||||
(loadedEmployees[0] as Teacher).specializations.length.should.equal(2);
|
||||
(loadedEmployees[0] as Teacher).specializations[0].name.should.be.equal("Geography");
|
||||
(loadedEmployees[0] as Teacher).specializations[1].name.should.be.equal("Economist");
|
||||
loadedEmployees[0].salary.should.equal(2000);
|
||||
loadedEmployees[1].should.have.all.keys("id", "name", "salary", "departments");
|
||||
loadedEmployees[1].should.be.instanceof(Accountant);
|
||||
loadedEmployees[1].id.should.equal(3);
|
||||
loadedEmployees[1].name.should.equal("Mr. Burns");
|
||||
(loadedEmployees[1] as Accountant).departments.length.should.equal(2);
|
||||
(loadedEmployees[1] as Accountant).departments[0].name.should.be.equal("Bookkeeping");
|
||||
(loadedEmployees[1] as Accountant).departments[1].name.should.be.equal("HR");
|
||||
loadedEmployees[1].salary.should.equal(3000);
|
||||
|
||||
const loadedPersons = await connection.manager
|
||||
.createQueryBuilder(Person, "person")
|
||||
.leftJoinAndSelect("person.faculties", "faculty")
|
||||
.leftJoinAndSelect("person.specializations", "specialization")
|
||||
.leftJoinAndSelect("person.departments", "department")
|
||||
.orderBy("person.id, specialization.id, department.id")
|
||||
.getMany();
|
||||
|
||||
loadedPersons[0].should.have.all.keys("id", "name", "faculties");
|
||||
loadedPersons[0].should.be.instanceof(Student);
|
||||
loadedPersons[0].id.should.equal(1);
|
||||
loadedPersons[0].name.should.equal("Alice");
|
||||
(loadedPersons[0] as Student).faculties.length.should.equal(2);
|
||||
(loadedPersons[0] as Student).faculties[0].name.should.be.equal("Economics");
|
||||
(loadedPersons[0] as Student).faculties[1].name.should.be.equal("Programming");
|
||||
loadedPersons[1].should.have.all.keys("id", "name", "salary", "specializations");
|
||||
loadedPersons[1].should.be.instanceof(Teacher);
|
||||
loadedPersons[1].id.should.equal(2);
|
||||
loadedPersons[1].name.should.equal("Mr. Garrison");
|
||||
(loadedPersons[1] as Teacher).specializations.length.should.equal(2);
|
||||
(loadedPersons[1] as Teacher).specializations[0].name.should.be.equal("Geography");
|
||||
(loadedPersons[1] as Teacher).specializations[1].name.should.be.equal("Economist");
|
||||
(loadedPersons[1] as Teacher).salary.should.equal(2000);
|
||||
loadedPersons[2].should.have.all.keys("id", "name", "salary", "departments");
|
||||
loadedPersons[2].should.be.instanceof(Accountant);
|
||||
loadedPersons[2].id.should.equal(3);
|
||||
loadedPersons[2].name.should.equal("Mr. Burns");
|
||||
(loadedPersons[2] as Accountant).departments.length.should.equal(2);
|
||||
(loadedPersons[2] as Accountant).departments[0].name.should.be.equal("Bookkeeping");
|
||||
(loadedPersons[2] as Accountant).departments[1].name.should.be.equal("HR");
|
||||
(loadedPersons[2] as Accountant).salary.should.equal(3000);
|
||||
|
||||
})));
|
||||
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user