@@ -6,98 +6,98 @@ import { isPromiseLike } from './utils';
6
6
7
7
export class EntityFactory < Entity , Settings > {
8
8
9
- private mapFunction : ( entity : Entity ) => Promise < Entity > ;
9
+ private mapFunction : ( entity : Entity ) => Promise < Entity > ;
10
10
11
- constructor (
12
- public name : string ,
13
- public entity : ObjectType < Entity > ,
14
- private factory : FactoryFunction < Entity , Settings > ,
15
- private settings ?: Settings
16
- ) { }
11
+ constructor (
12
+ public name : string ,
13
+ public entity : ObjectType < Entity > ,
14
+ private factory : FactoryFunction < Entity , Settings > ,
15
+ private settings ?: Settings
16
+ ) { }
17
17
18
- // -------------------------------------------------------------------------
19
- // Public API
20
- // -------------------------------------------------------------------------
18
+ // -------------------------------------------------------------------------
19
+ // Public API
20
+ // -------------------------------------------------------------------------
21
21
22
- /**
23
- * This function is used to alter the generated values of entity, before it
24
- * is persist into the database
25
- */
26
- public map ( mapFunction : ( entity : Entity ) => Promise < Entity > ) : EntityFactory < Entity , Settings > {
27
- this . mapFunction = mapFunction ;
28
- return this ;
29
- }
22
+ /**
23
+ * This function is used to alter the generated values of entity, before it
24
+ * is persist into the database
25
+ */
26
+ public map ( mapFunction : ( entity : Entity ) => Promise < Entity > ) : EntityFactory < Entity , Settings > {
27
+ this . mapFunction = mapFunction ;
28
+ return this ;
29
+ }
30
30
31
- /**
32
- * Make a new entity, but does not persist it
33
- */
34
- public async make ( ) : Promise < Entity > {
35
- if ( this . factory ) {
36
- let entity = await this . resolveEntity ( this . factory ( Faker , this . settings ) ) ;
37
- if ( this . mapFunction ) {
38
- entity = await this . mapFunction ( entity ) ;
39
- }
40
- return entity ;
41
- }
42
- throw new Error ( 'Could not found entity' ) ;
31
+ /**
32
+ * Make a new entity, but does not persist it
33
+ */
34
+ public async make ( ) : Promise < Entity > {
35
+ if ( this . factory ) {
36
+ let entity = await this . resolveEntity ( this . factory ( Faker , this . settings ) ) ;
37
+ if ( this . mapFunction ) {
38
+ entity = await this . mapFunction ( entity ) ;
39
+ }
40
+ return entity ;
43
41
}
42
+ throw new Error ( 'Could not found entity' ) ;
43
+ }
44
44
45
- /**
46
- * Seed makes a new entity and does persist it
47
- */
48
- public async seed ( ) : Promise < Entity > {
49
- const connection : Connection = ( global as any ) . seeder . connection ;
50
- if ( connection ) {
51
- const em = connection . createEntityManager ( ) ;
52
- try {
53
- const entity = await this . make ( ) ;
54
- return await em . save < Entity > ( entity ) ;
55
- } catch ( error ) {
56
- throw new Error ( 'Could not save entity' ) ;
57
- }
58
- } else {
59
- throw new Error ( 'No db connection is given' ) ;
60
- }
45
+ /**
46
+ * Seed makes a new entity and does persist it
47
+ */
48
+ public async seed ( ) : Promise < Entity > {
49
+ const connection : Connection = ( global as any ) . seeder . connection ;
50
+ if ( connection ) {
51
+ const em = connection . createEntityManager ( ) ;
52
+ try {
53
+ const entity = await this . make ( ) ;
54
+ return await em . save < Entity > ( entity ) ;
55
+ } catch ( error ) {
56
+ throw new Error ( 'Could not save entity' ) ;
57
+ }
58
+ } else {
59
+ throw new Error ( 'No db connection is given' ) ;
61
60
}
61
+ }
62
62
63
- public async makeMany ( amount : number ) : Promise < Entity [ ] > {
64
- const list = [ ] ;
65
- for ( let index = 0 ; index < amount ; index ++ ) {
66
- list [ index ] = await this . make ( ) ;
67
- }
68
- return list ;
63
+ public async makeMany ( amount : number ) : Promise < Entity [ ] > {
64
+ const list = [ ] ;
65
+ for ( let index = 0 ; index < amount ; index ++ ) {
66
+ list [ index ] = await this . make ( ) ;
69
67
}
68
+ return list ;
69
+ }
70
70
71
- public async seedMany ( amount : number ) : Promise < Entity [ ] > {
72
- const list = [ ] ;
73
- for ( let index = 0 ; index < amount ; index ++ ) {
74
- list [ index ] = await this . seed ( ) ;
75
- }
76
- return list ;
71
+ public async seedMany ( amount : number ) : Promise < Entity [ ] > {
72
+ const list = [ ] ;
73
+ for ( let index = 0 ; index < amount ; index ++ ) {
74
+ list [ index ] = await this . seed ( ) ;
77
75
}
76
+ return list ;
77
+ }
78
78
79
- // -------------------------------------------------------------------------
80
- // Prrivat Helpers
81
- // -------------------------------------------------------------------------
79
+ // -------------------------------------------------------------------------
80
+ // Prrivat Helpers
81
+ // -------------------------------------------------------------------------
82
82
83
- private async resolveEntity ( entity : Entity ) : Promise < Entity > {
84
- for ( const attribute in entity ) {
85
- if ( entity . hasOwnProperty ( attribute ) ) {
86
- if ( isPromiseLike ( entity [ attribute ] ) ) {
87
- entity [ attribute ] = await entity [ attribute ] ;
88
- }
83
+ private async resolveEntity ( entity : Entity ) : Promise < Entity > {
84
+ for ( const attribute in entity ) {
85
+ if ( entity . hasOwnProperty ( attribute ) ) {
86
+ if ( isPromiseLike ( entity [ attribute ] ) ) {
87
+ entity [ attribute ] = await entity [ attribute ] ;
88
+ }
89
89
90
- if ( typeof entity [ attribute ] === 'object' ) {
91
- const subEntityFactory = entity [ attribute ] ;
92
- try {
93
- entity [ attribute ] = await ( subEntityFactory as any ) . make ( ) ;
94
- } catch ( e ) {
95
- throw new Error ( `Could not make ${ ( subEntityFactory as any ) . name } ` ) ;
96
- }
97
- }
98
- }
90
+ if ( typeof entity [ attribute ] === 'object' && ! ( entity [ attribute ] instanceof Date ) ) {
91
+ const subEntityFactory = entity [ attribute ] ;
92
+ try {
93
+ entity [ attribute ] = await ( subEntityFactory as any ) . make ( ) ;
94
+ } catch ( e ) {
95
+ throw new Error ( `Could not make ${ ( subEntityFactory as any ) . name } ` ) ;
96
+ }
99
97
}
100
- return entity ;
98
+ }
101
99
}
100
+ return entity ;
101
+ }
102
102
103
103
}
0 commit comments