import { Mapping } from './Mapping' ; import { Wetland } from './Wetland' ; import { Entity , Scope } from './Scope' ; import { EntityCtor , EntityInterface } from './EntityInterface' ; import { Homefront } from 'homefront' ; import { EntityRepository } from './EntityRepository' ; import { Store } from './Store' ; /**
 * The main entity manager for wetland.
 * This distributes scopes and supplies some core methods.
 */ export class EntityManager { type { Wetland } private readonly wetland : Wetland = null ; type { { } } private entities : { [ key ] : { entity : EntityCtor < EntityInterface > ; mapping : Mapping < EntityInterface > ; } ; } = { } ; type { Map } private repositories : Map < EntityCtor < EntityInterface > , EntityRepository < any > > = new Map ( ) ; constructor param { Wetland } wetland public constructor ( wetland ) { this . wetland = wetland ; } returns { Homefront } public getConfig ( ) { return this . wetland . getConfig ( ) ; } returns { Scope } public createScope ( ) { return new Scope ( this , this . wetland ) ; } param { string } name returns { Function } public getEntity ( name ) { const entity = this . entities [ name ] ; if ( ! entity ) { throw new Error ( `No entity found for "${ name }".` ) ; } return entity . entity ; } param { string | Entity } entity param { Scope } scope returns { EntityRepository } public getRepository < T > ( entity , scope ? ) { const entityReference = this . resolveEntityReference ( entity ) as EntityCtor < T > ; if ( ! this . repositories . has ( entityReference ) || scope ) { const entityMapping = Mapping . forEntity ( entityReference ) ; const Repository = entityMapping . getRepository ( ) ; if ( ! Repository ) { throw new Error ( [ `Unable to find Repository for entity "${ entityMapping . getEntityName ( ) || entityReference . name }".` , `Did you forget to register your entity or set entityPath(s)?` , ] . join ( ' ' ) ) ; } if ( scope ) { return new Repository ( scope , entityReference ) ; } this . repositories . set ( entityReference , new Repository ( this , entityReference ) ) ; } return this . repositories . get ( entityReference ) as EntityRepository < T > ; } param { EntityInterface } entity returns { Store } public getStore ( entity ? ) { return this . wetland . getStore ( this . getStoreName ( entity ) ) ; } returns { { } } public getEntities ( ) { return this . entities ; } param { EntityInterface } entity returns { EntityManager } public registerEntity < T > ( entity ) { const mapping = this . getMapping ( entity ) . setEntityManager ( this ) ; if ( typeof entity . setMapping === 'function' ) { entity . setMapping ( mapping ) ; } this . entities [ mapping . getEntityName ( ) ] = { entity , mapping } ; return this ; } param { EntityInterface | string | { } } entity returns { Mapping } public getMapping < T > ( entity ) { return Mapping . forEntity ( this . resolveEntityReference ( entity ) ) as Mapping < T > ; } param { EntityInterface [ ] } entities returns { EntityManager } public registerEntities ( entities ) { entities . forEach ( entity => { this . registerEntity ( entity ) ; } ) ; return this ; } param { EntityInterface | string | { } } hint returns { EntityInterface | null } public resolveEntityReference ( hint ) { if ( typeof hint === 'string' ) { return this . getEntity ( hint ) ; } if ( typeof hint === 'object' ) { return hint as EntityCtor < EntityInterface > ; } return typeof hint === 'function' ? hint as EntityCtor < EntityInterface > : null ; } private getStoreName ( entity ? ) { if ( typeof entity === 'string' ) { return entity ; } if ( entity ) { return this . getMapping ( entity ) . getStoreName ( ) ; } return null ; } }