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

nested relations query #28

Merged
merged 4 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"scripts": {
"docker:up": "docker-compose up -d",
"docker:down": "docker-compose down",
"test:e2e": "node_modules/.bin/jest --verbose --coverage --config ./jest.json",
"test:e2e": "jest --verbose --coverage",
"pretest:e2e": "npm run test:e2e:typeorm:prepare",
"posttest:e2e": "npm run test:e2e:typeorm:clean",
"test:e2e:typeorm:prepare": "cd integration/typeorm && npm run db:sync -- -f=e2e/orm && npm run db:flush -- -f=e2e/orm && npm run db:seeds -- -f=e2e/orm",
"test:e2e:typeorm:clean": "cd integration/typeorm && npm run db:flush -- -f=e2e/orm",
"coverage": "node_modules/.bin/jest --verbose --coverage --config ./jest.json --coverageReporters=text-lcov | coveralls",
"coverage": "jest --verbose --coverage --coverageReporters=text-lcov | coveralls",
"clean": "cd dist && rm -rf `ls | grep -v \"LICENSE\\|package.json\\|README.md\"`",
"clean:typeorm": "cd integration/typeorm/node_modules/@nestjsx/crud && rm -rf *",
"update:typeorm": "cp -a ./dist/. ./integration/typeorm/node_modules/@nestjsx/crud",
"reset:typeorm": "cd integration/typeorm && npm i @nestjsx/crud@next",
"build": "node_modules/.bin/tsc",
"build": "tsc -b tsconfig.build.json",
"prebuild": "npm run clean",
"postbuild": "npm run clean:typeorm && npm run update:typeorm"
},
Expand All @@ -35,9 +35,11 @@
"typeorm": "^0.2.9"
},
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/jest": "^23.3.10",
"@types/node": "^10.12.12",
"@types/supertest": "^2.0.7",
"chai": "^4.2.0",
"coveralls": "^3.0.2",
"jest": "^23.6.0",
"nodemon": "^1.18.7",
Expand Down
Loading