Skip to content

Commit

Permalink
Merge pull request #28 from Diluka/feature/deep-relations
Browse files Browse the repository at this point in the history
nested relations query
  • Loading branch information
michaelyali committed Jan 25, 2019
2 parents e255120 + 01fcd9a commit 72dd210
Show file tree
Hide file tree
Showing 16 changed files with 348 additions and 30 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ _Syntax:_
> ?join=**relation**||**field1**,**field2**,...
> ?join=**relation1**||**field11**,**field12**,...&join=**relation1**.**relation2**||**field21**,**field22**,...&join=...
_Examples:_

> ?join=**profile**
Expand All @@ -322,7 +324,9 @@ _Examples:_
> ?join=**profile**||**firstName**,**email**&join=**notifications**||**content**&join=**tasks**
**_Notice:_** `id` field always persists in relational objects.
> ?join=**relation1**&join=**relation1**.**relation2**&join=**relation1**.**relation2**.**relation3**
**_Notice:_** `id` field always persists in relational objects. To use nested relations, the parent level **MUST** be set before the child level like example above.

_Alias:_ `join[]`

Expand Down Expand Up @@ -476,7 +480,8 @@ An Object of [relations](http://typeorm.io/#/relations) that allowed to be fetch
notifications: {
exclude: ['token']
},
company: {}
company: {},
'company.projects': {}
}
}
```
Expand Down
8 changes: 8 additions & 0 deletions integration/typeorm/e2e/company.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { CrudValidate } from '../../../src/';

import { BaseEntity } from '../src/base-entity';
import { User } from './user.entity';
import { Project } from './project.entity';
import { Task } from './task.entity';

const { CREATE, UPDATE } = CrudValidate;

Expand Down Expand Up @@ -34,4 +36,10 @@ export class Company extends BaseEntity {

@OneToMany((type) => User, (u) => u.company)
users: User[];

@OneToMany((type) => Project, (p) => p.company)
projects: Project[];

@OneToMany((type) => Task, (t) => t.company)
tasks: Task[];
}
38 changes: 38 additions & 0 deletions integration/typeorm/e2e/project.entity.ts
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[];
}
83 changes: 83 additions & 0 deletions integration/typeorm/e2e/seeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,93 @@ export class Seeds1544303473346 implements MigrationInterface {
('fericapozzati7@k3zaraxg9t7e1f.ml', '9c81f2857e8f', false, 2, 19),
('os.s.l.ka@6-6-6.cf', '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;`,
);
Expand Down
43 changes: 43 additions & 0 deletions integration/typeorm/e2e/task.entity.ts
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;
}
12 changes: 10 additions & 2 deletions integration/typeorm/e2e/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, Column, JoinColumn, OneToOne, ManyToOne } from 'typeorm';
import { Entity, Column, JoinColumn, OneToOne, ManyToOne, ManyToMany, OneToMany } from 'typeorm';
import {
IsOptional,
IsString,
Expand All @@ -11,9 +11,11 @@ import {
import { Type } from '../../../node_modules/class-transformer';
import { CrudValidate } from '../../../src/';

import { BaseEntity } from '../src//base-entity';
import { BaseEntity } from '../src/base-entity';
import { UserProfile } from './user-profile.entity';
import { Company } from './company.entity';
import { Project } from './project.entity';
import { Task } from './task.entity';

const { UPDATE, CREATE } = CrudValidate;

Expand Down Expand Up @@ -60,4 +62,10 @@ export class User extends BaseEntity {

@ManyToOne((type) => Company, (c) => c.users)
company: Company;

@ManyToMany((type) => Project, (c) => c.users)
projects: Project[];

@OneToMany((type) => Task, (t) => t.user)
tasks: Task[];
}
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./jest.config.json');
File renamed without changes.
61 changes: 60 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 72dd210

Please sign in to comment.