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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | 16x 350x 350x 350x 350x 791x 3090x 783x 3090x 652x 652x 652x 947x 490x 947x 394x 263x 10237x 67x 62x 27x 3x 11x 11x 60x 11x 60x 60x 119x 60x 11x 60x 11x 1x 1x 1x 1x 11x 11x 11x 11x 11x | import { EntityOptions, PropertyOptions, RelationshipOptions } from "./types";
/**
* MetadataRegistry is responsible for storing and retrieving schema metadata
* about entities, properties, and relationships.
*
* This class is designed to be instantiated directly rather than used as a singleton,
* allowing for better testing and composition. It's typically used with a SchemaBuilder
* to register metadata from decorated classes.
*/
export class MetadataRegistry {
private entities = new Map<Function, EntityOptions>();
private properties = new Map<Function, Map<string, PropertyOptions>>();
private ids = new Map<Function, Set<string>>();
private relationships = new Map<Function, Map<string, RelationshipOptions>>();
constructor() {}
// Entity registration
registerEntity(target: Function, options: EntityOptions = {}): void {
this.entities.set(target, options);
}
// Property registration
registerProperty(
target: Function,
propertyKey: string,
options: PropertyOptions = {}
): void {
if (!this.properties.has(target)) {
this.properties.set(target, new Map());
}
this.properties.get(target)!.set(propertyKey, options);
}
// ID registration
registerIdProperty(target: Function, propertyKey: string): void {
if (!this.ids.has(target)) {
this.ids.set(target, new Set());
}
this.ids.get(target)!.add(propertyKey);
}
// Relationship registration
registerRelationship(
target: Function,
propertyKey: string,
options: RelationshipOptions
): void {
if (!this.relationships.has(target)) {
this.relationships.set(target, new Map());
}
this.relationships.get(target)!.set(propertyKey, options);
}
// Getters
getEntityMetadata(target: Function): EntityOptions | undefined {
return this.entities.get(target);
}
getPropertyMetadata(
target: Function,
propertyKey: string
): PropertyOptions | undefined {
return this.properties.get(target)?.get(propertyKey);
}
getAllProperties(target: Function): Map<string, PropertyOptions> | undefined {
return this.properties.get(target);
}
getIdProperties(target: Function): Set<string> | undefined {
return this.ids.get(target);
}
getRelationshipMetadata(
target: Function,
propertyKey: string
): RelationshipOptions | undefined {
return this.relationships.get(target)?.get(propertyKey);
}
getAllRelationships(
target: Function
): Map<string, RelationshipOptions> | undefined {
return this.relationships.get(target);
}
getAllEntities(): Map<Function, EntityOptions> {
return this.entities;
}
/**
* Create a clone of this registry
* Useful for creating modified copies without changing the original
*/
clone(): MetadataRegistry {
const cloned = new MetadataRegistry();
// Clone entities
this.entities.forEach((options, target) => {
cloned.entities.set(target, { ...options });
});
// Clone properties
this.properties.forEach((propertyMap, target) => {
const clonedPropertyMap = new Map<string, PropertyOptions>();
propertyMap.forEach((options, key) => {
clonedPropertyMap.set(key, { ...options });
});
cloned.properties.set(target, clonedPropertyMap);
});
// Clone IDs
this.ids.forEach((idSet, target) => {
cloned.ids.set(target, new Set<string>(idSet));
});
// Clone relationships
this.relationships.forEach((relationshipMap, target) => {
const clonedRelationshipMap = new Map<string, RelationshipOptions>();
relationshipMap.forEach((options, key) => {
clonedRelationshipMap.set(key, { ...options });
});
cloned.relationships.set(target, clonedRelationshipMap);
});
return cloned;
}
/**
* Clear all metadata from this registry
*/
clear(): void {
this.entities.clear();
this.properties.clear();
this.ids.clear();
this.relationships.clear();
}
}
|