import { Injectable } from "@angular/core" ; import { Entity , ENTITY_CONFIG_PREFIX , EntityConstructor , } from "./model/entity" ; import { ConfigService } from "../config/config.service" ; import { EntitySchemaField } from "./schema/entity-schema-field" ; import { addPropertySchema } from "./database-field.decorator" ; import { OperationType } from "../permissions/entity-permissions.service" ; import { DynamicEntityService } from "./dynamic-entity.service" ; import { LoggingService } from "../logging/logging.service" ; /**
 * A service that allows to work with configuration-objects
 * related to entities such as assigning dynamic attributes
 * and their schemas to existing entities
 */ @ Injectable ( { providedIn : "root" , } ) export class EntityConfigService { static readonly PREFIX_ENTITY_CONFIG = "entity:" ; constructor ( private configService , private dynamicEntityService , private loggingService ) { } param entityType param configAttributes public addConfigAttributes < T extends Entity > ( entityType , configAttributes ? ) { const entityConfig = configAttributes || this . getEntityConfig ( entityType ) ; if ( entityConfig ?. attributes ) { entityConfig . attributes . forEach ( ( attribute ) => addPropertySchema ( entityType . prototype , attribute . name , attribute . schema ) ) ; } } param entityType public getEntityConfig ( entityType ) { const configName = EntityConfigService . PREFIX_ENTITY_CONFIG + entityType . ENTITY_TYPE ; return this . configService . getConfig < EntityConfig > ( configName ) ; } /**
     * Assigns additional schema-fields to all entities that are
     * defined inside the config. Entities that are not registered
     * using the  DatabaseEntity -Decorator won't work and will
     * trigger an error message
      setupEntitiesFromConfig ( ) { const entitiesNotFound = [ ] ; for ( const config of this . configService . getAllConfigs < EntityConfig & { _id : string ; } > ( ENTITY_CONFIG_PREFIX ) ) { const id = config . _id . substring ( ENTITY_CONFIG_PREFIX . length ) ; try { const ctor = this . dynamicEntityService . getEntityConstructor ( id ) ; this . addConfigAttributes ( ctor , config ) ; } catch ( err ) { entitiesNotFound . push ( id ) ; } } if ( entitiesNotFound . length > 0 ) { this . loggingService . error ( `The following entities were defined in the config but are not registered properly: ${ entitiesNotFound . join ( ", " ) }.\n` + `Make sure they exist as a class and are properly registered (see the how-to guides for more info on this topic)` ) ; } } } /**
 * Dynamic configuration for a entity.
 * This allows to change entity metadata based on the configuration.
 */ export interface EntityConfig { permissions ? : { [ key in OperationType ] ? : string [ ] ; } ; /**
     * A list of attributes that will be dynamically added/overwritten to the entity.
     */ attributes ? : { /**
         * The name of the attribute (class variable) to be added/overwritten.
         */ name : string ; /**
         * The (new) schema configuration for this attribute.
         */ schema : EntitySchemaField ; } [ ] ; }