import 'reflect-metadata' ; import { ObjectType , getConnection , Connection } from 'typeorm' ; import { EntityFactory } from './entity-factory' ; import { EntityFactoryDefinition , Factory , FactoryFunction , SeederConstructor , Seeder } from './types' ; import { getNameOfEntity } from './utils/factory.util' ; import { loadFiles , importFiles } from './utils/file.util' ; import { ConfigureOption , configureConnection , getConnectionOptions , createConnection } from './connection' ; export * from './importer' ; export * from './connection' ; export { Factory , Seeder } from './types' ; export { times } from './helpers' ; ( global as any ) . seeder = { entityFactories : new Map < string , EntityFactoryDefinition < any , any > > ( ) , } ; export const define = < Entity , Context > ( entity , factoryFn ) => { ; ( global as any ) . seeder . entityFactories . set ( getNameOfEntity ( entity ) , { entity , factory : factoryFn , } ) ; } ; export const factory = < Entity , Context > ( entity ) => ( context ? ) => { const name = getNameOfEntity ( entity ) ; const entityFactoryObject = ( global as any ) . seeder . entityFactories . get ( name ) ; return new EntityFactory < Entity , Context > ( name , entity , entityFactoryObject . factory , context ) ; } ; export const runSeeder = async ( clazz ) : Promise < any > => { const seeder = new clazz ( ) ; const connection = await createConnection ( ) ; return seeder . run ( factory , connection ) ; } ; export const useRefreshDatabase = async ( options = { } ) : Promise < Connection > => { configureConnection ( options ) ; const option = await getConnectionOptions ( ) ; const connection = await createConnection ( option ) ; if ( connection && connection . isConnected ) { await connection . dropDatabase ( ) ; await connection . synchronize ( ) ; } return connection ; } ; export const tearDownDatabase = async ( ) : Promise < void > => { const connection = await createConnection ( ) ; return connection && connection . isConnected ? connection . close ( ) : undefined ; } ; export const useSeeding = async ( options = { } ) : Promise < void > => { configureConnection ( options ) ; const option = await getConnectionOptions ( ) ; const factoryFiles = loadFiles ( option . factories ) ; await importFiles ( factoryFiles ) ; } ;