diff --git a/src/entity-factory.ts b/src/entity-factory.ts index 001229e8..dffe4990 100644 --- a/src/entity-factory.ts +++ b/src/entity-factory.ts @@ -4,14 +4,14 @@ import { FactoryFunction, EntityProperty } from './types' import { isPromiseLike } from './utils/factory.util' import { printError } from './utils/log.util' -export class EntityFactory { +export class EntityFactory { private mapFunction: (entity: Entity) => Promise constructor( public name: string, public entity: ObjectType, - private factory: FactoryFunction, - private settings?: Settings, + private factory: FactoryFunction, + private context?: Context, ) {} // ------------------------------------------------------------------------- @@ -22,7 +22,7 @@ export class EntityFactory { * This function is used to alter the generated values of entity, before it * is persist into the database */ - public map(mapFunction: (entity: Entity) => Promise): EntityFactory { + public map(mapFunction: (entity: Entity) => Promise): EntityFactory { this.mapFunction = mapFunction return this } @@ -78,7 +78,7 @@ export class EntityFactory { private async makeEnity(overrideParams: EntityProperty = {}, isSeeding = false): Promise { if (this.factory) { - let entity = await this.resolveEntity(this.factory(Faker, this.settings), isSeeding) + let entity = await this.resolveEntity(this.factory(Faker, this.context), isSeeding) if (this.mapFunction) { entity = await this.mapFunction(entity) } diff --git a/src/typeorm-seeding.ts b/src/typeorm-seeding.ts index b479edd6..66e95cb6 100644 --- a/src/typeorm-seeding.ts +++ b/src/typeorm-seeding.ts @@ -30,17 +30,17 @@ export const setConnection = (connection: Connection) => ((global as any).seeder export const getConnection = () => (global as any).seeder.connection -export const define = (entity: ObjectType, factoryFn: FactoryFunction) => { +export const define = (entity: ObjectType, factoryFn: FactoryFunction) => { ;(global as any).seeder.entityFactories.set(getNameOfEntity(entity), { entity, factory: factoryFn, }) } -export const factory: Factory = (entity: ObjectType) => (settings?: Settings) => { +export const factory: Factory = (entity: ObjectType) => (context?: Context) => { const name = getNameOfEntity(entity) const entityFactoryObject = (global as any).seeder.entityFactories.get(name) - return new EntityFactory(name, entity, entityFactoryObject.factory, settings) + return new EntityFactory(name, entity, entityFactoryObject.factory, context) } export const runSeeder = async (clazz: SeederConstructor): Promise => { diff --git a/src/types.ts b/src/types.ts index 12bc8e49..d83a3bb9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,7 +6,7 @@ import { EntityFactory } from './entity-factory' /** * FactoryFunction is the fucntion, which generate a new filled entity */ -export type FactoryFunction = (faker: typeof Faker, settings?: Settings) => Entity +export type FactoryFunction = (faker: typeof Faker, context?: Context) => Entity /** * EntityProperty defines an object whose keys and values must be properties of the given Entity. @@ -14,11 +14,11 @@ export type FactoryFunction = (faker: typeof Faker, settings?: export type EntityProperty = { [Property in keyof Entity]?: Entity[Property] } /** - * Factory gets the EntityFactory to the given Entity and pass the settings along + * Factory gets the EntityFactory to the given Entity and pass the context along */ -export type Factory = ( +export type Factory = ( entity: ObjectType, -) => (settings?: Settings) => EntityFactory +) => (context?: Context) => EntityFactory /** * Seed are the class to create some data. Those seed are run by the cli. @@ -35,7 +35,7 @@ export type SeederConstructor = new () => Seeder /** * Value of our EntityFactory state */ -export interface EntityFactoryDefinition { +export interface EntityFactoryDefinition { entity: ObjectType - factory: FactoryFunction + factory: FactoryFunction }