Skip to content

Commit

Permalink
refactor: rename Settings to Context
Browse files Browse the repository at this point in the history
  • Loading branch information
hirsch committed Apr 12, 2020
1 parent 1b4dd16 commit 9f7b293
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/entity-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { FactoryFunction, EntityProperty } from './types'
import { isPromiseLike } from './utils/factory.util'
import { printError } from './utils/log.util'

export class EntityFactory<Entity, Settings> {
export class EntityFactory<Entity, Context> {
private mapFunction: (entity: Entity) => Promise<Entity>

constructor(
public name: string,
public entity: ObjectType<Entity>,
private factory: FactoryFunction<Entity, Settings>,
private settings?: Settings,
private factory: FactoryFunction<Entity, Context>,
private context?: Context,
) {}

// -------------------------------------------------------------------------
Expand All @@ -22,7 +22,7 @@ export class EntityFactory<Entity, Settings> {
* This function is used to alter the generated values of entity, before it
* is persist into the database
*/
public map(mapFunction: (entity: Entity) => Promise<Entity>): EntityFactory<Entity, Settings> {
public map(mapFunction: (entity: Entity) => Promise<Entity>): EntityFactory<Entity, Context> {
this.mapFunction = mapFunction
return this
}
Expand Down Expand Up @@ -78,7 +78,7 @@ export class EntityFactory<Entity, Settings> {

private async makeEnity(overrideParams: EntityProperty<Entity> = {}, isSeeding = false): Promise<Entity> {
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)
}
Expand Down
6 changes: 3 additions & 3 deletions src/typeorm-seeding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, Settings>(entity: ObjectType<Entity>, factoryFn: FactoryFunction<Entity, Settings>) => {
export const define = <Entity, Context>(entity: ObjectType<Entity>, factoryFn: FactoryFunction<Entity, Context>) => {
;(global as any).seeder.entityFactories.set(getNameOfEntity(entity), {
entity,
factory: factoryFn,
})
}

export const factory: Factory = <Entity, Settings>(entity: ObjectType<Entity>) => (settings?: Settings) => {
export const factory: Factory = <Entity, Context>(entity: ObjectType<Entity>) => (context?: Context) => {
const name = getNameOfEntity(entity)
const entityFactoryObject = (global as any).seeder.entityFactories.get(name)
return new EntityFactory<Entity, Settings>(name, entity, entityFactoryObject.factory, settings)
return new EntityFactory<Entity, Context>(name, entity, entityFactoryObject.factory, context)
}

export const runSeeder = async (clazz: SeederConstructor): Promise<void> => {
Expand Down
12 changes: 6 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import { EntityFactory } from './entity-factory'
/**
* FactoryFunction is the fucntion, which generate a new filled entity
*/
export type FactoryFunction<Entity, Settings> = (faker: typeof Faker, settings?: Settings) => Entity
export type FactoryFunction<Entity, Context> = (faker: typeof Faker, context?: Context) => Entity

/**
* EntityProperty defines an object whose keys and values must be properties of the given Entity.
*/
export type EntityProperty<Entity> = { [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 = <Entity, Settings>(
export type Factory = <Entity, Context>(
entity: ObjectType<Entity>,
) => (settings?: Settings) => EntityFactory<Entity, Settings>
) => (context?: Context) => EntityFactory<Entity, Context>

/**
* Seed are the class to create some data. Those seed are run by the cli.
Expand All @@ -35,7 +35,7 @@ export type SeederConstructor = new () => Seeder
/**
* Value of our EntityFactory state
*/
export interface EntityFactoryDefinition<Entity, Settings> {
export interface EntityFactoryDefinition<Entity, Context> {
entity: ObjectType<Entity>
factory: FactoryFunction<Entity, Settings>
factory: FactoryFunction<Entity, Context>
}

0 comments on commit 9f7b293

Please sign in to comment.