-
-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from Diluka/feature/deep-relations
nested relations query
- Loading branch information
Showing
16 changed files
with
348 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright under the Parsec Tech Co., Ltd. Version 1.0; | ||
* you may not use this file except in compliance with the permit. | ||
* Copyright (c) 2019 ChongQing Parsec Technology Corporation. All Rights Reserved. | ||
* Version 1.0 | ||
*/ | ||
|
||
import { Entity, Column, ManyToOne, ManyToMany, OneToMany, JoinTable } from 'typeorm'; | ||
import { BaseEntity } from '../src/base-entity'; | ||
import { Company } from './company.entity'; | ||
import { User } from './user.entity'; | ||
import { Task } from './task.entity'; | ||
|
||
@Entity('projects') | ||
export class Project extends BaseEntity { | ||
@Column({ type: 'varchar', length: 100, nullable: false, unique: true }) | ||
name: string; | ||
|
||
@Column({ type: 'text', nullable: true }) | ||
description: string; | ||
|
||
@Column({ nullable: false }) | ||
companyId: number; | ||
|
||
/** | ||
* Relations | ||
*/ | ||
|
||
@ManyToOne((type) => Company, (c) => c.projects) | ||
company: Company; | ||
|
||
@ManyToMany((type) => User, (u) => u.projects, { cascade: true }) | ||
@JoinTable() | ||
users: User[]; | ||
|
||
@OneToMany((type) => Task, (t) => t.project) | ||
tasks: Task[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,10 +66,93 @@ export class Seeds1544303473346 implements MigrationInterface { | |
('[email protected]', '9c81f2857e8f', false, 2, 19), | ||
('[email protected]', '9c81f2857e8f', false, 2, 20); | ||
`); | ||
|
||
// projects | ||
await queryRunner.query(` | ||
INSERT INTO public.projects ("name", "description", "companyId") | ||
VALUES ('project1', 'project 1 desc', 1), | ||
('project2', 'project 2 desc', 1), | ||
('project3', 'project 3 desc', 1), | ||
('project4', 'project 4 desc', 1), | ||
('project5', 'project 5 desc', 1), | ||
('project6', 'project 6 desc', 1), | ||
('project7', 'project 7 desc', 1), | ||
('project8', 'project 8 desc', 1), | ||
('project9', 'project 9 desc', 2), | ||
('project10', 'project 10 desc', 3), | ||
('project11', 'project 11 desc', 4), | ||
('project12', 'project 12 desc', 5), | ||
('project13', 'project 13 desc', 6), | ||
('project14', 'project 14 desc', 7), | ||
('project15', 'project 15 desc', 8), | ||
('project16', 'project 16 desc', 9); | ||
`); | ||
|
||
// tasks | ||
await queryRunner.query(` | ||
INSERT INTO public.tasks ("name", "status", "companyId", "projectId", "userId") | ||
VALUES ('task11', 'a', 1, 1, 1), | ||
('task12', 'a', 1, 1, 1), | ||
('task13', 'a', 1, 1, 1), | ||
('task14', 'a', 1, 1, 1), | ||
('task21', 'a', 1, 2, 2), | ||
('task22', 'a', 1, 2, 2), | ||
('task23', 'a', 1, 2, 2), | ||
('task24', 'a', 1, 2, 2), | ||
('task31', 'a', 1, 3, 3), | ||
('task32', 'a', 1, 3, 3), | ||
('task33', 'a', 1, 3, 3), | ||
('task34', 'a', 1, 3, 3), | ||
('task41', 'a', 1, 4, 4), | ||
('task42', 'a', 1, 4, 4), | ||
('task43', 'a', 1, 4, 4), | ||
('task44', 'a', 1, 4, 4), | ||
('task1', 'a', 1, 1, 5), | ||
('task2', 'a', 1, 1, 5), | ||
('task3', 'a', 1, 1, 5), | ||
('task4', 'a', 1, 1, 5), | ||
('task1', 'a', 1, 1, 6), | ||
('task2', 'a', 1, 1, 6), | ||
('task3', 'a', 1, 1, 6), | ||
('task4', 'a', 1, 1, 6), | ||
('task1', 'a', 1, 1, 7), | ||
('task2', 'a', 1, 1, 7), | ||
('task3', 'a', 1, 1, 7), | ||
('task4', 'a', 1, 1, 7); | ||
`); | ||
|
||
// user projects | ||
await queryRunner.query(` | ||
INSERT INTO public.projects_users_users ("projectsId", "usersId") | ||
VALUES (1, 1), | ||
(1, 2), | ||
(1, 3), | ||
(1, 4), | ||
(1, 5), | ||
(1, 6), | ||
(1, 7), | ||
(1, 8), | ||
(1, 9), | ||
(2, 1), | ||
(2, 2), | ||
(2, 3), | ||
(2, 4), | ||
(2, 5), | ||
(2, 6), | ||
(2, 7), | ||
(2, 8), | ||
(2, 9); | ||
`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<any> { | ||
// flush | ||
await queryRunner.query( | ||
`DELETE FROM public.tasks; ALTER SEQUENCE tasks_id_seq RESTART WITH 1;`, | ||
); | ||
await queryRunner.query( | ||
`DELETE FROM public.projects; ALTER SEQUENCE projects_id_seq RESTART WITH 1;`, | ||
); | ||
await queryRunner.query( | ||
`DELETE FROM public.users; ALTER SEQUENCE users_id_seq RESTART WITH 1;`, | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright under the Parsec Tech Co., Ltd. Version 1.0; | ||
* you may not use this file except in compliance with the permit. | ||
* Copyright (c) 2019 ChongQing Parsec Technology Corporation. All Rights Reserved. | ||
* Version 1.0 | ||
*/ | ||
|
||
import { Entity, Column, ManyToOne } from 'typeorm'; | ||
import { BaseEntity } from '../src/base-entity'; | ||
import { Company } from './company.entity'; | ||
import { Project } from './project.entity'; | ||
import { User } from './user.entity'; | ||
|
||
@Entity('tasks') | ||
export class Task extends BaseEntity { | ||
@Column({ type: 'varchar', length: 100, nullable: false }) | ||
name: string; | ||
|
||
@Column({ type: 'varchar', nullable: false }) | ||
status: string; | ||
|
||
@Column({ nullable: false }) | ||
companyId: number; | ||
|
||
@Column({ nullable: false }) | ||
projectId: number; | ||
|
||
@Column({ nullable: false }) | ||
userId: number; | ||
|
||
/** | ||
* Relations | ||
*/ | ||
|
||
@ManyToOne((type) => Company, (c) => c.tasks) | ||
company: Company; | ||
|
||
@ManyToOne((type) => Project, (c) => c.tasks) | ||
project: Project; | ||
|
||
@ManyToOne((type) => User, (u) => u.tasks) | ||
user: User; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./jest.config.json'); |
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.