@maykonpaulo/maestro-core 0.5.0 → 0.7.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1040,6 +1040,38 @@ declare function detectDisplayField(fields: Array<{
1040
1040
  isPrimaryKey: boolean;
1041
1041
  }>): string | undefined;
1042
1042
 
1043
+ /**
1044
+ * Public structural validator for the `IntrospectionResult` contract. Accepts unknown/untrusted input
1045
+ * (e.g. a parsed JSON file) and checks it against the required (non-optional) fields of
1046
+ * `IntrospectionResult`/`EntityIntrospectionSchema`/`FieldIntrospectionSchema`/`RelationIntrospectionSchema`.
1047
+ * Optional derived fields (`isUnique`, `candidateForSearch`, `isTimestamp`, …) are not validated —
1048
+ * they pass through untouched when present.
1049
+ */
1050
+ interface IntrospectionValidationIssue {
1051
+ /** Logical path of the offending field, e.g. `entities[0].fields[1].nullable`. */
1052
+ path: string;
1053
+ /** Human-readable reason, including expected vs. received type when applicable. */
1054
+ message: string;
1055
+ }
1056
+ interface IntrospectionValidationResult {
1057
+ valid: boolean;
1058
+ issues: IntrospectionValidationIssue[];
1059
+ }
1060
+ /**
1061
+ * Validates that an unknown value matches the `IntrospectionResult` contract closely enough to be
1062
+ * safely consumed downstream. Accumulates every issue found (rather than failing fast) so callers can
1063
+ * surface a complete, readable list of problems in one pass.
1064
+ */
1065
+ declare function validateIntrospectionResult(value: unknown): IntrospectionValidationResult;
1066
+
1067
+ /**
1068
+ * Normalizes a valid `IntrospectionResult` into a deterministic, canonical form: entities sorted by
1069
+ * `table`, relations sorted by `id`. The internal order of `fields`/`indices` is preserved, since column
1070
+ * order is operationally meaningful. Never adds volatile data (timestamps, absolute paths, hashes) —
1071
+ * the same input always normalizes to the same output.
1072
+ */
1073
+ declare function normalizeIntrospectionResult(result: IntrospectionResult): IntrospectionResult;
1074
+
1043
1075
  interface GeneratedConfig {
1044
1076
  entities: Record<string, EntitySchema>;
1045
1077
  relations: Record<string, RelationSchema>;
@@ -1382,6 +1414,47 @@ declare function loadDeclarativeConfigFromString(content: string, options: LoadD
1382
1414
  /** Reads, parses and validates a declarative config from a file path. Format is detected from the extension unless overridden. */
1383
1415
  declare function loadDeclarativeConfigFromFile(filePath: string, options?: LoadDeclarativeConfigFromFileOptions): Promise<DeclarativeFileConfig>;
1384
1416
 
1417
+ interface DeclarativeGeneratorOptions {
1418
+ /** Adds one minimal list/detail consumer per entity, derived from visible fields. Default: false. */
1419
+ includeConsumers?: boolean;
1420
+ /** Includes fields flagged as internal (hidden in metadata, or hidden: true in schema). Default: true. */
1421
+ includeInternalFields?: boolean;
1422
+ /** Sorts entities alphabetically by id and fields alphabetically by name for deterministic output. Default: true. */
1423
+ sort?: boolean;
1424
+ }
1425
+ /** Metadata-shaped input. Structurally compatible with `MaestroMetadata`, so it can be passed directly. */
1426
+ interface DeclarativeGeneratorMetadataInput {
1427
+ entities: EntityMetadata[];
1428
+ relations?: RelationMetadata[];
1429
+ operations?: OperationMetadata[];
1430
+ }
1431
+ /** Schema-shaped input, mirroring `MaestroConfig.entities` / `MaestroConfig.relations`. */
1432
+ interface DeclarativeGeneratorSchemaInput {
1433
+ entities: EntitySchema[];
1434
+ relations?: RelationSchema[];
1435
+ }
1436
+ /** Generates a `DeclarativeFileConfig` from already-built `EntityMetadata[]` (+ optional relations/operations). */
1437
+ declare function generateDeclarativeConfigFromMetadata(input: DeclarativeGeneratorMetadataInput, options?: DeclarativeGeneratorOptions): DeclarativeFileConfig;
1438
+ /** Generates a `DeclarativeFileConfig` from raw `EntitySchema[]` (+ optional relations), before metadata normalization. */
1439
+ declare function generateDeclarativeConfigFromSchema(input: DeclarativeGeneratorSchemaInput, options?: DeclarativeGeneratorOptions): DeclarativeFileConfig;
1440
+
1441
+ interface YamlSerializer {
1442
+ stringify(value: unknown): string;
1443
+ }
1444
+
1445
+ type DeclarativeSerializationFormat = 'yaml' | 'json';
1446
+ interface SerializeDeclarativeConfigOptions {
1447
+ format: DeclarativeSerializationFormat;
1448
+ /** Required for `format: 'yaml'`. The core has no YAML runtime dependency — pass a serializer (e.g. `{ stringify: YAML.stringify }` from the `yaml` package). */
1449
+ yamlSerializer?: YamlSerializer;
1450
+ }
1451
+ /** Serializes a declarative config to formatted, stable JSON. */
1452
+ declare function serializeDeclarativeConfigToJson(config: DeclarativeFileConfig): string;
1453
+ /** Serializes a declarative config to YAML using an injected serializer (the core has no bundled YAML dependency). */
1454
+ declare function serializeDeclarativeConfigToYaml(config: DeclarativeFileConfig, yamlSerializer: YamlSerializer): string;
1455
+ /** Serializes a declarative config to the requested textual format. */
1456
+ declare function serializeDeclarativeConfig(config: DeclarativeFileConfig, options: SerializeDeclarativeConfigOptions): string;
1457
+
1385
1458
  declare const GOVERNANCE_EVENT_TYPES: {
1386
1459
  readonly OPERATION_EXECUTED: "governance.operation.executed";
1387
1460
  readonly AUTHORIZATION_DENIED: "governance.authorization.denied";
@@ -1455,4 +1528,4 @@ declare class DefaultGovernanceApi implements GovernanceApi {
1455
1528
  getPolicyViolations(filter?: PolicyViolationFilter): Promise<PolicyViolation[]>;
1456
1529
  }
1457
1530
 
1458
- export { type Actor, type ActorType, type AuditEvent, type AuditFilter, type AuditLevel, type AuditRecordedPayload, AuditRecorder, type AuditRepository, type AuditTimeline, type AuthorizationContext, type AuthorizationDecision, type AuthorizationDeniedPayload, type AuthorizationProvider, type AuthorizationResult, type CompileDeclarationsOptions, type ConfigProvider, type ConfirmationApproval, type ConfirmationApprovedPayload, type ConfirmationDeclaration, ConfirmationEngine, type ConfirmationRejectedPayload, type ConfirmationRepository, type ConfirmationRequest, type ConfirmationRequestedPayload, type ConfirmationStatus, ConsoleLogger, ConsoleStructuredLogger, type ConsumerActionsConfig, type ConsumerDeclaration, type ConsumerDetailConfig, type ConsumerFormConfig, type ConsumerFormsConfig, type ConsumerListConfig, type ConsumerProjectionFields, type ConsumerProjectionOperations, type ContextAction, type ContextActionCondition, type ContextActionStyle, ContextualAuthorizationEngine, type CorrelationContext, type CorrelationId, type CreateMaestroFromIntrospectionOptions, CsvExportProvider, type CursorPagination, DEFAULT_CAPABILITIES, type DatasourceDeleteContext, type DatasourceFindContext, type DatasourceProvider, type DatasourceQueryContext, DatasourceRegistry, type DatasourceUpdateContext, type DatasourceWriteContext, type DeclarativeCompilationResult, type DeclarativeConfig, type DeclarativeFileConfig, type DeclarativeFileFormat, DefaultGovernanceApi, type DiffChange, type DiffChangeKind, DiffEngine, type DiffEngineOptions, type DiffSummary, type DomainEvent, type EntityCapabilities, type EntityDeclaration, type EntityDiffChange, type EntityDiffChangeKind, type EntityExportConfig, type EntityIntrospectionSchema, type EntityLabelConfig, type EntityMetadata, type EntitySchema, type EntitySourceConfig, type EnumOption, type EnumOptionDeclaration, ErrorCode, type EventBus, type EventHandler, type ExportConfig, type ExportFormat, type ExportOptions, type ExportProvider, type ExportResult, type FeatureFlag, type FeatureFlagProvider, type FieldDeclaration, type FieldDetailConfig, type FieldDiffChange, type FieldDiffChangeKind, type FieldFormConfig, type FieldIntrospectionSchema, type FieldListConfig, type FieldMetadata, type FieldSchema, type FieldSchemaDetailConfig, type FieldSchemaEnumOption, type FieldSchemaFormConfig, type FieldSchemaListConfig, type FieldType, type FileSystemReader, type FilterDescriptor, type FilterOperator, GOVERNANCE_EVENT_TYPES, type GeneratedConfig, type GovernanceApi, type GovernanceEventBus, type GovernanceEventType, type ImpactLevel, InMemoryAuditRepository, InMemoryConfigProvider, InMemoryConfirmationRepository, InMemoryDatasourceProvider, InMemoryEventBus, InMemoryFeatureFlagProvider, InMemoryGovernanceEventBus, InMemoryPolicyProvider, InMemorySnapshotRepository, type IndexIntrospectionSchema, type IntrospectionDiff, type IntrospectionProvider, type IntrospectionReport, type IntrospectionReportChange, type IntrospectionReportStats, type IntrospectionResult, IntrospectionRuntime, type IntrospectionRuntimeResult, type IntrospectionRuntimeRunOptions, type IntrospectionSnapshot, type ListResult, type LoadDeclarativeConfigFromFileOptions, type LoadDeclarativeConfigFromStringOptions, type LoadedConfig, type LogEntry, type LogLevel, type Logger, type MaestroActorResolver, type MaestroConfig, MaestroEngine, MaestroError, type MaestroFileLoaderOptions, type MaestroHttpHandler, type MaestroHttpHandlers, type MaestroHttpOptions, type MaestroHttpRequest, type MaestroHttpResponse, type MaestroMetadata, type MaestroRequestContext, type MergeIntrospectionOptions, type MergeStrategy, type Metadata, MetadataEngine, MetadataRiskClassifier, type MetadataValue, type OffsetPagination, type OperationContext, type OperationDeclaration, type OperationDef, type OperationExecutedPayload, type OperationLogStatus, type OperationMetadata, OperationRegistry, type OperationResult, type OperationScope, type OperationalRisk, type PagePagination, type PaginationInput, type Permission, type PolicyContext, type PolicyDecision, PolicyEngine, type PolicyEvaluationResult, type PolicyProvider, type PolicyRule, type PolicyRuleResult, type PolicyTriggeredPayload, type PolicyViolation, type PolicyViolationFilter, type QueryInput, RbacEngine, type RbacPolicy, type RecordAuditInput, type RelationDeclaration, type RelationDiffChange, type RelationDiffChangeKind, type RelationDisplayConfig, type RelationEndpoint, type RelationIntrospectionSchema, type RelationMetadata, type RelationSchema, type RelationType, ReportGenerator, type RequestConfirmationInput, type ResolvedConsumerProjection, type ResourceRef, type RiskClassificationInput, type RiskClassifier, type Role, type SchemaValidationError, type SchemaValidationResult, type SearchConfig, type SearchInput, type SnapshotRepository, type SoftDeleteConfig, type SortDescriptor, type SortDirection, type StructuredLogEntry, type StructuredLogger, type YamlParser, compileDeclarations, createMaestro, createMaestroFromIntrospection, createMaestroHttpHandlers, detectDisplayField, generateAllConfigs, generateCorrelationId, generateEntityConfig, generateRelationConfig, humanizeFieldName, inferFieldType, isSearchCandidate, isSoftDeleteCandidate, isTimestampField, loadDeclarativeConfigFromFile, loadDeclarativeConfigFromString, loadMaestroConfig, mergeIntrospectionWithOverrides, parseQueryInput, resolveConsumerProjections, tableNameToEntityId, tableNameToLabel, validateConsumerDeclaration, validateEntityDeclaration, validateMaestroConfig };
1531
+ export { type Actor, type ActorType, type AuditEvent, type AuditFilter, type AuditLevel, type AuditRecordedPayload, AuditRecorder, type AuditRepository, type AuditTimeline, type AuthorizationContext, type AuthorizationDecision, type AuthorizationDeniedPayload, type AuthorizationProvider, type AuthorizationResult, type CompileDeclarationsOptions, type ConfigProvider, type ConfirmationApproval, type ConfirmationApprovedPayload, type ConfirmationDeclaration, ConfirmationEngine, type ConfirmationRejectedPayload, type ConfirmationRepository, type ConfirmationRequest, type ConfirmationRequestedPayload, type ConfirmationStatus, ConsoleLogger, ConsoleStructuredLogger, type ConsumerActionsConfig, type ConsumerDeclaration, type ConsumerDetailConfig, type ConsumerFormConfig, type ConsumerFormsConfig, type ConsumerListConfig, type ConsumerProjectionFields, type ConsumerProjectionOperations, type ContextAction, type ContextActionCondition, type ContextActionStyle, ContextualAuthorizationEngine, type CorrelationContext, type CorrelationId, type CreateMaestroFromIntrospectionOptions, CsvExportProvider, type CursorPagination, DEFAULT_CAPABILITIES, type DatasourceDeleteContext, type DatasourceFindContext, type DatasourceProvider, type DatasourceQueryContext, DatasourceRegistry, type DatasourceUpdateContext, type DatasourceWriteContext, type DeclarativeCompilationResult, type DeclarativeConfig, type DeclarativeFileConfig, type DeclarativeFileFormat, type DeclarativeGeneratorMetadataInput, type DeclarativeGeneratorOptions, type DeclarativeGeneratorSchemaInput, type DeclarativeSerializationFormat, DefaultGovernanceApi, type DiffChange, type DiffChangeKind, DiffEngine, type DiffEngineOptions, type DiffSummary, type DomainEvent, type EntityCapabilities, type EntityDeclaration, type EntityDiffChange, type EntityDiffChangeKind, type EntityExportConfig, type EntityIntrospectionSchema, type EntityLabelConfig, type EntityMetadata, type EntitySchema, type EntitySourceConfig, type EnumOption, type EnumOptionDeclaration, ErrorCode, type EventBus, type EventHandler, type ExportConfig, type ExportFormat, type ExportOptions, type ExportProvider, type ExportResult, type FeatureFlag, type FeatureFlagProvider, type FieldDeclaration, type FieldDetailConfig, type FieldDiffChange, type FieldDiffChangeKind, type FieldFormConfig, type FieldIntrospectionSchema, type FieldListConfig, type FieldMetadata, type FieldSchema, type FieldSchemaDetailConfig, type FieldSchemaEnumOption, type FieldSchemaFormConfig, type FieldSchemaListConfig, type FieldType, type FileSystemReader, type FilterDescriptor, type FilterOperator, GOVERNANCE_EVENT_TYPES, type GeneratedConfig, type GovernanceApi, type GovernanceEventBus, type GovernanceEventType, type ImpactLevel, InMemoryAuditRepository, InMemoryConfigProvider, InMemoryConfirmationRepository, InMemoryDatasourceProvider, InMemoryEventBus, InMemoryFeatureFlagProvider, InMemoryGovernanceEventBus, InMemoryPolicyProvider, InMemorySnapshotRepository, type IndexIntrospectionSchema, type IntrospectionDiff, type IntrospectionProvider, type IntrospectionReport, type IntrospectionReportChange, type IntrospectionReportStats, type IntrospectionResult, IntrospectionRuntime, type IntrospectionRuntimeResult, type IntrospectionRuntimeRunOptions, type IntrospectionSnapshot, type IntrospectionValidationIssue, type IntrospectionValidationResult, type ListResult, type LoadDeclarativeConfigFromFileOptions, type LoadDeclarativeConfigFromStringOptions, type LoadedConfig, type LogEntry, type LogLevel, type Logger, type MaestroActorResolver, type MaestroConfig, MaestroEngine, MaestroError, type MaestroFileLoaderOptions, type MaestroHttpHandler, type MaestroHttpHandlers, type MaestroHttpOptions, type MaestroHttpRequest, type MaestroHttpResponse, type MaestroMetadata, type MaestroRequestContext, type MergeIntrospectionOptions, type MergeStrategy, type Metadata, MetadataEngine, MetadataRiskClassifier, type MetadataValue, type OffsetPagination, type OperationContext, type OperationDeclaration, type OperationDef, type OperationExecutedPayload, type OperationLogStatus, type OperationMetadata, OperationRegistry, type OperationResult, type OperationScope, type OperationalRisk, type PagePagination, type PaginationInput, type Permission, type PolicyContext, type PolicyDecision, PolicyEngine, type PolicyEvaluationResult, type PolicyProvider, type PolicyRule, type PolicyRuleResult, type PolicyTriggeredPayload, type PolicyViolation, type PolicyViolationFilter, type QueryInput, RbacEngine, type RbacPolicy, type RecordAuditInput, type RelationDeclaration, type RelationDiffChange, type RelationDiffChangeKind, type RelationDisplayConfig, type RelationEndpoint, type RelationIntrospectionSchema, type RelationMetadata, type RelationSchema, type RelationType, ReportGenerator, type RequestConfirmationInput, type ResolvedConsumerProjection, type ResourceRef, type RiskClassificationInput, type RiskClassifier, type Role, type SchemaValidationError, type SchemaValidationResult, type SearchConfig, type SearchInput, type SerializeDeclarativeConfigOptions, type SnapshotRepository, type SoftDeleteConfig, type SortDescriptor, type SortDirection, type StructuredLogEntry, type StructuredLogger, type YamlParser, type YamlSerializer, compileDeclarations, createMaestro, createMaestroFromIntrospection, createMaestroHttpHandlers, detectDisplayField, generateAllConfigs, generateCorrelationId, generateDeclarativeConfigFromMetadata, generateDeclarativeConfigFromSchema, generateEntityConfig, generateRelationConfig, humanizeFieldName, inferFieldType, isSearchCandidate, isSoftDeleteCandidate, isTimestampField, loadDeclarativeConfigFromFile, loadDeclarativeConfigFromString, loadMaestroConfig, mergeIntrospectionWithOverrides, normalizeIntrospectionResult, parseQueryInput, resolveConsumerProjections, serializeDeclarativeConfig, serializeDeclarativeConfigToJson, serializeDeclarativeConfigToYaml, tableNameToEntityId, tableNameToLabel, validateConsumerDeclaration, validateEntityDeclaration, validateIntrospectionResult, validateMaestroConfig };
package/dist/index.js CHANGED
@@ -2515,6 +2515,115 @@ async function loadMaestroConfig(options) {
2515
2515
  };
2516
2516
  }
2517
2517
 
2518
+ // src/introspection/IntrospectionValidator.ts
2519
+ function isObject(value) {
2520
+ return value !== null && typeof value === "object" && !Array.isArray(value);
2521
+ }
2522
+ function nonEmptyString(value) {
2523
+ return typeof value === "string" && value.length > 0;
2524
+ }
2525
+ function typeOf(value) {
2526
+ if (value === null) return "null";
2527
+ if (Array.isArray(value)) return "array";
2528
+ return typeof value;
2529
+ }
2530
+ function reject(issues, path, expected, received) {
2531
+ issues.push({ path, message: `expected ${expected}, received ${typeOf(received)}.` });
2532
+ }
2533
+ function validateEndpoint(endpoint, path, issues) {
2534
+ if (!isObject(endpoint)) {
2535
+ reject(issues, path, 'an object with "table" and "field"', endpoint);
2536
+ return;
2537
+ }
2538
+ if (!nonEmptyString(endpoint["table"])) {
2539
+ reject(issues, `${path}.table`, "a non-empty string", endpoint["table"]);
2540
+ }
2541
+ if (!nonEmptyString(endpoint["field"])) {
2542
+ reject(issues, `${path}.field`, "a non-empty string", endpoint["field"]);
2543
+ }
2544
+ }
2545
+ function validateField(entityIndex, fieldIndex, field, issues) {
2546
+ const path = `entities[${entityIndex}].fields[${fieldIndex}]`;
2547
+ if (!isObject(field)) {
2548
+ reject(issues, path, "an object", field);
2549
+ return;
2550
+ }
2551
+ if (!nonEmptyString(field["name"])) reject(issues, `${path}.name`, "a non-empty string", field["name"]);
2552
+ if (typeof field["nativeType"] !== "string") reject(issues, `${path}.nativeType`, "a string", field["nativeType"]);
2553
+ if (typeof field["type"] !== "string") reject(issues, `${path}.type`, "a string", field["type"]);
2554
+ if (typeof field["nullable"] !== "boolean") reject(issues, `${path}.nullable`, "a boolean", field["nullable"]);
2555
+ if (typeof field["isPrimaryKey"] !== "boolean") {
2556
+ reject(issues, `${path}.isPrimaryKey`, "a boolean", field["isPrimaryKey"]);
2557
+ }
2558
+ }
2559
+ function validateEntity2(index, entity, issues) {
2560
+ const path = `entities[${index}]`;
2561
+ if (!isObject(entity)) {
2562
+ reject(issues, path, "an object", entity);
2563
+ return;
2564
+ }
2565
+ if (!nonEmptyString(entity["table"])) reject(issues, `${path}.table`, "a non-empty string", entity["table"]);
2566
+ if (!Array.isArray(entity["fields"])) {
2567
+ reject(issues, `${path}.fields`, "an array", entity["fields"]);
2568
+ return;
2569
+ }
2570
+ const fields = entity["fields"];
2571
+ for (let j = 0; j < fields.length; j++) {
2572
+ validateField(index, j, fields[j], issues);
2573
+ }
2574
+ }
2575
+ function validateRelation2(index, relation, issues) {
2576
+ const path = `relations[${index}]`;
2577
+ if (!isObject(relation)) {
2578
+ reject(issues, path, "an object", relation);
2579
+ return;
2580
+ }
2581
+ if (!nonEmptyString(relation["id"])) reject(issues, `${path}.id`, "a non-empty string", relation["id"]);
2582
+ if (typeof relation["type"] !== "string") reject(issues, `${path}.type`, "a string", relation["type"]);
2583
+ validateEndpoint(relation["from"], `${path}.from`, issues);
2584
+ validateEndpoint(relation["to"], `${path}.to`, issues);
2585
+ if (relation["confidence"] !== "definite" && relation["confidence"] !== "inferred") {
2586
+ issues.push({
2587
+ path: `${path}.confidence`,
2588
+ message: `expected "definite" or "inferred", received ${JSON.stringify(relation["confidence"])}.`
2589
+ });
2590
+ }
2591
+ }
2592
+ function validateIntrospectionResult(value) {
2593
+ const issues = [];
2594
+ if (!isObject(value)) {
2595
+ reject(issues, "", 'an object with "entities" and "relations" arrays', value);
2596
+ return { valid: false, issues };
2597
+ }
2598
+ if (!Array.isArray(value["entities"])) {
2599
+ reject(issues, "entities", "an array", value["entities"]);
2600
+ } else {
2601
+ const entities = value["entities"];
2602
+ for (let i = 0; i < entities.length; i++) {
2603
+ validateEntity2(i, entities[i], issues);
2604
+ }
2605
+ }
2606
+ if (!Array.isArray(value["relations"])) {
2607
+ reject(issues, "relations", "an array", value["relations"]);
2608
+ } else {
2609
+ const relations = value["relations"];
2610
+ for (let i = 0; i < relations.length; i++) {
2611
+ validateRelation2(i, relations[i], issues);
2612
+ }
2613
+ }
2614
+ return { valid: issues.length === 0, issues };
2615
+ }
2616
+
2617
+ // src/introspection/IntrospectionNormalizer.ts
2618
+ function compareStrings(a, b) {
2619
+ return a < b ? -1 : a > b ? 1 : 0;
2620
+ }
2621
+ function normalizeIntrospectionResult(result) {
2622
+ const entities = [...result.entities].sort((a, b) => compareStrings(a.table, b.table));
2623
+ const relations = [...result.relations].sort((a, b) => compareStrings(a.id, b.id));
2624
+ return { entities, relations };
2625
+ }
2626
+
2518
2627
  // src/config-generator/ConfigGenerator.ts
2519
2628
  function detectSoftDelete2(fields) {
2520
2629
  const PRIORITY = [
@@ -3411,6 +3520,189 @@ async function loadDeclarativeConfigFromFile(filePath, options = {}) {
3411
3520
  return loadDeclarativeConfigFromString(content, { format, yamlParser: options.yamlParser, source: filePath });
3412
3521
  }
3413
3522
 
3523
+ // src/declarative/generator/DeclarativeConfigGenerator.ts
3524
+ function sortByName(items) {
3525
+ return [...items].sort((a, b) => a.name.localeCompare(b.name));
3526
+ }
3527
+ function capabilitiesOverride(capabilities) {
3528
+ const overrides = {};
3529
+ let hasOverride = false;
3530
+ for (const key of Object.keys(DEFAULT_CAPABILITIES)) {
3531
+ if (capabilities[key] !== DEFAULT_CAPABILITIES[key]) {
3532
+ overrides[key] = capabilities[key];
3533
+ hasOverride = true;
3534
+ }
3535
+ }
3536
+ return hasOverride ? overrides : void 0;
3537
+ }
3538
+ function relationToDeclaration(rel) {
3539
+ return {
3540
+ id: rel.id,
3541
+ type: rel.type,
3542
+ fromField: rel.from.field,
3543
+ toEntity: rel.to.entity,
3544
+ toField: rel.to.field,
3545
+ label: rel.label,
3546
+ display: rel.display ? { tab: rel.display.tab, label: rel.display.label } : void 0
3547
+ };
3548
+ }
3549
+ function fieldMetadataToDeclaration(field, primaryKey) {
3550
+ const decl = { type: field.type };
3551
+ if (field.label) decl.label = field.label;
3552
+ if (field.required) decl.required = true;
3553
+ if (field.readonly) decl.readonly = true;
3554
+ if (field.name === primaryKey) decl.primary = true;
3555
+ if (field.sensitive) decl.sensitive = true;
3556
+ if (field.hidden) decl.visible = false;
3557
+ if (field.searchable) decl.searchable = true;
3558
+ if (field.sortable) decl.sortable = true;
3559
+ if (field.filterable) decl.filterable = true;
3560
+ if (field.exportable) decl.exportable = true;
3561
+ if (field.description) decl.description = field.description;
3562
+ if (field.enumOptions) decl.enumOptions = field.enumOptions;
3563
+ if (field.relationEntity) decl.relationEntity = field.relationEntity;
3564
+ return decl;
3565
+ }
3566
+ function fieldSchemaToDeclaration(field, primaryKey) {
3567
+ const decl = { type: field.type };
3568
+ if (field.label) decl.label = field.label;
3569
+ if (field.required) decl.required = true;
3570
+ if (field.readonly) decl.readonly = true;
3571
+ if (primaryKey && field.name === primaryKey) decl.primary = true;
3572
+ if (field.sensitive) decl.sensitive = true;
3573
+ if (field.hidden) decl.visible = false;
3574
+ if (field.searchable) decl.searchable = true;
3575
+ if (field.sortable) decl.sortable = true;
3576
+ if (field.filterable) decl.filterable = true;
3577
+ if (field.exportable) decl.exportable = true;
3578
+ if (field.description) decl.description = field.description;
3579
+ if (field.enumOptions) decl.enumOptions = field.enumOptions;
3580
+ if (field.relationEntity) decl.relationEntity = field.relationEntity;
3581
+ return decl;
3582
+ }
3583
+ function operationMetadataToDeclaration(op, entityId) {
3584
+ const key = op.id.startsWith(`${entityId}.`) ? op.id.slice(entityId.length + 1) : op.id;
3585
+ const declaration = { label: op.label };
3586
+ if (op.description) declaration.description = op.description;
3587
+ if (op.scope) declaration.scope = op.scope;
3588
+ if (op.requiresConfirmation) declaration.confirmation = { required: true };
3589
+ if (op.requiredPermission) declaration.permission = op.requiredPermission;
3590
+ return { key, declaration };
3591
+ }
3592
+ function entityMetadataToDeclaration(entity, relations, operations, options) {
3593
+ const sourceFields = options.includeInternalFields ? entity.fields : entity.fields.filter((f) => !f.hidden);
3594
+ const orderedFields = options.sort ? sortByName(sourceFields) : sourceFields;
3595
+ const fields = {};
3596
+ for (const field of orderedFields) {
3597
+ fields[field.name] = fieldMetadataToDeclaration(field, entity.primaryKey);
3598
+ }
3599
+ const orderedRelations = options.sort ? [...relations].sort((a, b) => a.id.localeCompare(b.id)) : relations;
3600
+ const orderedOperations = options.sort ? [...operations].sort((a, b) => a.id.localeCompare(b.id)) : operations;
3601
+ const operationsRecord = {};
3602
+ for (const op of orderedOperations) {
3603
+ const { key, declaration: declaration2 } = operationMetadataToDeclaration(op, entity.id);
3604
+ operationsRecord[key] = declaration2;
3605
+ }
3606
+ const declaration = {
3607
+ entity: entity.id,
3608
+ label: entity.label.singular,
3609
+ pluralLabel: entity.label.plural,
3610
+ fields
3611
+ };
3612
+ if (entity.description) declaration.description = entity.description;
3613
+ const capabilities = capabilitiesOverride(entity.capabilities);
3614
+ if (capabilities) declaration.capabilities = capabilities;
3615
+ if (orderedOperations.length > 0) declaration.operations = operationsRecord;
3616
+ if (orderedRelations.length > 0) {
3617
+ declaration.relationships = orderedRelations.map(relationToDeclaration);
3618
+ }
3619
+ return declaration;
3620
+ }
3621
+ function entitySchemaToDeclaration(entity, relations, options) {
3622
+ const sourceFields = options.includeInternalFields ? entity.fields : entity.fields.filter((f) => !f.hidden);
3623
+ const orderedFields = options.sort ? sortByName(sourceFields) : sourceFields;
3624
+ const fields = {};
3625
+ for (const field of orderedFields) {
3626
+ fields[field.name] = fieldSchemaToDeclaration(field, entity.source.primaryKey);
3627
+ }
3628
+ const orderedRelations = options.sort ? [...relations].sort((a, b) => a.id.localeCompare(b.id)) : relations;
3629
+ const declaration = {
3630
+ entity: entity.id,
3631
+ label: entity.label.singular,
3632
+ pluralLabel: entity.label.plural,
3633
+ fields
3634
+ };
3635
+ if (entity.description) declaration.description = entity.description;
3636
+ if (entity.capabilities) declaration.capabilities = entity.capabilities;
3637
+ if (orderedRelations.length > 0) {
3638
+ declaration.relationships = orderedRelations.map(relationToDeclaration);
3639
+ }
3640
+ return declaration;
3641
+ }
3642
+ function generateDefaultConsumerDeclaration(entity) {
3643
+ const visibleFieldNames = Object.entries(entity.fields).filter(([, field]) => field.visible !== false).map(([name]) => name);
3644
+ return {
3645
+ consumer: entity.entity,
3646
+ entity: entity.entity,
3647
+ list: { fields: visibleFieldNames },
3648
+ detail: { fields: visibleFieldNames }
3649
+ };
3650
+ }
3651
+ function generateDeclarativeConfigFromMetadata(input, options = {}) {
3652
+ const resolved = {
3653
+ includeInternalFields: options.includeInternalFields ?? true,
3654
+ sort: options.sort ?? true
3655
+ };
3656
+ const orderedEntities = resolved.sort ? [...input.entities].sort((a, b) => a.id.localeCompare(b.id)) : input.entities;
3657
+ const entities = orderedEntities.map(
3658
+ (entity) => entityMetadataToDeclaration(
3659
+ entity,
3660
+ (input.relations ?? []).filter((rel) => rel.from.entity === entity.id),
3661
+ (input.operations ?? []).filter((op) => op.entity === entity.id),
3662
+ resolved
3663
+ )
3664
+ );
3665
+ const consumers = options.includeConsumers ? entities.map(generateDefaultConsumerDeclaration) : void 0;
3666
+ return consumers ? { entities, consumers } : { entities };
3667
+ }
3668
+ function generateDeclarativeConfigFromSchema(input, options = {}) {
3669
+ const resolved = {
3670
+ includeInternalFields: options.includeInternalFields ?? true,
3671
+ sort: options.sort ?? true
3672
+ };
3673
+ const orderedEntities = resolved.sort ? [...input.entities].sort((a, b) => a.id.localeCompare(b.id)) : input.entities;
3674
+ const entities = orderedEntities.map(
3675
+ (entity) => entitySchemaToDeclaration(
3676
+ entity,
3677
+ (input.relations ?? []).filter((rel) => rel.from.entity === entity.id),
3678
+ resolved
3679
+ )
3680
+ );
3681
+ const consumers = options.includeConsumers ? entities.map(generateDefaultConsumerDeclaration) : void 0;
3682
+ return consumers ? { entities, consumers } : { entities };
3683
+ }
3684
+
3685
+ // src/declarative/generator/DeclarativeConfigSerializer.ts
3686
+ function serializeDeclarativeConfigToJson(config) {
3687
+ return `${JSON.stringify(config, null, 2)}
3688
+ `;
3689
+ }
3690
+ function serializeDeclarativeConfigToYaml(config, yamlSerializer) {
3691
+ return yamlSerializer.stringify(config);
3692
+ }
3693
+ function serializeDeclarativeConfig(config, options) {
3694
+ if (options.format === "json") {
3695
+ return serializeDeclarativeConfigToJson(config);
3696
+ }
3697
+ if (!options.yamlSerializer) {
3698
+ throw new MaestroError(
3699
+ "CONFIGURATION_ERROR" /* CONFIGURATION_ERROR */,
3700
+ 'Cannot serialize declarative config to YAML: a "yamlSerializer" must be provided. The core has no bundled YAML dependency \u2014 install a YAML library (e.g. "yaml") and pass { yamlSerializer: { stringify: YAML.stringify } }.'
3701
+ );
3702
+ }
3703
+ return serializeDeclarativeConfigToYaml(config, options.yamlSerializer);
3704
+ }
3705
+
3414
3706
  // src/governance/GovernanceEventType.ts
3415
3707
  var GOVERNANCE_EVENT_TYPES = {
3416
3708
  OPERATION_EXECUTED: "governance.operation.executed",
@@ -3514,6 +3806,8 @@ export {
3514
3806
  detectDisplayField,
3515
3807
  generateAllConfigs,
3516
3808
  generateCorrelationId,
3809
+ generateDeclarativeConfigFromMetadata,
3810
+ generateDeclarativeConfigFromSchema,
3517
3811
  generateEntityConfig,
3518
3812
  generateRelationConfig,
3519
3813
  humanizeFieldName,
@@ -3525,12 +3819,17 @@ export {
3525
3819
  loadDeclarativeConfigFromString,
3526
3820
  loadMaestroConfig,
3527
3821
  mergeIntrospectionWithOverrides,
3822
+ normalizeIntrospectionResult,
3528
3823
  parseQueryInput,
3529
3824
  resolveConsumerProjections,
3825
+ serializeDeclarativeConfig,
3826
+ serializeDeclarativeConfigToJson,
3827
+ serializeDeclarativeConfigToYaml,
3530
3828
  tableNameToEntityId,
3531
3829
  tableNameToLabel,
3532
3830
  validateConsumerDeclaration,
3533
3831
  validateEntityDeclaration,
3832
+ validateIntrospectionResult,
3534
3833
  validateMaestroConfig
3535
3834
  };
3536
3835
  //# sourceMappingURL=index.js.map