/** * Lema Relationship Management System * * Provides type definitions and utilities for managing relationships * between Lema collections. Supports various relationship types and * cascade behaviors for maintaining data integrity. * * Relationship Types: * - **o2m** (One-to-Many): One record in current collection relates to many in related collection * - **m2o** (Many-to-One): Many records in current collection relate to one in related collection * - **o2o** (One-to-One): One record in current collection relates to one in related collection * - **m2m** (Many-to-Many): Many records in current collection relate to many in related collection * * Cascade Behaviors: * - **CASCADE**: Automatically delete related records when parent is deleted * - **SET_NULL**: Set foreign key to null when parent is deleted * - **RESTRICT**: Prevent deletion of parent if related records exist * * Dependencies: * - No external dependencies, pure TypeScript type definitions * * Integration Points: * - Used by Lema ORM for foreign key constraint generation * - Used by field definitions for relationship configuration * - Used by migration systems for database schema generation * - Used by validation services for relationship integrity checks * * @example * ```typescript * const userPostRelation: LemaRelation = { * relationship: 'm2o', * related_field: 'author_id', * related_collection_key: 'users', * field: 'author', * collection_key: 'posts', * on_delete: 'SET_NULL' * }; * ``` */ /** * Collection Relationship Types * * Defines the cardinality of relationships between Lema collections. * Each type represents how records in one collection relate to records * in another collection. */ export type LemaCollectionRelationship = 'o2m' | 'm2o' | 'o2o' | 'm2m'; /** * Relation On-Delete Behaviors * * Defines what happens to related records when a parent record * is deleted. These behaviors ensure data integrity and consistency * across related collections. */ export type LemaRelationOnDelete = 'CASCADE' | 'SET_NULL' | 'RESTRICT'; /** * Complete Relationship Definition * * Defines all aspects of a relationship between two collections, * including the relationship type, field mappings, and cascade behaviors. * * @property relationship - Type of relationship (cardinality) * @property related_field - Foreign key field name in the related collection * @property related_collection_key - Unique identifier of the related collection * @property field - Field name in the current collection that stores the relationship * @property collection_key - Unique identifier of the current collection * @property on_delete - Behavior when parent record is deleted */ export type LemaRelation = { relationship: LemaCollectionRelationship | string; related_field: string; related_collection_key: string; field: string; collection_key: string; on_delete?: LemaRelationOnDelete; };