|
1 |
| -import { MigrationInterface, QueryRunner } from 'typeorm'; |
| 1 | +import { plainToClass } from 'class-transformer'; |
| 2 | +import { MigrationInterface, Repository, QueryRunner } from 'typeorm'; |
| 3 | +import { Company } from './companies'; |
| 4 | +import { Project, UserProject } from './projects'; |
| 5 | +import { Name, User } from './users'; |
| 6 | +import { License, UserLicense } from './users-licenses'; |
| 7 | +import { UserProfile } from './users-profiles'; |
2 | 8 |
|
3 | 9 | export class Seeds1544303473346 implements MigrationInterface {
|
| 10 | + private save<T>(repo: Repository<T>, type: any, data: Partial<T>[]): Promise<T[]> { |
| 11 | + return repo.save( |
| 12 | + data.map((partial: Partial<T>) => |
| 13 | + plainToClass<any, any>(type, partial, { ignoreDecorators: true }), |
| 14 | + ), |
| 15 | + ); |
| 16 | + } |
| 17 | + |
4 | 18 | public async up(queryRunner: QueryRunner): Promise<any> {
|
| 19 | + const { connection } = queryRunner; |
| 20 | + |
| 21 | + const companiesRepo = connection.getRepository(Company); |
| 22 | + const projectsRepo = connection.getRepository(Project); |
| 23 | + const usersProfilesRepo = connection.getRepository(UserProfile); |
| 24 | + const usersRepo = connection.getRepository(User); |
| 25 | + const licensesRepo = connection.getRepository(License); |
| 26 | + const usersLincesesRepo = connection.getRepository(UserLicense); |
| 27 | + const usersProjectsRepo = connection.getRepository(UserProject); |
| 28 | + |
5 | 29 | // companies
|
6 |
| - await queryRunner.query(` |
7 |
| - INSERT INTO public.companies ("name", "domain") VALUES |
8 |
| - ('Name1', 'Domain1'), |
9 |
| - ('Name2', 'Domain2'), |
10 |
| - ('Name3', 'Domain3'), |
11 |
| - ('Name4', 'Domain4'), |
12 |
| - ('Name5', 'Domain5'), |
13 |
| - ('Name6', 'Domain6'), |
14 |
| - ('Name7', 'Domain7'), |
15 |
| - ('Name8', 'Domain8'), |
16 |
| - ('Name9', 'Domain9'), |
17 |
| - ('Name10', 'Domain10'); |
18 |
| - `); |
| 30 | + await this.save(companiesRepo, Company, [ |
| 31 | + { name: 'Name1', domain: 'Domain1' }, |
| 32 | + { name: 'Name2', domain: 'Domain2' }, |
| 33 | + { name: 'Name3', domain: 'Domain3' }, |
| 34 | + { name: 'Name4', domain: 'Domain4' }, |
| 35 | + { name: 'Name5', domain: 'Domain5' }, |
| 36 | + { name: 'Name6', domain: 'Domain6' }, |
| 37 | + { name: 'Name7', domain: 'Domain7' }, |
| 38 | + { name: 'Name8', domain: 'Domain8' }, |
| 39 | + { name: 'Name9', domain: 'Domain9' }, |
| 40 | + { name: 'Name10', domain: 'Domain10' }, |
| 41 | + ]); |
19 | 42 |
|
20 | 43 | // projects
|
21 |
| - await queryRunner.query(` |
22 |
| - INSERT INTO public.projects ("name", "description", "isActive", "companyId") VALUES |
23 |
| - ('Project1', 'description1', true, 1), |
24 |
| - ('Project2', 'description2', true, 1), |
25 |
| - ('Project3', 'description3', true, 2), |
26 |
| - ('Project4', 'description4', true, 2), |
27 |
| - ('Project5', 'description5', true, 3), |
28 |
| - ('Project6', 'description6', true, 3), |
29 |
| - ('Project7', 'description7', true, 4), |
30 |
| - ('Project8', 'description8', true, 4), |
31 |
| - ('Project9', 'description9', true, 5), |
32 |
| - ('Project10', 'description10', true, 5), |
33 |
| - ('Project11', 'description11', false, 6), |
34 |
| - ('Project12', 'description12', false, 6), |
35 |
| - ('Project13', 'description13', false, 7), |
36 |
| - ('Project14', 'description14', false, 7), |
37 |
| - ('Project15', 'description15', false, 8), |
38 |
| - ('Project16', 'description16', false, 8), |
39 |
| - ('Project17', 'description17', false, 9), |
40 |
| - ('Project18', 'description18', false, 9), |
41 |
| - ('Project19', 'description19', false, 10), |
42 |
| - ('Project20', 'description20', false, 10); |
43 |
| - `); |
| 44 | + await this.save(projectsRepo, Project, [ |
| 45 | + { name: 'Project1', description: 'description1', isActive: true, companyId: 1 }, |
| 46 | + { name: 'Project2', description: 'description2', isActive: true, companyId: 1 }, |
| 47 | + { name: 'Project3', description: 'description3', isActive: true, companyId: 2 }, |
| 48 | + { name: 'Project4', description: 'description4', isActive: true, companyId: 2 }, |
| 49 | + { name: 'Project5', description: 'description5', isActive: true, companyId: 3 }, |
| 50 | + { name: 'Project6', description: 'description6', isActive: true, companyId: 3 }, |
| 51 | + { name: 'Project7', description: 'description7', isActive: true, companyId: 4 }, |
| 52 | + { name: 'Project8', description: 'description8', isActive: true, companyId: 4 }, |
| 53 | + { name: 'Project9', description: 'description9', isActive: true, companyId: 5 }, |
| 54 | + { name: 'Project10', description: 'description10', isActive: true, companyId: 5 }, |
| 55 | + { name: 'Project11', description: 'description11', isActive: false, companyId: 6 }, |
| 56 | + { name: 'Project12', description: 'description12', isActive: false, companyId: 6 }, |
| 57 | + { name: 'Project13', description: 'description13', isActive: false, companyId: 7 }, |
| 58 | + { name: 'Project14', description: 'description14', isActive: false, companyId: 7 }, |
| 59 | + { name: 'Project15', description: 'description15', isActive: false, companyId: 8 }, |
| 60 | + { name: 'Project16', description: 'description16', isActive: false, companyId: 8 }, |
| 61 | + { name: 'Project17', description: 'description17', isActive: false, companyId: 9 }, |
| 62 | + { name: 'Project18', description: 'description18', isActive: false, companyId: 9 }, |
| 63 | + { name: 'Project19', description: 'description19', isActive: false, companyId: 10 }, |
| 64 | + { name: 'Project20', description: 'description20', isActive: false, companyId: 10 }, |
| 65 | + ]); |
44 | 66 |
|
45 | 67 | // user-profiles
|
46 |
| - await queryRunner.query(` |
47 |
| - INSERT INTO public.user_profiles ("name") VALUES |
48 |
| - ('User1'), |
49 |
| - ('User2'), |
50 |
| - ('User3'), |
51 |
| - ('User4'), |
52 |
| - ('User5'), |
53 |
| - ('User6'), |
54 |
| - ('User7'), |
55 |
| - ('User8'), |
56 |
| - ('User9'), |
57 |
| - ('User1'), |
58 |
| - ('User1'), |
59 |
| - ('User1'), |
60 |
| - ('User1'), |
61 |
| - ('User1'), |
62 |
| - ('User1'), |
63 |
| - ('User1'), |
64 |
| - ('User1'), |
65 |
| - ('User1'), |
66 |
| - ('User1'), |
67 |
| - ('User2'); |
68 |
| - `); |
| 68 | + await this.save(usersProfilesRepo, UserProfile, [ |
| 69 | + { name: 'User1' }, |
| 70 | + { name: 'User2' }, |
| 71 | + { name: 'User3' }, |
| 72 | + { name: 'User4' }, |
| 73 | + { name: 'User5' }, |
| 74 | + { name: 'User6' }, |
| 75 | + { name: 'User7' }, |
| 76 | + { name: 'User8' }, |
| 77 | + { name: 'User9' }, |
| 78 | + { name: 'User1' }, |
| 79 | + { name: 'User1' }, |
| 80 | + { name: 'User1' }, |
| 81 | + { name: 'User1' }, |
| 82 | + { name: 'User1' }, |
| 83 | + { name: 'User1' }, |
| 84 | + { name: 'User1' }, |
| 85 | + { name: 'User1' }, |
| 86 | + { name: 'User1' }, |
| 87 | + { name: 'User1' }, |
| 88 | + { name: 'User2' }, |
| 89 | + ]); |
69 | 90 |
|
70 | 91 | // users
|
71 |
| - await queryRunner.query(` |
72 |
| - INSERT INTO public.users ("email", "isActive", "companyId", "profileId", "nameFirst", "nameLast") VALUES |
73 |
| - ('[email protected]', true, 1, 1, 'firstname1', 'lastname1'), |
74 |
| - ('[email protected]', true, 1, 2, NULL, NULL), |
75 |
| - ('[email protected]', true, 1, 3, NULL, NULL), |
76 |
| - ('[email protected]', true, 1, 4, NULL, NULL), |
77 |
| - ('[email protected]', true, 1, 5, NULL, NULL), |
78 |
| - ('[email protected]', true, 1, 6, NULL, NULL), |
79 |
| - ('[email protected]', false, 1, 7, NULL, NULL), |
80 |
| - ('[email protected]', false, 1, 8, NULL, NULL), |
81 |
| - ('[email protected]', false, 1, 9, NULL, NULL), |
82 |
| - ('[email protected]', true, 1, 10, NULL, NULL), |
83 |
| - ('[email protected]', true, 2, 11, NULL, NULL), |
84 |
| - ('[email protected]', true, 2, 12, NULL, NULL), |
85 |
| - ('[email protected]', true, 2, 13, NULL, NULL), |
86 |
| - ('[email protected]', true, 2, 14, NULL, NULL), |
87 |
| - ('[email protected]', true, 2, 15, NULL, NULL), |
88 |
| - ('[email protected]', true, 2, 16, NULL, NULL), |
89 |
| - ('[email protected]', false, 2, 17, NULL, NULL), |
90 |
| - ('[email protected]', false, 2, 18, NULL, NULL), |
91 |
| - ('[email protected]', false, 2, 19, NULL, NULL), |
92 |
| - ('[email protected]', false, 2, 20, NULL, NULL), |
93 |
| - ('[email protected]', false, 2, NULL, NULL, NULL); |
94 |
| - `); |
| 92 | + const name: Name = { first: null, last: null }; |
| 93 | + const name1: Name = { first: 'firstname1', last: 'lastname1' }; |
| 94 | + await this.save(usersRepo, User, [ |
| 95 | + { email: '[email protected]', isActive: true, companyId: 1, profileId: 1, name: name1 }, |
| 96 | + { email: '[email protected]', isActive: true, companyId: 1, profileId: 2, name }, |
| 97 | + { email: '[email protected]', isActive: true, companyId: 1, profileId: 3, name }, |
| 98 | + { email: '[email protected]', isActive: true, companyId: 1, profileId: 4, name }, |
| 99 | + { email: '[email protected]', isActive: true, companyId: 1, profileId: 5, name }, |
| 100 | + { email: '[email protected]', isActive: true, companyId: 1, profileId: 6, name }, |
| 101 | + { email: '[email protected]', isActive: false, companyId: 1, profileId: 7, name }, |
| 102 | + { email: '[email protected]', isActive: false, companyId: 1, profileId: 8, name }, |
| 103 | + { email: '[email protected]', isActive: false, companyId: 1, profileId: 9, name }, |
| 104 | + { email: '[email protected]', isActive: true, companyId: 1, profileId: 10, name }, |
| 105 | + { email: '[email protected]', isActive: true, companyId: 2, profileId: 11, name }, |
| 106 | + { email: '[email protected]', isActive: true, companyId: 2, profileId: 12, name }, |
| 107 | + { email: '[email protected]', isActive: true, companyId: 2, profileId: 13, name }, |
| 108 | + { email: '[email protected]', isActive: true, companyId: 2, profileId: 14, name }, |
| 109 | + { email: '[email protected]', isActive: true, companyId: 2, profileId: 15, name }, |
| 110 | + { email: '[email protected]', isActive: true, companyId: 2, profileId: 16, name }, |
| 111 | + { email: '[email protected]', isActive: false, companyId: 2, profileId: 17, name }, |
| 112 | + { email: '[email protected]', isActive: false, companyId: 2, profileId: 18, name }, |
| 113 | + { email: '[email protected]', isActive: false, companyId: 2, profileId: 19, name }, |
| 114 | + { email: '[email protected]', isActive: false, companyId: 2, profileId: 20, name }, |
| 115 | + { email: '[email protected]', isActive: false, companyId: 2, profileId: null, name }, |
| 116 | + ]); |
95 | 117 |
|
96 | 118 | // licenses
|
97 |
| - await queryRunner.query(` |
98 |
| - INSERT INTO public.licenses ("name") VALUES |
99 |
| - ('License1'), |
100 |
| - ('License2'), |
101 |
| - ('License3'), |
102 |
| - ('License4'), |
103 |
| - ('License5'); |
104 |
| - `); |
| 119 | + await this.save(licensesRepo, License, [ |
| 120 | + { name: 'License1' }, |
| 121 | + { name: 'License2' }, |
| 122 | + { name: 'License3' }, |
| 123 | + { name: 'License4' }, |
| 124 | + { name: 'License5' }, |
| 125 | + ]); |
105 | 126 |
|
106 | 127 | // user-licenses
|
107 |
| - await queryRunner.query(` |
108 |
| - INSERT INTO public.user_licenses ("userId", "licenseId", "yearsActive") VALUES |
109 |
| - (1, 1, 3), |
110 |
| - (1, 2, 5), |
111 |
| - (1, 4, 7), |
112 |
| - (2, 5, 1); |
113 |
| - `); |
| 128 | + await this.save(usersLincesesRepo, UserLicense, [ |
| 129 | + { userId: 1, licenseId: 1, yearsActive: 3 }, |
| 130 | + { userId: 1, licenseId: 2, yearsActive: 5 }, |
| 131 | + { userId: 1, licenseId: 4, yearsActive: 7 }, |
| 132 | + { userId: 2, licenseId: 5, yearsActive: 1 }, |
| 133 | + ]); |
114 | 134 |
|
115 | 135 | // user-projects
|
116 |
| - await queryRunner.query(` |
117 |
| - INSERT INTO public.user_projects ("projectId", "userId", "review") VALUES |
118 |
| - (1, 1, 'User project 1 1'), |
119 |
| - (1, 2, 'User project 1 2'), |
120 |
| - (2, 2, 'User project 2 2'), |
121 |
| - (3, 3, 'User project 3 3'); |
122 |
| - `); |
| 136 | + await this.save(usersProjectsRepo, UserProject, [ |
| 137 | + { projectId: 1, userId: 1, review: 'User project 1 1' }, |
| 138 | + { projectId: 1, userId: 2, review: 'User project 1 2' }, |
| 139 | + { projectId: 2, userId: 2, review: 'User project 2 2' }, |
| 140 | + { projectId: 3, userId: 3, review: 'User project 3 3' }, |
| 141 | + ]); |
123 | 142 | }
|
124 | 143 |
|
125 | 144 | public async down(queryRunner: QueryRunner): Promise<any> {}
|
|
0 commit comments