11import * as Faker from 'faker'
2- import { ObjectType } from 'typeorm'
2+ import { ObjectType , SaveOptions } from 'typeorm'
33import { FactoryFunction , EntityProperty } from './types'
44import { isPromiseLike } from './utils/factory.util'
55import { printError , printWarning } from './utils/log.util'
@@ -38,14 +38,14 @@ export class EntityFactory<Entity, Context> {
3838 /**
3939 * Create makes a new entity and does persist it
4040 */
41- public async create ( overrideParams : EntityProperty < Entity > = { } ) : Promise < Entity > {
41+ public async create ( overrideParams : EntityProperty < Entity > = { } , saveOptions ?: SaveOptions ) : Promise < Entity > {
4242 const option = await getConnectionOptions ( )
4343 const connection = await createConnection ( option )
4444 if ( connection && connection . isConnected ) {
4545 const em = connection . createEntityManager ( )
4646 try {
4747 const entity = await this . makeEnity ( overrideParams , true )
48- return await em . save < Entity > ( entity )
48+ return await em . save < Entity > ( entity , saveOptions )
4949 } catch ( error ) {
5050 const message = 'Could not save entity'
5151 printError ( message , error )
@@ -66,10 +66,14 @@ export class EntityFactory<Entity, Context> {
6666 return list
6767 }
6868
69- public async createMany ( amount : number , overrideParams : EntityProperty < Entity > = { } ) : Promise < Entity [ ] > {
69+ public async createMany (
70+ amount : number ,
71+ overrideParams : EntityProperty < Entity > = { } ,
72+ saveOptions ?: SaveOptions ,
73+ ) : Promise < Entity [ ] > {
7074 const list = [ ]
7175 for ( let index = 0 ; index < amount ; index ++ ) {
72- list [ index ] = await this . create ( overrideParams )
76+ list [ index ] = await this . create ( overrideParams , saveOptions )
7377 }
7478 return list
7579 }
@@ -85,50 +89,52 @@ export class EntityFactory<Entity, Context> {
8589 }
8690
8791 // -------------------------------------------------------------------------
88- // Prrivat Helpers
92+ // Private Helpers
8993 // -------------------------------------------------------------------------
9094
9195 private async makeEnity ( overrideParams : EntityProperty < Entity > = { } , isSeeding = false ) : Promise < Entity > {
92- if ( this . factory ) {
93- let entity = await this . resolveEntity ( this . factory ( Faker , this . context ) , isSeeding )
94- if ( this . mapFunction ) {
95- entity = await this . mapFunction ( entity )
96- }
96+ if ( ! this . factory ) {
97+ throw new Error ( 'Could not found entity' )
98+ }
9799
98- for ( const key in overrideParams ) {
99- if ( overrideParams . hasOwnProperty ( key ) ) {
100- entity [ key ] = overrideParams [ key ]
101- }
102- }
100+ let entity = await this . resolveEntity ( this . factory ( Faker , this . context ) , isSeeding )
101+ if ( this . mapFunction ) {
102+ entity = await this . mapFunction ( entity )
103+ }
103104
104- return entity
105+ for ( const key in overrideParams ) {
106+ if ( overrideParams . hasOwnProperty ( key ) ) {
107+ entity [ key ] = overrideParams [ key ]
108+ }
105109 }
106- throw new Error ( 'Could not found entity' )
110+
111+ return entity
107112 }
108113
109114 private async resolveEntity ( entity : Entity , isSeeding = false ) : Promise < Entity > {
110115 for ( const attribute in entity ) {
111- if ( entity . hasOwnProperty ( attribute ) ) {
112- if ( isPromiseLike ( entity [ attribute ] ) ) {
113- entity [ attribute ] = await entity [ attribute ]
114- }
115- if (
116- entity [ attribute ] &&
117- typeof entity [ attribute ] === 'object' &&
118- entity [ attribute ] . constructor . name === EntityFactory . name
119- ) {
120- const subEntityFactory = entity [ attribute ]
121- try {
122- if ( isSeeding ) {
123- entity [ attribute ] = await ( subEntityFactory as any ) . create ( )
124- } else {
125- entity [ attribute ] = await ( subEntityFactory as any ) . make ( )
126- }
127- } catch ( error ) {
128- const message = `Could not make ${ ( subEntityFactory as any ) . name } `
129- printError ( message , error )
130- throw new Error ( message )
116+ if ( ! entity . hasOwnProperty ( attribute ) ) {
117+ continue
118+ }
119+ if ( isPromiseLike ( entity [ attribute ] ) ) {
120+ entity [ attribute ] = await entity [ attribute ]
121+ }
122+ if (
123+ entity [ attribute ] &&
124+ typeof entity [ attribute ] === 'object' &&
125+ entity [ attribute ] . constructor . name === EntityFactory . name
126+ ) {
127+ const subEntityFactory = entity [ attribute ]
128+ try {
129+ if ( isSeeding ) {
130+ entity [ attribute ] = await ( subEntityFactory as any ) . create ( )
131+ } else {
132+ entity [ attribute ] = await ( subEntityFactory as any ) . make ( )
131133 }
134+ } catch ( error ) {
135+ const message = `Could not make ${ ( subEntityFactory as any ) . name } `
136+ printError ( message , error )
137+ throw new Error ( message )
132138 }
133139 }
134140 }
0 commit comments