Skip to content

Commit

Permalink
cleanup and move factories for db-seeding
Browse files Browse the repository at this point in the history
  • Loading branch information
DPDS93CT committed Nov 16, 2023
1 parent 9c5f52a commit 7746a23
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 72 deletions.
71 changes: 24 additions & 47 deletions src/console/db-seed-with-factory.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -23,55 +24,31 @@ export class DbSeedWFConsole extends CommandRunner {
public override async run(_passedParams: string[], _options?: Record<string, unknown>): Promise<void> {
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');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<PersonEntity> {
/* eslint-disable @typescript-eslint/typedef */
public model = PersonEntity;
public model: Constructor<PersonEntity> = PersonEntity;

protected definition(_faker: Faker): EntityData<PersonEntity> {
const sexType: SexType = this.getRandomSex();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<PersonRollenZuweisungEntity> {
/* eslint-disable @typescript-eslint/typedef */
public model = PersonRollenZuweisungEntity;
public model: Constructor<PersonRollenZuweisungEntity> = PersonRollenZuweisungEntity;

protected definition(_faker: Faker): EntityData<PersonRollenZuweisungEntity> {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<RolleBerechtigungsZuweisungEntity> {
/* eslint-disable @typescript-eslint/typedef */
public model = RolleBerechtigungsZuweisungEntity;
public model: Constructor<RolleBerechtigungsZuweisungEntity> = RolleBerechtigungsZuweisungEntity;

protected definition(_faker: Faker): EntityData<RolleBerechtigungsZuweisungEntity> {
return {
Expand Down
7 changes: 3 additions & 4 deletions test/rolle-factory.ts → src/shared/dbinit/rolle-factory.ts
Original file line number Diff line number Diff line change
@@ -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<RolleEntity> {
/* eslint-disable @typescript-eslint/typedef */
public model = RolleEntity;
public model: Constructor<RolleEntity> = RolleEntity;

protected definition(_faker: Faker): EntityData<RolleEntity> {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ServiceProviderEntity> {
/* eslint-disable @typescript-eslint/typedef */
public model = ServiceProviderEntity;
public model: Constructor<ServiceProviderEntity> = ServiceProviderEntity;

protected definition(_faker: Faker): EntityData<ServiceProviderEntity> {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ServiceProviderZugriffEntity> {
/* eslint-disable @typescript-eslint/typedef */
public model = ServiceProviderZugriffEntity;
public model: Constructor<ServiceProviderZugriffEntity> = ServiceProviderZugriffEntity;

protected definition(_faker: Faker): EntityData<ServiceProviderZugriffEntity> {
return {
Expand Down

0 comments on commit 7746a23

Please sign in to comment.