From ca88005775d0aa37d1668d458ad17d260b192499 Mon Sep 17 00:00:00 2001 From: Samuel Martins Date: Thu, 31 Dec 2020 17:19:16 -0300 Subject: [PATCH] feat: add save options to create and createMany --- src/entity-factory.ts | 82 +++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/src/entity-factory.ts b/src/entity-factory.ts index 2badd1d3..75cc13d0 100644 --- a/src/entity-factory.ts +++ b/src/entity-factory.ts @@ -1,5 +1,5 @@ import * as Faker from 'faker' -import { ObjectType } from 'typeorm' +import { ObjectType, SaveOptions } from 'typeorm' import { FactoryFunction, EntityProperty } from './types' import { isPromiseLike } from './utils/factory.util' import { printError, printWarning } from './utils/log.util' @@ -38,14 +38,14 @@ export class EntityFactory { /** * Create makes a new entity and does persist it */ - public async create(overrideParams: EntityProperty = {}): Promise { + public async create(overrideParams: EntityProperty = {}, saveOptions?: SaveOptions): Promise { const option = await getConnectionOptions() const connection = await createConnection(option) if (connection && connection.isConnected) { const em = connection.createEntityManager() try { const entity = await this.makeEnity(overrideParams, true) - return await em.save(entity) + return await em.save(entity, saveOptions) } catch (error) { const message = 'Could not save entity' printError(message, error) @@ -66,10 +66,14 @@ export class EntityFactory { return list } - public async createMany(amount: number, overrideParams: EntityProperty = {}): Promise { + public async createMany( + amount: number, + overrideParams: EntityProperty = {}, + saveOptions?: SaveOptions, + ): Promise { const list = [] for (let index = 0; index < amount; index++) { - list[index] = await this.create(overrideParams) + list[index] = await this.create(overrideParams, saveOptions) } return list } @@ -85,50 +89,52 @@ export class EntityFactory { } // ------------------------------------------------------------------------- - // Prrivat Helpers + // Private Helpers // ------------------------------------------------------------------------- private async makeEnity(overrideParams: EntityProperty = {}, isSeeding = false): Promise { - if (this.factory) { - let entity = await this.resolveEntity(this.factory(Faker, this.context), isSeeding) - if (this.mapFunction) { - entity = await this.mapFunction(entity) - } + if (!this.factory) { + throw new Error('Could not found entity') + } - for (const key in overrideParams) { - if (overrideParams.hasOwnProperty(key)) { - entity[key] = overrideParams[key] - } - } + let entity = await this.resolveEntity(this.factory(Faker, this.context), isSeeding) + if (this.mapFunction) { + entity = await this.mapFunction(entity) + } - return entity + for (const key in overrideParams) { + if (overrideParams.hasOwnProperty(key)) { + entity[key] = overrideParams[key] + } } - throw new Error('Could not found entity') + + return entity } private async resolveEntity(entity: Entity, isSeeding = false): Promise { for (const attribute in entity) { - if (entity.hasOwnProperty(attribute)) { - if (isPromiseLike(entity[attribute])) { - entity[attribute] = await entity[attribute] - } - if ( - entity[attribute] && - typeof entity[attribute] === 'object' && - entity[attribute].constructor.name === EntityFactory.name - ) { - const subEntityFactory = entity[attribute] - try { - if (isSeeding) { - entity[attribute] = await (subEntityFactory as any).create() - } else { - entity[attribute] = await (subEntityFactory as any).make() - } - } catch (error) { - const message = `Could not make ${(subEntityFactory as any).name}` - printError(message, error) - throw new Error(message) + if (!entity.hasOwnProperty(attribute)) { + continue + } + if (isPromiseLike(entity[attribute])) { + entity[attribute] = await entity[attribute] + } + if ( + entity[attribute] && + typeof entity[attribute] === 'object' && + entity[attribute].constructor.name === EntityFactory.name + ) { + const subEntityFactory = entity[attribute] + try { + if (isSeeding) { + entity[attribute] = await (subEntityFactory as any).create() + } else { + entity[attribute] = await (subEntityFactory as any).make() } + } catch (error) { + const message = `Could not make ${(subEntityFactory as any).name}` + printError(message, error) + throw new Error(message) } } }