UNPKG

chrono-forge

Version:

A comprehensive framework for building resilient Temporal workflows, advanced state management, and real-time streaming activities in TypeScript. Designed for a seamless developer experience with powerful abstractions, dynamic orchestration, and full cont

149 lines (148 loc) 5.37 kB
import { schema, Schema } from 'normalizr'; /** * Represents a relationship between schemas with ID attribute and key. * This type extends the base Schema with additional relationship metadata. */ export type SchemaRelationship = { _idAttribute: string; _key: string; idAttribute: string | ((entity: any, parent?: any, key?: string) => any); }; /** * Enhanced Entity schema that extends normalizr's Entity with additional schema definition and ID attribute. * Provides type safety for schema relationships and custom ID attribute functions. */ export interface EnhancedEntity extends schema.Entity { schema: { [key: string]: (Schema & SchemaRelationship) | [Schema & SchemaRelationship]; }; idAttribute: string | ((entity: any, parent?: any, key?: string) => any); } /** * Definition of all schemas with their names as keys. * Used as the primary configuration input for the SchemaManager. */ export type SchemasDefinition = { [schemaName: string]: SchemaDefinition; }; /** * Definition of a single schema with optional ID attribute and relationships. * Each field represents either a single or array relationship to another entity. */ export type SchemaDefinition = { idAttribute?: string | ((entity: any, parent?: any, key?: string) => any); } & Record<string, string | [string]>; /** * Represents a relationship between entities. * Defines whether the relationship is to one (single) or many (collection) entities. */ export type Relationship = { relatedEntityName: string; isMany: boolean; }; /** * Represents how an entity is referenced by another entity. * Contains the field name in the referencing entity and whether it's a collection. */ export type ReferencedBy = { fieldName: string; isMany: boolean; }; /** * Map of entities that reference this entity. * Keys are the names of referencing entities, values describe how they reference this entity. */ export type ReferencedByMap = { [referencingEntityName: string]: ReferencedBy; }; /** * Represents all relationships for an entity. * Includes both outgoing relationships and incoming references (_referencedBy). */ export type EntityRelationships = { [fieldName: string]: Relationship | undefined; } & { _referencedBy: ReferencedByMap; }; /** * Map of all entities and their relationships. * Provides a complete picture of the data model's relational structure. */ export type RelationshipMap = { [entityName: string]: EntityRelationships; }; /** * Manages schema definitions and relationships between entities. * Implements the Singleton pattern to ensure a single source of truth for schemas. * Provides methods to create, retrieve, and analyze entity relationships. */ export declare class SchemaManager { private constructor(); private static instance; private schemas; private relationshipMap; /** * Gets all registered schemas. * @returns Record of all schema entities indexed by name. */ static get schemas(): Record<string, EnhancedEntity>; /** * Gets the relationship map between all entities. * @returns RelationshipMap containing all entity relationships. */ static get relationshipMap(): RelationshipMap; /** * Gets the singleton instance of SchemaManager. * Creates the instance if it doesn't exist yet. * @returns The singleton SchemaManager instance. */ static getInstance(): SchemaManager; /** * Sets schemas based on the provided configuration. * Creates schema entities and builds the relationship map. * @param schemaConfig The schema configuration to set. * @returns The created schema entities. */ setSchemas(schemaConfig: SchemasDefinition): Record<string, EnhancedEntity>; /** * Retrieves all registered schemas. * @returns The current schema entities. */ getSchemas(): Record<string, EnhancedEntity>; /** * Retrieves a specific schema by name. * @param schemaName The name of the schema to retrieve. * @returns The requested schema entity. * @throws Error if the schema is not defined. */ getSchema(schemaName: string): EnhancedEntity; /** * Creates schemas dynamically based on the configuration provided. * Performs a two-pass process: * 1. Creates schema entities without relationships * 2. Defines relationships between entities and builds the relationship map * * @param schemaConfig The schema configuration. * @returns The created schema entities. */ private createSchemas; /** * Parses schemas from a YAML string and sets them in the SchemaManager. * Convenience method for initializing schemas from YAML configuration. * * @param yamlSchema The YAML string containing schema definitions. */ static parseYAML(yamlSchema: string): void; /** * Gets the relationship map for all entities. * The relationship map describes both outgoing and incoming relationships. * * @returns RelationshipMap containing all entity relationships. */ getRelationshipMap(): RelationshipMap; } /** * Exports the schemas from SchemaManager. * Provides a convenient way to access the schemas without directly using the SchemaManager. */ export declare const schemas: Record<string, EnhancedEntity>;