import fg from 'fast-glob' ; import path from 'path' ; import { DuplicateEntityPhysicalNameError , EntityTarget , MissingRequiredEntityPhysicalNameError , } from '@typedorm/common' ; import { Connection } from './connection' ; import { EntityMetadataBuilder } from './entity-metadata-builder' ; import { MetadataManager } from '@typedorm/common' ; import { EntityMetadata } from '../metadata/entity-metadata' ; export class ConnectionMetadataBuilder { constructor ( private connection ) { } buildEntityMetadatas ( entities ) { let possibleEntitiesToBuild = [ ] as Function [ ] ; if ( typeof entities === 'string' ) { possibleEntitiesToBuild = [ ... this . loadEntitiesFromDirs ( entities ) ] ; } else { possibleEntitiesToBuild = [ ... entities ] ; } const entitiesToBuild = possibleEntitiesToBuild . filter ( entity => MetadataManager . metadataStorage . hasKnownEntity ( entity ) ) ; const entityMetadatas = new EntityMetadataBuilder ( this . connection ) . build ( entitiesToBuild ) ; this . sanitizeDuplicates ( entityMetadatas ) ; return entityMetadatas ; } private loadEntitiesFromDirs ( pathMatchPattern ) { const classesDirectory = path . normalize ( pathMatchPattern ) ; const allFiles = fg . sync ( classesDirectory , { dot : false , } ) . map ( file => require ( file ) ) ; return this . recursiveLoadModulesFromFiles ( allFiles , [ ] ) ; } private recursiveLoadModulesFromFiles ( exported , allLoaded ) { if ( typeof exported === 'function' ) { allLoaded . push ( exported ) ; } else if ( Array . isArray ( exported ) ) { exported . forEach ( ( file ) => this . recursiveLoadModulesFromFiles ( file , allLoaded ) ) ; } else if ( typeof exported === 'object' && exported !== null ) { Object . keys ( exported ) . forEach ( key => this . recursiveLoadModulesFromFiles ( exported [ key ] , allLoaded ) ) ; } return allLoaded ; } private sanitizeDuplicates ( metadata ) { const cache = [ ] ; metadata . forEach ( metadata => { if ( ! metadata . name ) { throw new MissingRequiredEntityPhysicalNameError ( metadata . target . name ) ; } if ( cache . includes ( metadata . name ) ) { throw new DuplicateEntityPhysicalNameError ( metadata . name ) ; } else { cache . push ( metadata . name ) ; } } ) ; } }