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

100 lines (99 loc) 3.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.schemas = exports.SchemaManager = void 0; const js_yaml_1 = __importDefault(require("js-yaml")); const normalizr_1 = require("normalizr"); class SchemaManager { constructor() { } static instance; schemas = {}; relationshipMap = {}; static get schemas() { return this.getInstance().getSchemas(); } static get relationshipMap() { return this.getInstance().getRelationshipMap(); } static getInstance() { if (!this.instance) { this.instance = new SchemaManager(); } return this.instance; } setSchemas(schemaConfig) { this.schemas = this.createSchemas(schemaConfig); return this.schemas; } getSchemas() { return this.schemas; } getSchema(schemaName) { if (!this.schemas[schemaName]) { throw new Error(`Schema ${schemaName} not defined!`); } return this.schemas[schemaName]; } createSchemas(schemaConfig) { const schemas = {}; this.relationshipMap = {}; for (const [name, definition] of Object.entries(schemaConfig)) { const { idAttribute } = definition; schemas[name] = new normalizr_1.schema.Entity(name, {}, { idAttribute: typeof idAttribute === 'string' || typeof idAttribute === 'function' ? idAttribute : 'id' }); this.relationshipMap[name] = { _referencedBy: {} }; } for (const [name, definition] of Object.entries(schemaConfig)) { const { idAttribute, ...relationships } = definition; const entitySchema = schemas[name]; Object.entries(relationships).forEach(([relationKey, relationValue]) => { let relatedEntityName; let isMany = false; if (typeof relationValue === 'string') { relatedEntityName = relationValue; } else if (Array.isArray(relationValue)) { relatedEntityName = relationValue[0]; isMany = true; } else { return; } if (isMany) { entitySchema.define({ [relationKey]: [schemas[relatedEntityName]] }); } else { entitySchema.define({ [relationKey]: schemas[relatedEntityName] }); } if (!this.relationshipMap[name][relationKey]) { this.relationshipMap[name][relationKey] = { relatedEntityName, isMany }; } if (!this.relationshipMap[relatedEntityName]._referencedBy[name]) { this.relationshipMap[relatedEntityName]._referencedBy[name] = { fieldName: relationKey, isMany }; } }); } return schemas; } static parseYAML(yamlSchema) { const schemaManager = SchemaManager.getInstance(); const schemaConfig = js_yaml_1.default.load(yamlSchema); schemaManager.setSchemas(schemaConfig); } getRelationshipMap() { return this.relationshipMap; } } exports.SchemaManager = SchemaManager; exports.schemas = SchemaManager.schemas;