import { Entity, EntitySchema, NormalizationResult, NormalizedEntities, Schema } from './normalizer'; import { DenormalizeOptions } from './denormalizer'; /** * Schema definition for registry */ export interface SchemaDefinition { /** ID attribute name or function */ idAttribute?: string | ((entity: Entity) => string); /** Relationship definitions (can reference other schemas by name) */ relations?: Record; /** Process entity after extraction */ processStrategy?: (entity: Entity) => Entity; /** Merge strategy for duplicate entities */ mergeStrategy?: (existing: Entity, incoming: Entity) => Entity; /** Fields to exclude from normalization */ excludeFields?: string[]; /** Schema version */ version?: number; /** Migration function from previous version */ migrate?: (entity: Entity, fromVersion: number) => Entity; } /** * Relation definition */ export interface RelationDefinition { /** Schema name or schema instance */ schema: string | Schema; /** Relation type */ type: 'entity' | 'array' | 'union'; /** For union types, the discriminator */ schemaAttribute?: string | ((entity: Entity) => string); /** Union schema mappings */ schemas?: Record; } /** * Schema registry configuration */ export interface SchemaRegistryConfig { /** Enable strict mode (throw on undefined schemas) */ strict?: boolean; /** Enable automatic reverse relationship inference */ inferReverseRelations?: boolean; /** Default ID attribute */ defaultIdAttribute?: string; /** Schema version storage key */ versionStorageKey?: string; /** Enable schema caching */ enableCaching?: boolean; } /** * Schema registry interface */ export interface SchemaRegistry { /** Register a schema */ register: (name: string, definition?: SchemaDefinition) => EntitySchema; /** Get a schema by name */ get: (name: string) => EntitySchema | undefined; /** Check if schema exists */ has: (name: string) => boolean; /** Get all registered schema names */ getNames: () => string[]; /** Get all schemas */ getAll: () => Map; /** Unregister a schema */ unregister: (name: string) => boolean; /** Clear all schemas */ clear: () => void; /** Normalize data using registry */ normalize: (data: unknown, schemaName: string) => NormalizationResult; /** Denormalize data using registry */ denormalize: (input: unknown, schemaName: string, entities: NormalizedEntities, options?: DenormalizeOptions) => T | null; /** Validate schema integrity */ validate: () => SchemaValidationResult; /** Get schema dependencies */ getDependencies: (name: string) => string[]; /** Get reverse dependencies */ getReverseDependencies: (name: string) => string[]; /** Export schemas for serialization */ export: () => ExportedSchemas; /** Import schemas from serialization */ import: (schemas: ExportedSchemas) => void; /** Get schema version */ getVersion: (name: string) => number; /** Migrate entity to latest schema version */ migrateEntity: (name: string, entity: Entity, fromVersion: number) => Entity; } /** * Schema validation result */ export interface SchemaValidationResult { valid: boolean; errors: SchemaValidationError[]; warnings: SchemaValidationWarning[]; } /** * Schema validation error */ export interface SchemaValidationError { schema: string; type: 'missing_relation' | 'circular_dependency' | 'invalid_definition'; message: string; details?: unknown; } /** * Schema validation warning */ export interface SchemaValidationWarning { schema: string; type: 'unused_schema' | 'missing_reverse_relation' | 'potential_issue'; message: string; details?: unknown; } /** * Exported schemas format */ export interface ExportedSchemas { version: string; schemas: Record; exportedAt: string; } /** * Create a schema registry * * @param config - Registry configuration * @returns Schema registry instance * * @example * ```typescript * const registry = createSchemaRegistry({ strict: true }); * * // Register schemas * registry.register('users', { * relations: { * posts: { schema: 'posts', type: 'array' }, * }, * }); * * registry.register('posts', { * relations: { * author: 'users', * }, * }); * * // Use registry * const { entities, result } = registry.normalize(apiData, 'posts'); * const post = registry.denormalize('101', 'posts', entities); * ``` */ export declare function createSchemaRegistry(config?: SchemaRegistryConfig): SchemaRegistry; /** * Get or create global schema registry */ export declare function getGlobalRegistry(): SchemaRegistry; /** * Reset global schema registry */ export declare function resetGlobalRegistry(): void; /** * Register a schema in global registry */ export declare function registerSchema(name: string, definition?: SchemaDefinition): EntitySchema; /** * Get schema from global registry */ export declare function getSchema(name: string): EntitySchema | undefined; /** * Normalize using global registry */ export declare function normalizeWithRegistry(data: unknown, schemaName: string): NormalizationResult; /** * Denormalize using global registry */ export declare function denormalizeWithRegistry(input: unknown, schemaName: string, entities: NormalizedEntities, options?: DenormalizeOptions): T | null; /** * Fluent schema builder */ export declare class SchemaBuilder { private readonly name; private readonly definition; private registry; constructor(name: string, registry?: SchemaRegistry); /** * Set ID attribute */ id(attribute: string | ((entity: Entity) => string)): this; /** * Add entity relation */ belongsTo(field: string, schemaName: string): this; /** * Add array relation */ hasMany(field: string, schemaName: string): this; /** * Add union relation */ union(field: string, schemas: Record, discriminator?: string | ((entity: Entity) => string)): this; /** * Set process strategy */ process(fn: (entity: Entity) => Entity): this; /** * Set merge strategy */ merge(fn: (existing: Entity, incoming: Entity) => Entity): this; /** * Set excluded fields */ exclude(...fields: string[]): this; /** * Set version */ version(v: number, migrate?: (entity: Entity, fromVersion: number) => Entity): this; /** * Build and register schema */ build(): EntitySchema; } /** * Create a schema builder */ export declare function defineSchema(name: string, registry?: SchemaRegistry): SchemaBuilder;