import { UnitOfWork } from './UnitOfWork' ; import { EntityManager } from './EntityManager' ; import { EntityCtor , EntityInterface , ProxyInterface } from './EntityInterface' ; import { EntityRepository } from './EntityRepository' ; import { Mapping } from './Mapping' ; import { Store } from './Store' ; import { Wetland } from './Wetland' ; import { Hydrator } from './Hydrator' ; import { Homefront } from 'homefront' ; import { EntityProxy } from './EntityProxy' ; import { IdentityMap } from './IdentityMap' ; export class Scope { type { UnitOfWork } private readonly unitOfWork : UnitOfWork ; type { EntityManager } private manager : EntityManager ; type { Wetland } private wetland : Wetland ; type { IdentityMap } private identityMap : IdentityMap = new IdentityMap ; param { EntityManager } manager param { Wetland } wetland public constructor ( manager , wetland ) { this . manager = manager ; this . wetland = wetland ; this . unitOfWork = new UnitOfWork ( this ) ; } returns { IdentityMap } public getIdentityMap ( ) { return this . identityMap ; } param { string | Entity } entity returns { EntityRepository } public getRepository < T > ( entity ) { return this . manager . getRepository ( entity , this ) ; } returns { Homefront } public getConfig ( ) { return this . manager . getConfig ( ) ; } param { Entity } entity param { * } primaryKeyValue param { boolean } proxy returns { EntityInterface } public getReference ( entity , primaryKeyValue , proxy = true ) { const ReferenceClass = this . resolveEntityReference ( entity ) ; const fromMap = this . identityMap . fetch ( ReferenceClass , primaryKeyValue ) ; if ( fromMap ) { return fromMap ; } const reference = new ReferenceClass ; const primaryKey = Mapping . forEntity ( ReferenceClass ) . getPrimaryKey ( ) ; reference [ primaryKey ] = primaryKeyValue ; this . unitOfWork . registerClean ( reference ) ; if ( proxy ) { const proxied = this . attach ( reference ) ; this . identityMap . register ( ReferenceClass , proxied ) ; return proxied ; } return reference ; } param { EntityInterface | string | { } } hint returns { EntityInterface | null } public resolveEntityReference ( hint ) { return this . manager . resolveEntityReference ( hint ) ; } param { ... EntityInterface } entity returns { Promise < any > } public refresh ( ... entity ) { const refreshes = [ ] ; const hydrator = new Hydrator ( this ) ; if ( ! Array . isArray ( entity ) || ! entity . length ) { return Promise . resolve ( null ) ; } entity . forEach ( toRefresh => { const entityCtor = this . resolveEntityReference ( toRefresh ) ; const primaryKeyName = Mapping . forEntity ( entityCtor ) . getPrimaryKey ( ) ; const primaryKey = toRefresh [ primaryKeyName ] ; if ( ! primaryKey ) { return refreshes . push ( Promise . reject ( new Error ( 'Cannot refresh entity without a PK value.' ) ) ) ; } const refresh = this . getRepository ( entityCtor ) . getQueryBuilder ( ) . where ( { [ primaryKeyName ] : primaryKey } ) . limit ( 1 ) . getQuery ( ) . execute ( ) . then ( freshData => hydrator . fromSchema ( freshData [ 0 ] , toRefresh ) ) ; refreshes . push ( refresh ) ; } ) ; return Promise . all ( refreshes ) ; } param { EntityInterface | string | { } } entity returns { Mapping } public getMapping < T > ( entity ) { return this . manager . getMapping ( entity ) ; } param { string } name returns { Function } public getEntity ( name ) { return this . manager . getEntity ( name ) ; } param { EntityInterface } entity returns { Store } public getStore ( entity ? ) { return this . manager . getStore ( entity ) ; } returns { UnitOfWork } public getUnitOfWork ( ) { return this . unitOfWork ; } returns { { } } public getEntities ( ) { return this . manager . getEntities ( ) ; } param { EntityInterface } entity param { boolean } active returns { EntityInterface & ProxyInterface } public attach < T > ( entity , active = false ) { return EntityProxy . patchEntity ( entity , this , active ) ; } param { ProxyInterface } entity returns { EntityInterface } public detach ( entity ) { entity . deactivateProxying ( ) ; this . unitOfWork . clear ( entity ) ; return entity . getTarget ( ) ; } param { { } [ ] } entities returns { Scope } public persist ( ... entities ) { entities . forEach ( entity => this . unitOfWork . registerNew ( entity ) ) ; return this ; } param { { } } entity returns { Scope } public remove ( entity ) { this . unitOfWork . registerDeleted ( entity ) ; return this ; } param { boolean } skipClean param { boolean } skipLifecycleHooks param { refreshCreated public flush ( skipClean = false , skipLifecycleHooks = false , config = { refreshCreated : null , refreshUpdated : null } ) { return this . unitOfWork . commit ( skipClean , skipLifecycleHooks , config ) ; } returns { Scope } public clear ( ) { this . unitOfWork . clear ( ) ; return this ; } } export type Entity = string | { new ( ) ; } | EntityInterface ;