supa-seed
Version:
A constraint-aware, framework-agnostic database seeding framework with deep PostgreSQL business logic discovery and MakerKit integration support
206 lines • 6.14 kB
TypeScript
/**
* Schema Introspection System v2.2.0
* Dynamically discovers database structure, constraints, and relationships
* Now includes deep PostgreSQL constraint discovery and business logic parsing
* Part of the constraint-aware architecture evolution
*/
import type { createClient } from '@supabase/supabase-js';
import { type ConstraintMetadata } from '../features/analysis/constraint-discovery-engine';
type SupabaseClient = ReturnType<typeof createClient>;
export interface DatabaseColumn {
name: string;
type: string;
isNullable: boolean;
defaultValue: string | null;
isPrimaryKey: boolean;
isForeignKey: boolean;
maxLength?: number;
enumValues?: string[];
}
export interface DatabaseConstraint {
name: string;
type: 'PRIMARY KEY' | 'FOREIGN KEY' | 'UNIQUE' | 'CHECK' | 'NOT NULL';
columns: string[];
referencedTable?: string;
referencedColumns?: string[];
checkDefinition?: string;
isDeferrable: boolean;
onDelete?: 'CASCADE' | 'SET NULL' | 'RESTRICT' | 'NO ACTION';
onUpdate?: 'CASCADE' | 'SET NULL' | 'RESTRICT' | 'NO ACTION';
}
export interface DatabaseTable {
name: string;
schema: string;
columns: DatabaseColumn[];
constraints: DatabaseConstraint[];
indexes: DatabaseIndex[];
triggers: DatabaseTrigger[];
rowCount: number;
hasData: boolean;
}
export interface DatabaseIndex {
name: string;
columns: string[];
isUnique: boolean;
method: string;
}
export interface DatabaseTrigger {
name: string;
timing: 'BEFORE' | 'AFTER' | 'INSTEAD OF';
events: ('INSERT' | 'UPDATE' | 'DELETE')[];
functionName: string;
}
export interface SchemaRelationship {
fromTable: string;
fromColumn: string;
toTable: string;
toColumn: string;
relationshipType: 'one_to_one' | 'one_to_many' | 'many_to_many';
cascadeDelete: boolean;
isRequired: boolean;
}
export interface TablePattern {
name: string;
confidence: number;
evidence: string[];
suggestedRole: 'user' | 'content' | 'association' | 'system' | 'auth';
columnMappings: Record<string, string[]>;
}
export interface SchemaIntrospectionResult {
tables: DatabaseTable[];
relationships: SchemaRelationship[];
patterns: TablePattern[];
constraints: {
userCreationConstraints: ConstraintRule[];
dataIntegrityRules: ConstraintRule[];
businessLogicConstraints: ConstraintRule[];
discoveredConstraints?: ConstraintMetadata;
};
framework: {
type: 'makerkit' | 'nextjs' | 'remix' | 'custom';
version: string;
confidence: number;
evidence: string[];
};
recommendations: SchemaRecommendation[];
}
export interface ConstraintRule {
table: string;
rule: string;
type: 'required_relationship' | 'conditional_insert' | 'value_constraint' | 'business_rule';
description: string;
sqlCondition: string;
requiresValidation: boolean;
}
export interface SchemaRecommendation {
type: 'warning' | 'optimization' | 'configuration';
message: string;
table?: string;
suggestedAction?: string;
priority: 'high' | 'medium' | 'low';
}
export declare class SchemaIntrospector {
private client;
private cache;
private introspectionCache;
private constraintEngine;
constructor(client: SupabaseClient);
/**
* Perform comprehensive schema introspection
*/
introspectSchema(): Promise<SchemaIntrospectionResult>;
/**
* Discover all tables with their columns, constraints, and metadata
*/
private discoverTables;
/**
* Fallback table discovery when information_schema is not accessible
*/
private fallbackTableDiscovery;
/**
* Get detailed column information for a table
*/
private getTableColumns;
/**
* Fallback method to get column info when information_schema is not available
*/
private fallbackGetColumns;
/**
* Get table constraints (primary keys, foreign keys, checks, etc.)
*/
private getTableConstraints;
/**
* Get detailed information about a specific constraint
*/
private getConstraintDetails;
/**
* Analyze relationships between tables
*/
private analyzeRelationships;
/**
* Identify table patterns and their likely roles in the application
*/
private identifyTablePatterns;
/**
* Analyze a single table to determine its pattern and role
*/
private analyzeTablePattern;
/**
* Helper methods for pattern recognition
*/
private hasUserTableColumns;
private hasContentTableColumns;
private hasAssociationTableColumns;
private hasMakerKitColumns;
private findColumnVariants;
private cleanColumnMappings;
/**
* Extract business constraints and rules from the schema
*/
private extractConstraints;
/**
* Detect framework type and version based on schema patterns
*/
private detectFramework;
/**
* Generate recommendations based on introspection results
*/
private generateRecommendations;
/**
* Helper methods for constraint analysis
*/
private analyzeUserCreationConstraints;
private analyzeDataIntegrityRules;
private analyzeBusinessLogicConstraints;
/**
* Utility methods
*/
private tableExists;
private getTableRowCount;
private getTableIndexes;
private getTableTriggers;
private getEnumValues;
private determineRelationshipType;
/**
* v2.2.0: Convert engine business rule to legacy format for compatibility
*/
private convertEngineRuleToLegacy;
/**
* v2.2.0: Map engine rule types to legacy types
*/
private mapEngineRuleType;
/**
* v2.2.0: Access discovered constraints metadata
*/
getDiscoveredConstraints(): ConstraintMetadata | null;
/**
* Clear the introspection cache
*/
clearCache(): void;
/**
* Get cached introspection result
*/
getCachedResult(): SchemaIntrospectionResult | null;
}
export {};
//# sourceMappingURL=schema-introspector.d.ts.map