48 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-08-31 20:51:14 -07:00
import { DatasetEntity } from '@datahub/data-models/entity/dataset/dataset-entity';
import { PersonEntity } from '@datahub/data-models/entity/person/person-entity';
2019-08-31 20:51:14 -07:00
/**
* Defines the interface for the DataModelEntity enum below.
* This allows each entry in the enum to be indexable
*/
export interface IDataModelEntity {
2019-08-31 20:51:14 -07:00
[DatasetEntity.displayName]: typeof DatasetEntity;
[PersonEntity.displayName]: typeof PersonEntity;
2019-08-31 20:51:14 -07:00
}
/**
* Enumeration of data model entity displayName values to the related Data Model entity
* Serves as the primary resource map of all DataModelEntity available classes
*/
export const DataModelEntity: IDataModelEntity = {
[DatasetEntity.displayName]: DatasetEntity,
[PersonEntity.displayName]: PersonEntity
2019-08-31 20:51:14 -07:00
};
/**
* Aliases the keys on the DataModelEntity enum for reference convenience
* This maps to the names of the available entities for example 'datasets', 'users', etc
*/
export type DataModelName = keyof typeof DataModelEntity;
/**
* Aliases the DataModelEntity classes found in the DataModelEntity enum, this is a union type of all entity classifiers
* For example { typeof DatasetEntity | typeof UserEntity | ... }
2019-08-31 20:51:14 -07:00
*/
export type DataModelEntity = typeof DataModelEntity[DataModelName];
/**
* A specific instance of data model entity
* For example { DatasetEntity | UserEntity | ... }
*/
export type DataModelEntityInstance = InstanceType<DataModelEntity>;
2019-08-31 20:51:14 -07:00
/**
* Guards on a string entityName if it maps to an entity data model (class inheriting from BaseEntity)
* @param {string} entityName the displayName to match against for DataModelEntity types
*/
export const isDataModelBaseEntityName = (entityName: DataModelName): boolean =>
Object.values(DataModelEntity)
.mapBy('displayName')
.includes(entityName);