supa-seed
Version:
A constraint-aware, framework-agnostic database seeding framework with deep PostgreSQL business logic discovery and MakerKit integration support
203 lines • 5.57 kB
TypeScript
/**
* Constraint Discovery Engine for PostgreSQL
* Parses triggers, functions, and constraints to extract business logic rules
* Part of supa-seed v2.2.0 constraint-aware architecture
*/
import type { createClient } from '@supabase/supabase-js';
type SupabaseClient = ReturnType<typeof createClient>;
export interface PostgreSQLFunction {
name: string;
schema: string;
definition: string;
returnType: string;
parameters: PostgreSQLParameter[];
language: string;
volatility: 'IMMUTABLE' | 'STABLE' | 'VOLATILE';
}
export interface PostgreSQLParameter {
name: string;
type: string;
mode: 'IN' | 'OUT' | 'INOUT';
}
export interface PostgreSQLTrigger {
name: string;
tableName: string;
schema: string;
timing: 'BEFORE' | 'AFTER' | 'INSTEAD OF';
events: ('INSERT' | 'UPDATE' | 'DELETE')[];
functionName: string;
functionSchema: string;
condition?: string;
isEnabled: boolean;
}
export interface BusinessRule {
id: string;
name: string;
type: 'validation' | 'transformation' | 'dependency' | 'business_logic';
table: string;
condition: string;
action: 'allow' | 'deny' | 'modify' | 'require';
errorMessage?: string;
autoFix?: AutoFixSuggestion;
confidence: number;
sqlPattern: string;
dependencies: string[];
}
export interface AutoFixSuggestion {
type: 'set_field' | 'create_dependency' | 'skip_operation' | 'modify_workflow';
description: string;
action: AutoFixAction;
confidence: number;
impact?: 'low' | 'medium' | 'high';
}
export interface AutoFixAction {
table?: string;
field?: string;
value?: any;
sqlCode?: string;
workflowModification?: {
skipStep?: boolean;
addStep?: WorkflowStepTemplate;
modifyConditions?: string[];
};
}
export interface WorkflowStepTemplate {
id: string;
table: string;
operation: 'insert' | 'update' | 'validate' | 'skip';
fields: Record<string, any>;
conditions?: string[];
}
export interface ConstraintMetadata {
tables: TableConstraints[];
businessRules: BusinessRule[];
dependencies: TableDependency[];
triggers: TriggerRule[];
functions: FunctionRule[];
confidence: number;
discoveryTimestamp: string;
}
export interface TableConstraints {
tableName: string;
constraints: BusinessRule[];
triggers: TriggerRule[];
dependencies: TableDependency[];
}
export interface TriggerRule {
triggerName: string;
tableName: string;
functionName: string;
extractedRules: BusinessRule[];
rawDefinition: string;
parsedSuccessfully: boolean;
}
export interface FunctionRule {
functionName: string;
schema: string;
extractedRules: BusinessRule[];
rawDefinition: string;
parsedSuccessfully: boolean;
}
export interface TableDependency {
fromTable: string;
toTable: string;
relationship: 'required' | 'optional' | 'conditional';
condition?: string;
constraint: string;
}
export interface DependencyGraph {
nodes: DependencyNode[];
edges: DependencyEdge[];
cycles: DependencyNode[][];
creationOrder: string[];
}
export interface DependencyNode {
table: string;
dependencies: string[];
dependents: string[];
}
export interface DependencyEdge {
from: string;
to: string;
type: 'required' | 'optional' | 'conditional';
constraint: string;
}
export declare class ConstraintDiscoveryEngine {
private client;
private discoveryCache;
private functionCache;
constructor(client: SupabaseClient);
/**
* Main entry point: discover all constraints for schema tables
*/
discoverConstraints(tableNames: string[]): Promise<ConstraintMetadata>;
/**
* Discover all triggers for specified tables
*/
private discoverTriggers;
/**
* Fallback trigger discovery using information_schema
*/
private fallbackTriggerDiscovery;
/**
* Discover and load function definitions for triggers
*/
private discoverTriggerFunctions;
/**
* Load a PostgreSQL function definition
*/
private loadFunctionDefinition;
/**
* Parse business logic rules from PostgreSQL functions
*/
private parseBusinessLogicFromFunctions;
/**
* Parse business rules from a single function definition
*/
private parseBusinessRulesFromFunction;
/**
* Extract business rule from RAISE EXCEPTION pattern
*/
private extractRuleFromException;
/**
* Extract business rule from conditional pattern
*/
private extractRuleFromConditional;
/**
* Build table-specific constraint metadata
*/
private buildTableConstraints;
/**
* Extract dependencies from business rules
*/
private extractDependencies;
/**
* Extract table-specific dependencies
*/
private extractTableDependencies;
/**
* Process triggers into rule metadata
*/
private processTriggerRules;
/**
* Process functions into rule metadata
*/
private processFunctionRules;
/**
* Build dependency graph for table creation ordering
*/
buildDependencyGraph(constraints: ConstraintMetadata): Promise<DependencyGraph>;
/**
* Utility methods
*/
private mapVolatility;
private calculateConfidenceScore;
private detectCycles;
private calculateCreationOrder;
/**
* Clear discovery cache
*/
clearCache(): void;
}
export {};
//# sourceMappingURL=constraint-discovery-engine.d.ts.map