Skip to content

Commit

Permalink
chore(): refactor reproduce of #87, but it passed on 4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Diluka committed Jun 14, 2019
1 parent 35acb44 commit cf6cb52
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 31 deletions.
8 changes: 6 additions & 2 deletions integration/crud-typeorm/companies/company.entity.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Entity, Column, OneToMany } from 'typeorm';
import { IsOptional, IsString, MaxLength, IsNotEmpty } from 'class-validator';
import { Column, Entity, OneToMany } from 'typeorm';
import { IsNotEmpty, IsOptional, IsString, MaxLength } from 'class-validator';
import { Type } from 'class-transformer';
import { CrudValidationGroups } from '@nestjsx/crud';

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

const { CREATE, UPDATE } = CrudValidationGroups;

Expand Down Expand Up @@ -40,4 +41,7 @@ export class Company extends BaseEntity {

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

@OneToMany(() => Task, (t) => t.company)
tasks: Task[];
}
15 changes: 6 additions & 9 deletions integration/crud-typeorm/projects/project.entity.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { Entity, Column, ManyToOne, ManyToMany, JoinTable } from 'typeorm';
import {
IsOptional,
IsString,
IsNumber,
MaxLength,
IsDefined,
IsBoolean,
} from 'class-validator';
import { Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany } from 'typeorm';
import { IsBoolean, IsDefined, IsNumber, IsOptional, IsString, MaxLength } from 'class-validator';
import { CrudValidationGroups } from '@nestjsx/crud';

import { BaseEntity } from '../base-entity';
import { Company } from '../companies/company.entity';
import { User } from '../users/user.entity';
import { Task } from '../tasks/task.entity';

const { CREATE, UPDATE } = CrudValidationGroups;

Expand Down Expand Up @@ -48,4 +42,7 @@ export class Project extends BaseEntity {
@ManyToMany((type) => User, (u) => u.projects, { cascade: true })
@JoinTable()
users?: User[];

@OneToMany((type) => Task, (t) => t.project)
tasks?: Task[];
}
33 changes: 33 additions & 0 deletions integration/crud-typeorm/seeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,39 @@ export class Seeds1544303473346 implements MigrationInterface {
('19@email.com', false, 2, 19),
('20@email.com', false, 2, 20);
`);

// 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);
`);
}

public async down(queryRunner: QueryRunner): Promise<any> {}
Expand Down
37 changes: 37 additions & 0 deletions integration/crud-typeorm/tasks/task.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Column, Entity, ManyToOne } from 'typeorm';

import { BaseEntity } from '../base-entity';
import { User } from '../users/user.entity';
import { Company } from '../companies/company.entity';
import { Project } from '../projects/project.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(() => Company, o => o.tasks)
company: Company;

@ManyToOne(() => Project, o => o.tasks)
project: Project;

@ManyToOne(() => User, o => o.tasks)
user: User;
}
16 changes: 6 additions & 10 deletions integration/crud-typeorm/users/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { Entity, Column, JoinColumn, OneToOne, ManyToOne, ManyToMany } from 'typeorm';
import {
IsOptional,
IsString,
MaxLength,
IsNotEmpty,
IsEmail,
IsBoolean,
ValidateNested,
} from 'class-validator';
import { Column, Entity, JoinColumn, ManyToMany, ManyToOne, OneToMany, OneToOne } from 'typeorm';
import { IsBoolean, IsEmail, IsNotEmpty, IsOptional, IsString, MaxLength, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';
import { CrudValidationGroups } from '@nestjsx/crud';

import { BaseEntity } from '../base-entity';
import { UserProfile } from '../users-profiles/user-profile.entity';
import { Company } from '../companies/company.entity';
import { Project } from '../projects/project.entity';
import { Task } from '../tasks/task.entity';

const { CREATE, UPDATE } = CrudValidationGroups;

Expand Down Expand Up @@ -57,4 +50,7 @@ export class User extends BaseEntity {

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

@OneToMany((type) => Task, (t) => t.user)
tasks?: Task[];
}
11 changes: 6 additions & 5 deletions packages/crud-typeorm/test/0.basic-crud.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import { Crud } from '../../crud/src/decorators/crud.decorator';
import { HttpExceptionFilter } from '../../../integration/shared/https-exception.filter';
import { withCache } from '../../../integration/crud-typeorm/orm.config';
import { Company } from '../../../integration/crud-typeorm/companies';
import { Project } from '../../../integration/crud-typeorm/projects';
import { User } from '../../../integration/crud-typeorm/users';
import { UserProfile } from '../../../integration/crud-typeorm/users-profiles';
import { CompaniesService } from './__fixture__/companies.service';
import { UsersService } from './__fixture__/users.service';
import { Entities } from './__fixture__/constants';

describe('#crud-typeorm', () => {
describe('#basic crud', () => {
Expand All @@ -27,7 +26,8 @@ describe('#crud-typeorm', () => {
})
@Controller('companies')
class CompaniesController {
constructor(public service: CompaniesService) {}
constructor(public service: CompaniesService) {
}
}

@Crud({
Expand Down Expand Up @@ -58,14 +58,15 @@ describe('#crud-typeorm', () => {
})
@Controller('companies/:companyId/users')
class UsersController {
constructor(public service: UsersService) {}
constructor(public service: UsersService) {
}
}

beforeAll(async () => {
const fixture = await Test.createTestingModule({
imports: [
TypeOrmModule.forRoot(withCache),
TypeOrmModule.forFeature([Company, Project, User, UserProfile]),
TypeOrmModule.forFeature(Entities),
],
controllers: [CompaniesController, UsersController],
providers: [
Expand Down
13 changes: 8 additions & 5 deletions packages/crud-typeorm/test/1.query-params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { withCache } from '../../../integration/crud-typeorm/orm.config';
import { Company } from '../../../integration/crud-typeorm/companies';
import { Project } from '../../../integration/crud-typeorm/projects';
import { User } from '../../../integration/crud-typeorm/users';
import { UserProfile } from '../../../integration/crud-typeorm/users-profiles';
import { CompaniesService } from './__fixture__/companies.service';
import { UsersService } from './__fixture__/users.service';
import { ProjectsService } from './__fixture__/projects.service';
import { Entities } from './__fixture__/constants';

describe('#crud-typeorm', () => {
describe('#query params', () => {
Expand All @@ -38,7 +38,8 @@ describe('#crud-typeorm', () => {
})
@Controller('companies')
class CompaniesController {
constructor(public service: CompaniesService) {}
constructor(public service: CompaniesService) {
}
}

@Crud({
Expand All @@ -57,7 +58,8 @@ describe('#crud-typeorm', () => {
})
@Controller('projects')
class ProjectsController {
constructor(public service: ProjectsService) {}
constructor(public service: ProjectsService) {
}
}

@Crud({
Expand All @@ -71,14 +73,15 @@ describe('#crud-typeorm', () => {
})
@Controller('users')
class UsersController {
constructor(public service: UsersService) {}
constructor(public service: UsersService) {
}
}

beforeAll(async () => {
const fixture = await Test.createTestingModule({
imports: [
TypeOrmModule.forRoot(withCache),
TypeOrmModule.forFeature([Company, Project, User, UserProfile]),
TypeOrmModule.forFeature(Entities),
],
controllers: [CompaniesController, ProjectsController, UsersController],
providers: [
Expand Down
35 changes: 35 additions & 0 deletions packages/crud-typeorm/test/__fixture__/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Created by Diluka on 2019-06-13.
*
*
* ----------- 神 兽 佑 我 -----------
* ┏┓ ┏┓+ +
* ┏┛┻━━━━━━┛┻┓ + +
* ┃ ┃
* ┣ ━ ┃ ++ + + +
* ████━████ ┃+
* ┃ ┃ +
* ┃ ┴ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛ Code is far away from bug
* ┃ ┃ with the animal protecting
* ┃ ┃ + + + +
* ┃ ┃
* ┃ ┃ +
* ┃ ┃ + +
* ┃ ┃ +
* ┃ ┗━━━┓ + +
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━━━━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
* ----------- 永 无 BUG ------------
*/
import { Company } from '../../../../integration/crud-typeorm/companies';
import { Project } from '../../../../integration/crud-typeorm/projects';
import { User } from '../../../../integration/crud-typeorm/users';
import { UserProfile } from '../../../../integration/crud-typeorm/users-profiles';
import { Task } from '../../../../integration/crud-typeorm/tasks/task.entity';

export const Entities = [Company, Project, User, UserProfile, Task];
12 changes: 12 additions & 0 deletions packages/crud-typeorm/test/__fixture__/tasks.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';

import { TypeOrmCrudService } from '../../../crud-typeorm/src/typeorm-crud.service';
import { Task } from '../../../../integration/crud-typeorm/tasks/task.entity';

@Injectable()
export class TasksService extends TypeOrmCrudService<Task> {
constructor(@InjectRepository(Task) repo) {
super(repo);
}
}
Loading

0 comments on commit cf6cb52

Please sign in to comment.