From 7746a23b142e6d2babae120c492673ba815af21a Mon Sep 17 00:00:00 2001 From: DPDS93CT Date: Thu, 16 Nov 2023 13:54:47 +0100 Subject: [PATCH] cleanup and move factories for db-seeding --- src/console/db-seed-with-factory.ts | 71 +++++++------------ {test => src/shared/dbinit}/person-factory.ts | 9 ++- .../person-rollen-zuweisung-factory.ts | 7 +- .../rolle-berechtigungs-zuweisung-factory.ts | 7 +- {test => src/shared/dbinit}/rolle-factory.ts | 7 +- .../dbinit}/service-provider-factory.ts | 7 +- .../service-provider-zugriff-factory.ts | 7 +- 7 files changed, 43 insertions(+), 72 deletions(-) rename {test => src/shared/dbinit}/person-factory.ts (84%) rename {test => src/shared/dbinit}/person-rollen-zuweisung-factory.ts (61%) rename {test => src/shared/dbinit}/rolle-berechtigungs-zuweisung-factory.ts (63%) rename {test => src/shared/dbinit}/rolle-factory.ts (62%) rename {test => src/shared/dbinit}/service-provider-factory.ts (65%) rename {test => src/shared/dbinit}/service-provider-zugriff-factory.ts (53%) diff --git a/src/console/db-seed-with-factory.ts b/src/console/db-seed-with-factory.ts index 744cfe2a7..9cef0632a 100644 --- a/src/console/db-seed-with-factory.ts +++ b/src/console/db-seed-with-factory.ts @@ -1,15 +1,16 @@ import { CommandRunner, SubCommand } from 'nest-commander'; import { LoggerService } from '../shared/logging/index.js'; import { EntityManager, MikroORM } from '@mikro-orm/core'; -import { PersonFactory } from '../../test/person-factory.js'; +import { PersonFactory } from '../shared/dbinit/person-factory.js'; import { PersonEntity } from '../modules/person/persistence/person.entity.js'; -import { ServiceProviderEntity } from '../modules/rolle/entity/service-provider.entity.js'; -import { ServiceProviderFactory } from '../../test/service-provider-factory.js'; import { ServiceProviderZugriffEntity } from '../modules/rolle/entity/service-provider-zugriff.entity.js'; -import { ServiceProviderZugriffFactory } from '../../test/service-provider-zugriff-factory.js'; +import { ServiceProviderZugriffFactory } from '../shared/dbinit/service-provider-zugriff-factory.js'; import { PersonRollenZuweisungEntity } from '../modules/rolle/entity/person-rollen-zuweisung.entity.js'; -import { PersonRollenZuweisungFactory } from '../../test/person-rollen-zuweisung-factory.js'; -import { RolleFactory } from '../../test/rolle-factory.js'; +import { PersonRollenZuweisungFactory } from '../shared/dbinit/person-rollen-zuweisung-factory.js'; +import { RolleFactory } from '../shared/dbinit/rolle-factory.js'; +import { RolleEntity } from '../modules/rolle/entity/rolle.entity.js'; +import { RolleBerechtigungsZuweisungEntity } from '../modules/rolle/entity/rolle-berechtigungs-zuweisung.entity.js'; +import { RolleBerechtigungsZuweisungFactory } from '../shared/dbinit/rolle-berechtigungs-zuweisung-factory.js'; @SubCommand({ name: 'seedwf', description: 'creates seed data in the database' }) export class DbSeedWFConsole extends CommandRunner { @@ -23,55 +24,31 @@ export class DbSeedWFConsole extends CommandRunner { public override async run(_passedParams: string[], _options?: Record): Promise { this.logger.info('Create seed data in the database with factories ...'); const forkedEm: EntityManager = this.orm.em.fork(); - - const serviceProviders: ServiceProviderEntity[] = new ServiceProviderFactory(forkedEm).make(1); - for (const serviceProvider of serviceProviders) { - await forkedEm.persistAndFlush(serviceProvider); - } - - let serviceProviderZugriffList: ServiceProviderZugriffEntity[] = new ServiceProviderZugriffFactory( - forkedEm, - ).make(1); - for (const serviceProviderZugriff of serviceProviderZugriffList) { - await forkedEm.persistAndFlush(serviceProviderZugriff); - } - serviceProviderZugriffList = new ServiceProviderZugriffFactory(forkedEm).make(1, { - serviceProvider: '999', - }); - for (const serviceProviderZugriff of serviceProviderZugriffList) { - await forkedEm.persistAndFlush(serviceProviderZugriff); - } - - const persons: PersonEntity[] = new PersonFactory(forkedEm).make(1); - for (const person of persons) { - await forkedEm.persistAndFlush(person); + let amount: number = 10; + if (_passedParams[0] !== undefined) { + amount = parseInt(_passedParams[0]); } const persons2: PersonEntity[] = await new PersonFactory(forkedEm).create(1); - /* eslint-disable @typescript-eslint/no-misused-promises */ - const serviceProviderZugriffEntities: ServiceProviderZugriffEntity[] = new ServiceProviderZugriffFactory( - forkedEm, - ) - .each((spz: ServiceProviderZugriffEntity) => { - const sp: ServiceProviderEntity = new ServiceProviderFactory(forkedEm).makeOne(); - spz.serviceProvider = sp.id; - }) - .make(10); - for (const spz of serviceProviderZugriffEntities) { - await forkedEm.persistAndFlush(spz); - } - - const personRolleZuweisungList: PersonRollenZuweisungEntity[] = new PersonRollenZuweisungFactory(forkedEm) + await new PersonRollenZuweisungFactory(forkedEm) .each((prz: PersonRollenZuweisungEntity) => { - prz.rolle = new RolleFactory(forkedEm).makeOne(); + const rolle: RolleEntity = new RolleFactory(forkedEm).makeOne(); + prz.rolle = rolle; + // would override the overrideParameter of create() + /*const person: PersonEntity = await new PersonFactory(forkedEm).createOne(); + prz.person = person.id;*/ + const serviceProviderZugriff: ServiceProviderZugriffEntity = new ServiceProviderZugriffFactory( + forkedEm, + ).makeOne(); + const rolleBerechitgungsZuweisung: RolleBerechtigungsZuweisungEntity = + new RolleBerechtigungsZuweisungFactory(forkedEm).makeOne(); + rolleBerechitgungsZuweisung.rolle = rolle; + rolleBerechitgungsZuweisung.rolleRecht = serviceProviderZugriff; }) - .make(10, { + .create(amount, { person: this.getRandomFromList(persons2)?.id, }); - for (const prz of personRolleZuweisungList) { - await forkedEm.persistAndFlush(prz); - } this.logger.info('Created seed data successfully'); } diff --git a/test/person-factory.ts b/src/shared/dbinit/person-factory.ts similarity index 84% rename from test/person-factory.ts rename to src/shared/dbinit/person-factory.ts index a4de72919..fa5a73088 100644 --- a/test/person-factory.ts +++ b/src/shared/dbinit/person-factory.ts @@ -1,12 +1,11 @@ -import { PersonEntity } from '../src/modules/person/persistence/person.entity.js'; +import { PersonEntity } from '../../modules/person/persistence/person.entity.js'; import { Factory, Faker } from '@mikro-orm/seeder'; -import { EntityData } from '@mikro-orm/core'; +import { Constructor, EntityData } from '@mikro-orm/core'; import { faker as fakerjs, SexType } from '@faker-js/faker'; -import { Geschlecht, Vertrauensstufe } from '../src/modules/person/domain/person.enums.js'; +import { Geschlecht, Vertrauensstufe } from '../../modules/person/domain/person.enums.js'; export class PersonFactory extends Factory { - /* eslint-disable @typescript-eslint/typedef */ - public model = PersonEntity; + public model: Constructor = PersonEntity; protected definition(_faker: Faker): EntityData { const sexType: SexType = this.getRandomSex(); diff --git a/test/person-rollen-zuweisung-factory.ts b/src/shared/dbinit/person-rollen-zuweisung-factory.ts similarity index 61% rename from test/person-rollen-zuweisung-factory.ts rename to src/shared/dbinit/person-rollen-zuweisung-factory.ts index 9f8fd5572..299d7b46a 100644 --- a/test/person-rollen-zuweisung-factory.ts +++ b/src/shared/dbinit/person-rollen-zuweisung-factory.ts @@ -1,11 +1,10 @@ import { Factory, Faker } from '@mikro-orm/seeder'; -import { EntityData } from '@mikro-orm/core'; +import { Constructor, EntityData } from '@mikro-orm/core'; import { faker as fakerjs } from '@faker-js/faker'; -import { PersonRollenZuweisungEntity } from '../src/modules/rolle/entity/person-rollen-zuweisung.entity.js'; +import { PersonRollenZuweisungEntity } from '../../modules/rolle/entity/person-rollen-zuweisung.entity.js'; export class PersonRollenZuweisungFactory extends Factory { - /* eslint-disable @typescript-eslint/typedef */ - public model = PersonRollenZuweisungEntity; + public model: Constructor = PersonRollenZuweisungEntity; protected definition(_faker: Faker): EntityData { return { diff --git a/test/rolle-berechtigungs-zuweisung-factory.ts b/src/shared/dbinit/rolle-berechtigungs-zuweisung-factory.ts similarity index 63% rename from test/rolle-berechtigungs-zuweisung-factory.ts rename to src/shared/dbinit/rolle-berechtigungs-zuweisung-factory.ts index b6f476c90..7a1087d48 100644 --- a/test/rolle-berechtigungs-zuweisung-factory.ts +++ b/src/shared/dbinit/rolle-berechtigungs-zuweisung-factory.ts @@ -1,11 +1,10 @@ import { Factory, Faker } from '@mikro-orm/seeder'; -import { EntityData } from '@mikro-orm/core'; +import { Constructor, EntityData } from '@mikro-orm/core'; import { faker as fakerjs } from '@faker-js/faker'; -import { RolleBerechtigungsZuweisungEntity } from '../src/modules/rolle/entity/rolle-berechtigungs-zuweisung.entity.js'; +import { RolleBerechtigungsZuweisungEntity } from '../../modules/rolle/entity/rolle-berechtigungs-zuweisung.entity.js'; export class RolleBerechtigungsZuweisungFactory extends Factory { - /* eslint-disable @typescript-eslint/typedef */ - public model = RolleBerechtigungsZuweisungEntity; + public model: Constructor = RolleBerechtigungsZuweisungEntity; protected definition(_faker: Faker): EntityData { return { diff --git a/test/rolle-factory.ts b/src/shared/dbinit/rolle-factory.ts similarity index 62% rename from test/rolle-factory.ts rename to src/shared/dbinit/rolle-factory.ts index c312c065e..98494b33c 100644 --- a/test/rolle-factory.ts +++ b/src/shared/dbinit/rolle-factory.ts @@ -1,11 +1,10 @@ import { Factory, Faker } from '@mikro-orm/seeder'; -import { EntityData } from '@mikro-orm/core'; +import { Constructor, EntityData } from '@mikro-orm/core'; import { faker as fakerjs } from '@faker-js/faker'; -import { RolleEntity } from '../src/modules/rolle/entity/rolle.entity.js'; +import { RolleEntity } from '../../modules/rolle/entity/rolle.entity.js'; export class RolleFactory extends Factory { - /* eslint-disable @typescript-eslint/typedef */ - public model = RolleEntity; + public model: Constructor = RolleEntity; protected definition(_faker: Faker): EntityData { return { diff --git a/test/service-provider-factory.ts b/src/shared/dbinit/service-provider-factory.ts similarity index 65% rename from test/service-provider-factory.ts rename to src/shared/dbinit/service-provider-factory.ts index 85a257188..e6dedcbd7 100644 --- a/test/service-provider-factory.ts +++ b/src/shared/dbinit/service-provider-factory.ts @@ -1,11 +1,10 @@ import { Factory, Faker } from '@mikro-orm/seeder'; -import { EntityData } from '@mikro-orm/core'; -import { ServiceProviderEntity } from '../src/modules/rolle/entity/service-provider.entity.js'; +import { Constructor, EntityData } from '@mikro-orm/core'; +import { ServiceProviderEntity } from '../../modules/rolle/entity/service-provider.entity.js'; import { faker as fakerjs } from '@faker-js/faker'; export class ServiceProviderFactory extends Factory { - /* eslint-disable @typescript-eslint/typedef */ - public model = ServiceProviderEntity; + public model: Constructor = ServiceProviderEntity; protected definition(_faker: Faker): EntityData { return { diff --git a/test/service-provider-zugriff-factory.ts b/src/shared/dbinit/service-provider-zugriff-factory.ts similarity index 53% rename from test/service-provider-zugriff-factory.ts rename to src/shared/dbinit/service-provider-zugriff-factory.ts index 01e68155a..9fa138818 100644 --- a/test/service-provider-zugriff-factory.ts +++ b/src/shared/dbinit/service-provider-zugriff-factory.ts @@ -1,10 +1,9 @@ import { Factory, Faker } from '@mikro-orm/seeder'; -import { EntityData } from '@mikro-orm/core'; -import { ServiceProviderZugriffEntity } from '../src/modules/rolle/entity/service-provider-zugriff.entity.js'; +import { Constructor, EntityData } from '@mikro-orm/core'; +import { ServiceProviderZugriffEntity } from '../../modules/rolle/entity/service-provider-zugriff.entity.js'; export class ServiceProviderZugriffFactory extends Factory { - /* eslint-disable @typescript-eslint/typedef */ - public model = ServiceProviderZugriffEntity; + public model: Constructor = ServiceProviderZugriffEntity; protected definition(_faker: Faker): EntityData { return {