Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/repeating aliases in nested relations #282

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions integration/crud-typeorm/categories/categories.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Controller } from '@nestjs/common';
import { ApiUseTags } from '@nestjs/swagger';
import { Crud } from '@nestjsx/crud';

import { CategoriesService } from './categories.service';
import { Category } from './category.entity';

@Crud({
model: {
type: Category,
},
params: {
id: {
field: 'id',
type: 'number',
primary: true,
},
},
query: {
join: {
children: {
eager: true,
},
'children.children': {
eager: true,
},
},
},
})
@ApiUseTags('categories')
@Controller('categories')
export class CategoriesController {
constructor(public service: CategoriesService) {}
}
16 changes: 16 additions & 0 deletions integration/crud-typeorm/categories/categories.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { CategoriesController } from './categories.controller';
import { CategoriesService } from './categories.service';
import { Category } from './category.entity';
import { File } from './file.entity';

@Module({
imports: [TypeOrmModule.forFeature([Category, File])],
providers: [CategoriesService],
exports: [CategoriesService
],
controllers: [CategoriesController],
})
export class UsersModule {}
13 changes: 13 additions & 0 deletions integration/crud-typeorm/categories/categories.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { TypeOrmCrudService } from '@nestjsx/crud-typeorm';
import { Repository } from "typeorm";

import { Category } from './category.entity';

@Injectable()
export class CategoriesService extends TypeOrmCrudService<Category> {
constructor(@InjectRepository(Category) repo: Repository<Category>) {
super(repo);
}
}
24 changes: 24 additions & 0 deletions integration/crud-typeorm/categories/category.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
import { BaseEntity } from '../base-entity';
import { Project } from '../projects/project.entity';
import { File } from './file.entity';

@Entity('categories')
export class Category extends BaseEntity {
@Column()
title: string;

@ManyToOne(() => File)
image: File;

@OneToMany(() => Project, (el) => el.category)
projects: Project[];

@ManyToOne(() => Category, (category) => category.children, {
nullable: true,
})
parent: Category;

@OneToMany(() => Category, (category) => category.parent)
children: Category[];
}
8 changes: 8 additions & 0 deletions integration/crud-typeorm/categories/file.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Column, Entity, ManyToOne } from 'typeorm';
import { BaseEntity } from '../base-entity';

@Entity("files")
export class File extends BaseEntity {
@Column()
path: string;
}
5 changes: 5 additions & 0 deletions integration/crud-typeorm/categories/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./category.entity";
export * from "./file.entity";
export * from "./categories.service";
export * from "./categories.controller";
export * from "./categories.module";
14 changes: 9 additions & 5 deletions integration/crud-typeorm/projects/project.entity.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Entity, Column, ManyToOne, ManyToMany, JoinTable } from 'typeorm';
import { Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany } from 'typeorm';
import { CrudValidationGroups } from '@nestjsx/crud';
import {
IsBoolean,
IsDefined,
IsNumber,
IsOptional,
IsString,
IsNumber,
MaxLength,
IsDefined,
IsBoolean,
} from 'class-validator';
import { CrudValidationGroups } from '@nestjsx/crud';

import { BaseEntity } from '../base-entity';
import { Category } from '../categories/category.entity';
import { Company } from '../companies/company.entity';
import { User } from '../users/user.entity';

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

@ManyToOne(() => Category, (el) => el.projects)
category: Category;
}
70 changes: 49 additions & 21 deletions integration/crud-typeorm/seeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,57 @@ export class Seeds1544303473346 implements MigrationInterface {
('Name10', 'Domain10');
`);

// files
await queryRunner.query(`
INSERT INTO public.files ("path") VALUES
('/uploads/file1.txt'),
('/uploads/file2.txt'),
('/uploads/file4.txt'),
('/uploads/file5.txt'),
('/uploads/file6.txt'),
('/uploads/file7.txt'),
('/uploads/file8.txt'),
('/uploads/file9.txt'),
('/uploads/file10.txt');
`);

// categories
await queryRunner.query(`
INSERT INTO public.categories ("title", "imageId", "parentId") VALUES
('Category1', 1, NULL),
('Category2', 2, NULL),
('Category1.1', 3, 1),
('Category1.2', 4, 1),
('Category2.1', 5, 2),
('Category2.2', 6, 2),
('Category1.1.1', 7, 3),
('Category1.1.2', 8, 3),
('Category1.1.3', 9, 4);
`);

// projects
await queryRunner.query(`
INSERT INTO public.projects ("name", "description", "isActive", "companyId") VALUES
('Project1', 'description1', true, 1),
('Project2', 'description2', true, 1),
('Project3', 'description3', true, 2),
('Project4', 'description4', true, 2),
('Project5', 'description5', true, 3),
('Project6', 'description6', true, 3),
('Project7', 'description7', true, 4),
('Project8', 'description8', true, 4),
('Project9', 'description9', true, 5),
('Project10', 'description10', true, 5),
('Project11', 'description11', false, 6),
('Project12', 'description12', false, 6),
('Project13', 'description13', false, 7),
('Project14', 'description14', false, 7),
('Project15', 'description15', false, 8),
('Project16', 'description16', false, 8),
('Project17', 'description17', false, 9),
('Project18', 'description18', false, 9),
('Project19', 'description19', false, 10),
('Project20', 'description20', false, 10);
INSERT INTO public.projects ("name", "description", "isActive", "companyId", "categoryId") VALUES
('Project1', 'description1', true, 1, 1),
('Project2', 'description2', true, 1, 1),
('Project3', 'description3', true, 2, 1),
('Project4', 'description4', true, 2, 2),
('Project5', 'description5', true, 3, 2),
('Project6', 'description6', true, 3, 2),
('Project7', 'description7', true, 4, 3),
('Project8', 'description8', true, 4, 3),
('Project9', 'description9', true, 5, 3),
('Project10', 'description10', true, 5, 4),
('Project11', 'description11', false, 6, 4),
('Project12', 'description12', false, 6, NULL),
('Project13', 'description13', false, 7, NULL),
('Project14', 'description14', false, 7, NULL),
('Project15', 'description15', false, 8, NULL),
('Project16', 'description16', false, 8, NULL),
('Project17', 'description17', false, 9, NULL),
('Project18', 'description18', false, 9, NULL),
('Project19', 'description19', false, 10, NULL),
('Project20', 'description20', false, 10, NULL);
`);

// users-profiles
Expand Down