1
1
import * as Faker from 'faker'
2
- import { ObjectType } from 'typeorm'
2
+ import { ObjectType , SaveOptions } from 'typeorm'
3
3
import { FactoryFunction , EntityProperty } from './types'
4
4
import { isPromiseLike } from './utils/factory.util'
5
5
import { printError , printWarning } from './utils/log.util'
@@ -38,14 +38,14 @@ export class EntityFactory<Entity, Context> {
38
38
/**
39
39
* Create makes a new entity and does persist it
40
40
*/
41
- public async create ( overrideParams : EntityProperty < Entity > = { } ) : Promise < Entity > {
41
+ public async create ( overrideParams : EntityProperty < Entity > = { } , saveOptions ?: SaveOptions ) : Promise < Entity > {
42
42
const option = await getConnectionOptions ( )
43
43
const connection = await createConnection ( option )
44
44
if ( connection && connection . isConnected ) {
45
45
const em = connection . createEntityManager ( )
46
46
try {
47
47
const entity = await this . makeEnity ( overrideParams , true )
48
- return await em . save < Entity > ( entity )
48
+ return await em . save < Entity > ( entity , saveOptions )
49
49
} catch ( error ) {
50
50
const message = 'Could not save entity'
51
51
printError ( message , error )
@@ -66,10 +66,14 @@ export class EntityFactory<Entity, Context> {
66
66
return list
67
67
}
68
68
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 [ ] > {
70
74
const list = [ ]
71
75
for ( let index = 0 ; index < amount ; index ++ ) {
72
- list [ index ] = await this . create ( overrideParams )
76
+ list [ index ] = await this . create ( overrideParams , saveOptions )
73
77
}
74
78
return list
75
79
}
@@ -85,50 +89,52 @@ export class EntityFactory<Entity, Context> {
85
89
}
86
90
87
91
// -------------------------------------------------------------------------
88
- // Prrivat Helpers
92
+ // Private Helpers
89
93
// -------------------------------------------------------------------------
90
94
91
95
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
+ }
97
99
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
+ }
103
104
104
- return entity
105
+ for ( const key in overrideParams ) {
106
+ if ( overrideParams . hasOwnProperty ( key ) ) {
107
+ entity [ key ] = overrideParams [ key ]
108
+ }
105
109
}
106
- throw new Error ( 'Could not found entity' )
110
+
111
+ return entity
107
112
}
108
113
109
114
private async resolveEntity ( entity : Entity , isSeeding = false ) : Promise < Entity > {
110
115
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 ( )
131
133
}
134
+ } catch ( error ) {
135
+ const message = `Could not make ${ ( subEntityFactory as any ) . name } `
136
+ printError ( message , error )
137
+ throw new Error ( message )
132
138
}
133
139
}
134
140
}
0 commit comments