supa-seed
Version:
A constraint-aware, framework-agnostic database seeding framework with deep PostgreSQL business logic discovery and MakerKit integration support
250 lines • 7 kB
TypeScript
/**
* Constraint-Aware Workflow Execution Engine
* Pre-validates and executes database operations based on discovered constraints
* Part of supa-seed v2.2.0 constraint-aware architecture
*/
import type { createClient } from '@supabase/supabase-js';
import { type ConstraintMetadata, type BusinessRule, type AutoFixSuggestion } from './constraint-discovery-engine';
type SupabaseClient = ReturnType<typeof createClient>;
export interface WorkflowStep {
id: string;
table: string;
operation: 'insert' | 'update' | 'validate' | 'skip';
required: boolean;
conditions?: ConstraintCondition[];
fields: FieldMapping[];
onError: ErrorAction;
dependencies?: string[];
autoFixes?: AutoFixSuggestion[];
}
export interface ConstraintCondition {
type: 'exists' | 'equals' | 'custom' | 'business_rule';
table?: string;
field?: string;
value?: any;
customSQL?: string;
businessRuleId?: string;
description: string;
}
export interface FieldMapping {
name: string;
source: string;
value?: any;
required?: boolean;
validator?: string;
}
export interface ErrorAction {
type: 'fail' | 'skip' | 'retry' | 'auto_fix';
maxRetries?: number;
fallbackValue?: any;
customHandler?: string;
}
export interface WorkflowConfiguration {
version: '2.2.0';
strategy: 'constraint-aware' | 'schema-first' | 'legacy';
workflows: {
userCreation: UserCreationWorkflow;
dataSeeding?: DataSeedingWorkflow;
cleanup?: CleanupWorkflow;
};
constraints: {
discovery: ConstraintDiscoveryConfig;
validation: ConstraintValidationConfig;
handling: ConstraintHandlingConfig;
};
}
export interface UserCreationWorkflow {
steps: WorkflowStep[];
errorHandling: ErrorHandlingStrategy;
rollback: RollbackStrategy;
validation: ValidationStrategy;
}
export interface DataSeedingWorkflow {
steps: WorkflowStep[];
parallelExecution: boolean;
batchSize: number;
}
export interface CleanupWorkflow {
steps: WorkflowStep[];
cascading: boolean;
}
export interface ConstraintDiscoveryConfig {
enabled: boolean;
analyzeTriggers: boolean;
parseFunctions: boolean;
buildDependencyGraph: boolean;
cacheResults: boolean;
}
export interface ConstraintValidationConfig {
preValidation: boolean;
continueOnWarnings: boolean;
stopOnErrors: boolean;
validateDependencies: boolean;
}
export interface ConstraintHandlingConfig {
autoFix: boolean;
suggestFixes: boolean;
skipInvalidOperations: boolean;
createDependenciesOnDemand: boolean;
}
export interface ErrorHandlingStrategy {
type: 'fail_fast' | 'graceful_degradation' | 'best_effort';
maxFailures: number;
failureThreshold: number;
}
export interface RollbackStrategy {
enabled: boolean;
onCriticalFailure: boolean;
preserveSuccessfulSteps: boolean;
}
export interface ValidationStrategy {
preExecution: boolean;
postExecution: boolean;
dependencyValidation: boolean;
}
export interface ExecutionContext {
inputData: Record<string, any>;
generatedData: Record<string, any>;
stepResults: Record<string, StepResult>;
constraints: ConstraintMetadata;
currentStep: number;
totalSteps: number;
}
export interface StepResult {
stepId: string;
success: boolean;
data?: any;
error?: string;
warnings: string[];
constraintViolations: ConstraintViolation[];
autoFixesApplied: AutoFixApplied[];
duration: number;
rollbackData?: any;
}
export interface ConstraintViolation {
rule: BusinessRule;
violationType: 'validation' | 'dependency' | 'business_logic';
message: string;
suggestedFix?: AutoFixSuggestion;
canAutoFix: boolean;
}
export interface AutoFixApplied {
originalViolation: ConstraintViolation;
fixApplied: AutoFixSuggestion;
success: boolean;
resultingValue?: any;
}
export interface ExecutionResult {
success: boolean;
stepsExecuted: StepResult[];
stepsSkipped: SkippedStep[];
constraintViolations: ConstraintViolation[];
autoFixesApplied: AutoFixApplied[];
rollbackActions?: RollbackAction[];
executionSummary: ExecutionSummary;
}
export interface SkippedStep {
stepId: string;
reason: string;
constraintViolations: ConstraintViolation[];
}
export interface RollbackAction {
stepId: string;
action: 'delete' | 'update' | 'custom';
table: string;
data: any;
completed: boolean;
}
export interface ExecutionSummary {
totalSteps: number;
successfulSteps: number;
skippedSteps: number;
failedSteps: number;
constraintViolationsFound: number;
autoFixesApplied: number;
duration: number;
}
export declare class ConstraintAwareExecutor {
private client;
private constraintEngine;
private constraints;
constructor(client: SupabaseClient);
/**
* Execute a workflow with full constraint awareness
*/
executeWorkflow(workflow: UserCreationWorkflow, inputData: Record<string, any>): Promise<ExecutionResult>;
/**
* Execute a single workflow step with constraint validation
*/
executeStep(step: WorkflowStep, context: ExecutionContext): Promise<StepResult>;
/**
* Validate step conditions against discovered constraints
*/
validateStepConditions(step: WorkflowStep, context: ExecutionContext): Promise<{
valid: boolean;
violations: ConstraintViolation[];
}>;
/**
* Validate a single condition
*/
private validateCondition;
/**
* Validate EXISTS condition
*/
private validateExistsCondition;
/**
* Validate EQUALS condition
*/
private validateEqualsCondition;
/**
* Validate custom SQL condition
*/
private validateCustomCondition;
/**
* Validate business rule condition
*/
private validateBusinessRuleCondition;
/**
* Validate a business rule
*/
private validateBusinessRule;
/**
* Apply auto-fixes for constraint violations
*/
private applyAutoFixes;
/**
* Apply a single auto-fix
*/
private applyAutoFix;
/**
* Apply set field auto-fix
*/
private applySetFieldFix;
/**
* Execute database operations
*/
private executeInsert;
/**
* Build insert data from field mappings
*/
private buildInsertData;
/**
* Utility methods
*/
private getOrDiscoverConstraints;
private createDummyRule;
private generateValue;
private resolveStepReference;
private logExecutionSummary;
private executeUpdate;
private executeValidation;
private handleStepError;
private preValidateWorkflow;
private postValidateWorkflow;
private rollbackSteps;
private applyCreateDependencyFix;
private applySkipOperationFix;
private applyModifyWorkflowFix;
}
export {};
//# sourceMappingURL=constraint-aware-executor.d.ts.map