-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add module for db-seeding with factories, PersonFactory
- Loading branch information
Showing
5 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { CommandRunner, SubCommand } from 'nest-commander'; | ||
import { LoggerService } from '../shared/logging/index.js'; | ||
import { MikroORM } from '@mikro-orm/core'; | ||
import { PersonFactory } from '../../test/person-factory.js'; | ||
import { PersonEntity } from '../modules/person/persistence/person.entity.js'; | ||
|
||
@SubCommand({ name: 'seedwf', description: 'creates seed data in the database' }) | ||
export class DbSeedWFConsole extends CommandRunner { | ||
public constructor( | ||
private readonly orm: MikroORM, | ||
private readonly logger: LoggerService, | ||
) { | ||
super(); | ||
} | ||
|
||
public override async run(_passedParams: string[], _options?: Record<string, unknown>): Promise<void> { | ||
this.logger.info('Create seed data in the database with factories ...'); | ||
/* eslint-disable @typescript-eslint/typedef */ | ||
const forkedEm = this.orm.em.fork(); | ||
const persons: PersonEntity[] = new PersonFactory(forkedEm).make(1); | ||
for (const person of persons) { | ||
await forkedEm.persistAndFlush(person); | ||
} | ||
this.logger.info('Created seed data successfully'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { PersonEntity } from '../src/modules/person/persistence/person.entity.js'; | ||
import { Factory, Faker } from '@mikro-orm/seeder'; | ||
import { EntityData } from '@mikro-orm/core'; | ||
import { faker as facer, SexType } from '@faker-js/faker'; | ||
import { Geschlecht, Vertrauensstufe } from '../src/modules/person/domain/person.enums.js'; | ||
|
||
export class PersonFactory extends Factory<PersonEntity> { | ||
/* eslint-disable @typescript-eslint/typedef */ | ||
public model = PersonEntity; | ||
|
||
protected definition(faker: Faker): EntityData<PersonEntity> { | ||
const sexType: SexType = this.getRandomSex(); | ||
return { | ||
keycloakUserId: facer.string.uuid(), | ||
referrer: facer.string.numeric(), | ||
mandant: facer.string.numeric(), | ||
stammorganisation: facer.string.numeric(), | ||
familienname: facer.person.lastName(), | ||
vorname: facer.person.firstName(sexType), | ||
nameTitel: facer.string.alpha(), | ||
nameAnrede: [facer.person.prefix(sexType)], | ||
namePraefix: [facer.person.prefix(sexType)], | ||
nameSuffix: [], | ||
nameSortierindex: facer.string.alpha(), | ||
geburtsdatum: faker.date.past(), | ||
geburtsort: facer.location.city(), | ||
geschlecht: this.getRandomSex() === 'female' ? Geschlecht.W : Geschlecht.M, | ||
vertrauensstufe: this.getRandomVetrauensstufe(), | ||
auskunftssperre: Math.random() < 0.5, | ||
}; | ||
} | ||
|
||
private getRandomSex(): SexType { | ||
return Math.random() < 0.5 ? 'female' : 'male'; | ||
} | ||
|
||
private getRandomVetrauensstufe(): Vertrauensstufe { | ||
const randomInt: number = Math.floor(Math.random() * 4) + 1; | ||
switch (randomInt) { | ||
case 1: | ||
return Vertrauensstufe.KEIN; | ||
case 2: | ||
return Vertrauensstufe.UNBE; | ||
case 3: | ||
return Vertrauensstufe.TEIL; | ||
default: | ||
return Vertrauensstufe.VOLL; | ||
} | ||
} | ||
} |