File

src/app/core/entity/database-entity.decorator.ts

Extends

Registry

Index

Methods

Methods

getEntityTypes
getEntityTypes(onlyUserFacing)

Get an array of entity types, optionally filtered to exclude internal, administrative types.

Parameters :
Name Optional Default value Description
onlyUserFacing No false

Whether to only include types that are explicitly defined and customized in the config, from which we infer they are user-facing.

Returns : literal type[]
Public add
add(key: string, mapping: T)
Inherited from Registry
Defined in Registry:17
Parameters :
Name Type Optional
key string No
mapping T No
Returns : void
Public addAll
addAll(tuples: [])
Inherited from Registry
Defined in Registry:31
Parameters :
Name Type Optional
tuples [] No
Returns : void
Public allowDuplicates
allowDuplicates()
Inherited from Registry
Defined in Registry:49

Calling this will allow the same keys to be added multiple times without thrown errors. This is useful for storybook where live-updates re-trigger the decorator while the registry is cached.

Returns : void
Public get
get(key: string)
Inherited from Registry
Defined in Registry:35
Parameters :
Name Type Optional
key string No
Returns : T
import { Entity, EntityConstructor } from "./model/entity";
import { Registry } from "../config/registry/dynamic-registry";
import { getEntitySchema } from "./database-field.decorator";

export class EntityRegistry extends Registry<EntityConstructor> {
  /**
   * Get an array of entity types, optionally filtered to exclude internal, administrative types.
   * @param onlyUserFacing Whether to only include types that are explicitly defined and customized in the config, from which we infer they are user-facing.
   */
  getEntityTypes(
    onlyUserFacing = false,
  ): { key: string; value: EntityConstructor }[] {
    let entities = Array.from(this.entries()).map(([key, value]) => ({
      key,
      value,
    }));
    if (onlyUserFacing) {
      entities = entities.filter(
        ({ key, value }) => !value.isInternalEntity && value.label,
      );
    }
    return entities;
  }
}

export const entityRegistry = new EntityRegistry((key, constructor) => {
  if (!(new constructor() instanceof Entity)) {
    throw Error(
      `Tried to register an entity-type that is not a subclass of Entity\n` +
        `type: ${key}; constructor: ${constructor}`,
    );
  }
});

/**
 * Decorator (Annotation `@DatabaseEntity()`) to set the string ENTITY_TYPE to an Entity Type.
 * The entity should also be added to the {@link databaseEntities} array of the surrounding module.
 *
 * also see {@link /additional-documentation/how-to-guides/create-a-new-entity-type.html}
 *
 * @param entityType The string key for this Entity Type, used as id prefix.
 */
export function DatabaseEntity(entityType: string) {
  return (constructor) => {
    entityRegistry.add(entityType, constructor);
    constructor.ENTITY_TYPE = entityType;

    // append parent schema definitions
    const parentConstructor = Object.getPrototypeOf(constructor);
    const schema = getEntitySchema(constructor);
    parentConstructor.schema.forEach((value, key) => schema.set(key, value));
  };
}

results matching ""

    No results matching ""