@@ -2,14 +2,18 @@ import 'reflect-metadata';
22import { Connection , ObjectType } from 'typeorm' ;
33
44import { EntityFactory } from './EntityFactory' ;
5- import { EntityFactoryDefinition , FactoryFunction , SeedConstructor } from './types' ;
5+ import {
6+ EntityConstructor , EntityFactoryDefinition , FactoryFunction , SeedConstructor
7+ } from './types' ;
68import { getNameOfClass } from './utils' ;
79
810// -------------------------------------------------------------------------
911// Handy Exports
1012// -------------------------------------------------------------------------
1113
1214export * from './importer' ;
15+ export { Factory , Seed } from './types' ;
16+ export { times } from './utils' ;
1317
1418// -------------------------------------------------------------------------
1519// Types & Variables
@@ -20,23 +24,32 @@ export * from './importer';
2024 entityFactories : new Map < string , EntityFactoryDefinition < any , any > > ( ) ,
2125} ;
2226
23- // -------------------------------------------------------------------------
24- // Util functions
25- // -------------------------------------------------------------------------
26-
2727// -------------------------------------------------------------------------
2828// Facade functions
2929// -------------------------------------------------------------------------
3030
31+ /**
32+ * Adds the typorm connection to the seed options
33+ */
3134export const setConnection = ( connection : Connection ) => ( global as any ) . seeder . connection = connection ;
3235
36+ /**
37+ * Returns the typorm connection from our seed options
38+ */
3339export const getConnection = ( ) => ( global as any ) . seeder . connection ;
3440
41+ /**
42+ * Defines a new entity factory
43+ */
3544export const define = < Entity , Settings > ( entity : ObjectType < Entity > , factoryFn : FactoryFunction < Entity , Settings > ) => {
3645 ( global as any ) . seeder . entityFactories . set ( getNameOfClass ( entity ) , { entity, factory : factoryFn } ) ;
3746} ;
3847
39- export const factory = < Entity , Settings > ( entity : any ) => ( settings ?: Settings ) => {
48+
49+ /**
50+ * Gets a defined entity factory and pass the settigns along to the entity factory function
51+ */
52+ export const factory = < Entity , Settings > ( entity : EntityConstructor < Entity > ) => ( settings ?: Settings ) => {
4053 const name = getNameOfClass ( entity ) ;
4154 const entityFactoryObject = ( global as any ) . seeder . entityFactories . get ( name ) ;
4255 return new EntityFactory < Entity , Settings > (
@@ -47,22 +60,10 @@ export const factory = <Entity, Settings>(entity: any) => (settings?: Settings)
4760 ) ;
4861} ;
4962
50- export const seed = async < Entity , Settings > ( entityFactory : EntityFactory < Entity , Settings > ) : Promise < Entity > => {
51- const connection : Connection = ( global as any ) . seeder . connection ;
52- if ( connection ) {
53- const em = connection . createEntityManager ( ) ;
54- try {
55- const entity = await entityFactory . make ( ) ;
56- return await em . save < Entity > ( entityFactory . entity , entity ) ;
57- } catch ( error ) {
58- throw new Error ( 'Could not save entity' ) ;
59- }
60- } else {
61- throw new Error ( 'No db connection is given' ) ;
62- }
63- } ;
64-
65- export const runSeeder = async < T > ( seederConstructor : SeedConstructor ) : Promise < T > => {
63+ /**
64+ * Runs a seed class
65+ */
66+ export const runSeed = async < T > ( seederConstructor : SeedConstructor ) : Promise < T > => {
6667 const seeder = new seederConstructor ( ) ;
6768 return seeder . seed ( factory , getConnection ( ) ) ;
6869} ;
0 commit comments