supa-seed
Version:
A constraint-aware, framework-agnostic database seeding framework with deep PostgreSQL business logic discovery and MakerKit integration support
183 lines • 5.55 kB
TypeScript
/**
* Junction Table Handler
* Handles many-to-many relationships and junction table seeding
*/
import type { createClient } from '@supabase/supabase-js';
import { DependencyGraph, ForeignKeyRelationship } from './dependency-graph';
type SupabaseClient = ReturnType<typeof createClient>;
export interface JunctionTableInfo {
tableName: string;
schema: string;
leftTable: string;
leftColumn: string;
rightTable: string;
rightColumn: string;
leftForeignKey: ForeignKeyRelationship;
rightForeignKey: ForeignKeyRelationship;
additionalColumns: JunctionColumn[];
cardinality: CardinalityInfo;
isDetected: boolean;
confidence: number;
}
export interface JunctionColumn {
name: string;
type: string;
isNullable: boolean;
hasDefault: boolean;
defaultValue?: any;
isTimestamp: boolean;
isMetadata: boolean;
}
export interface CardinalityInfo {
leftCardinality: 'one' | 'many';
rightCardinality: 'one' | 'many';
relationshipType: 'one_to_one' | 'one_to_many' | 'many_to_many';
isOptional: boolean;
estimatedDensity: number;
}
export interface RelationshipPattern {
name: string;
pattern: RegExp;
leftTable: string;
rightTable: string;
confidence: number;
description: string;
}
export interface JunctionTableDetectionResult {
success: boolean;
junctionTables: JunctionTableInfo[];
relationshipPatterns: RelationshipPattern[];
totalRelationships: number;
confidence: number;
warnings: string[];
errors: string[];
recommendations: string[];
}
export interface JunctionSeedingOptions {
generateRelationships: boolean;
relationshipDensity: number;
respectCardinality: boolean;
avoidOrphans: boolean;
distributionStrategy: 'random' | 'even' | 'weighted' | 'clustered';
clusterFactor: number;
weightFunction?: (leftRecord: any, rightRecord: any) => number;
generateMetadata: boolean;
includeTimestamps: boolean;
customDataGenerators: Map<string, (leftRecord: any, rightRecord: any) => any>;
batchSize: number;
maxRelationshipsPerRecord: number;
validateForeignKeys: boolean;
}
export interface JunctionSeedingResult {
success: boolean;
junctionTable: string;
relationshipsCreated: number;
relationshipsSkipped: number;
batchesProcessed: number;
orphansAvoided: number;
validationErrors: string[];
warnings: string[];
errors: string[];
executionTime: number;
metadata: {
leftTableRecords: number;
rightTableRecords: number;
possibleRelationships: number;
actualDensity: number;
averageRelationshipsPerLeftRecord: number;
averageRelationshipsPerRightRecord: number;
};
}
export declare const COMMON_JUNCTION_PATTERNS: RelationshipPattern[];
export declare class JunctionTableHandler {
private client;
private detectedJunctionTables;
private relationshipPatterns;
constructor(client: SupabaseClient);
/**
* Detect junction tables from dependency graph
*/
detectJunctionTables(dependencyGraph: DependencyGraph): Promise<JunctionTableDetectionResult>;
/**
* Analyze a specific table to determine if it's a junction table
*/
private analyzeJunctionTable;
/**
* Find foreign key relationship from dependency edge
*/
private findForeignKeyRelationship;
/**
* Get additional columns in junction table (beyond foreign keys)
*/
private getAdditionalColumns;
/**
* Analyze cardinality of the relationship
*/
private analyzeCardinality;
/**
* Calculate confidence score for junction table detection
*/
private calculateJunctionConfidence;
/**
* Detect relationship patterns in junction tables
*/
private detectRelationshipPatterns;
/**
* Seed junction table with relationships
*/
seedJunctionTable(junctionTableName: string, options?: Partial<JunctionSeedingOptions>): Promise<JunctionSeedingResult>;
/**
* Get records from a table for relationship generation
*/
private getTableRecords;
/**
* Generate relationships between two sets of records
*/
private generateRelationships;
/**
* Generate random relationships
*/
private generateRandomRelationships;
/**
* Generate even distribution of relationships
*/
private generateEvenRelationships;
/**
* Generate clustered relationships (some records have many relationships, others few)
*/
private generateClusteredRelationships;
/**
* Create a junction record from two related records
*/
private createJunctionRecord;
/**
* Generate value for a junction table column
*/
private generateColumnValue;
/**
* Calculate overall detection confidence
*/
private calculateDetectionConfidence;
/**
* Generate recommendations for junction table handling
*/
private generateJunctionRecommendations;
/**
* Get detected junction table information
*/
getJunctionTableInfo(tableName: string): JunctionTableInfo | undefined;
/**
* Get all detected junction tables
*/
getAllJunctionTables(): JunctionTableInfo[];
/**
* Add custom relationship pattern
*/
addRelationshipPattern(pattern: RelationshipPattern): void;
/**
* Clear detected junction tables
*/
clearDetectedTables(): void;
}
export {};
//# sourceMappingURL=junction-table-handler.d.ts.map