Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override params on factories before persisting #112

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/entity-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class EntityFactory<Entity, Context> {

/**
* This function is used to alter the generated values of entity, before it
* is persist into the database
* is persisted into the database
*/
public map(mapFunction: (entity: Entity) => Promise<Entity>): EntityFactory<Entity, Context> {
this.mapFunction = mapFunction
Expand All @@ -32,7 +32,7 @@ export class EntityFactory<Entity, Context> {
* Make a new entity, but does not persist it
*/
public async make(overrideParams: EntityProperty<Entity> = {}): Promise<Entity> {
return this.makeEnity(overrideParams, false)
return this.makeEntity(overrideParams, false)
}

/**
Expand All @@ -44,7 +44,7 @@ export class EntityFactory<Entity, Context> {
if (connection && connection.isConnected) {
const em = connection.createEntityManager()
try {
const entity = await this.makeEnity(overrideParams, true)
const entity = await this.makeEntity(overrideParams, true)
return await em.save<Entity>(entity, saveOptions)
} catch (error) {
const message = 'Could not save entity'
Expand Down Expand Up @@ -92,12 +92,13 @@ export class EntityFactory<Entity, Context> {
// Private Helpers
// -------------------------------------------------------------------------

private async makeEnity(overrideParams: EntityProperty<Entity> = {}, isSeeding = false): Promise<Entity> {
private async makeEntity(overrideParams: EntityProperty<Entity> = {}, isSeeding = false): Promise<Entity> {
if (!this.factory) {
throw new Error('Could not found entity')
}

let entity = await this.resolveEntity(this.factory(Faker, this.context), isSeeding)
let entity = this.factory(Faker, this.context)

if (this.mapFunction) {
entity = await this.mapFunction(entity)
}
Expand All @@ -108,7 +109,7 @@ export class EntityFactory<Entity, Context> {
}
}

return entity
return await this.resolveEntity(entity, isSeeding)
}

private async resolveEntity(entity: Entity, isSeeding = false): Promise<Entity> {
Expand Down