All files / src/entity EntityRegistry.ts

100% Statements 20/20
83.33% Branches 10/12
100% Functions 3/3
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55            1x   1x   1x             1x 8x     14x 14x 4x   4x 4x 1x     3x       10x 3x         7x         7x   7x       8x      
/*
 * © Copyright 2022 HP Development Company, L.P.
 * SPDX-License-Identifier: MIT
 */
 
import { TypeValue } from '@davinci/reflector';
import { EntityDefinition } from './EntityDefinition';
import { EntityDefinitionJSONSchema } from './types';
import { di } from '../di';
 
const primitiveTypes = [String, Number, Boolean, Date] as unknown[];
 
/**
 * The EntityRegistry class stores all of the EntityDefinition objects and provides a way
 * to cache and retrieve the EntityDefinitionJSONSchema objects.
 */
@di.singleton()
export class EntityRegistry {
	private entityDefinitionMap = new Map<TypeValue, EntityDefinition>();
 
	public getEntityDefinitionJsonSchema(typeValue: TypeValue): EntityDefinitionJSONSchema {
		const isPrimitiveType = primitiveTypes.includes(typeValue);
		if (isPrimitiveType) {
			const type = typeValue as StringConstructor | NumberConstructor | BooleanConstructor | DateConstructor;
 
			Eif (primitiveTypes.includes(typeValue)) {
				if (typeValue === Date) {
					return { type: 'string', format: 'date-time' };
				}
 
				return { type: type.name.toLowerCase() } as EntityDefinitionJSONSchema;
			}
		}
 
		if (this.entityDefinitionMap.has(typeValue)) {
			return this.entityDefinitionMap
				.get(typeValue)
				?.getEntityDefinitionJsonSchema() as EntityDefinitionJSONSchema;
		}
 
		const entityDefinition = new EntityDefinition({
			type: typeValue,
			entityDefinitionsMapCache: this.entityDefinitionMap
		});
 
		this.entityDefinitionMap.set(typeValue, entityDefinition);
 
		return entityDefinition.getEntityDefinitionJsonSchema();
	}
 
	public getEntityDefinitionMap() {
		return this.entityDefinitionMap;
	}
}