All files server/types/executor.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170                                                                                                                                                                                                                                                                                                                                                   
/**
 * Database executor type definitions
 * Interfaces for database execution with different engines
 */
 
import type { SQL } from 'drizzle-orm'
import type { DrizzleDatabase } from './core'
import type { DatabaseAdapter } from '../adapters/base-adapter'
 
/**
 * Database executor interface that wraps Drizzle ORM
 * Provides type-safe SQL execution with engine-specific implementations
 */
export interface DatabaseExecutor {
  /** The Drizzle database instance */
  db: DrizzleDatabase
  /** Optional schema for type inference */
  schema?: any
  /** Database adapter for SQL dialect-specific operations */
  databaseAdapter: DatabaseAdapter
  /** Execute a Drizzle SQL query or query object */
  execute<T = any[]>(query: SQL | any, numericFields?: string[]): Promise<T>
  /** Get the database engine type */
  getEngineType(): 'postgres' | 'mysql' | 'sqlite' | 'singlestore' | 'duckdb'
  /** Execute EXPLAIN on a SQL query to get the execution plan */
  explainQuery(sqlString: string, params: unknown[], options?: ExplainOptions): Promise<ExplainResult>
  /** Get existing indexes for the specified tables */
  getTableIndexes(tableNames: string[]): Promise<IndexInfo[]>
}
 
/**
 * Options for EXPLAIN query execution
 */
export interface ExplainOptions {
  /** Use EXPLAIN ANALYZE to actually execute the query and get real timing (PostgreSQL, MySQL 8.0.18+) */
  analyze?: boolean
}
 
/**
 * A single operation/node in the query execution plan
 * Normalized structure across all databases
 */
export interface ExplainOperation {
  /** Operation type (e.g., 'Seq Scan', 'Index Scan', 'Hash Join', 'Sort') */
  type: string
  /** Table name if applicable */
  table?: string
  /** Index name if used */
  index?: string
  /** Estimated row count from planner */
  estimatedRows?: number
  /** Actual row count (if ANALYZE was used) */
  actualRows?: number
  /** Estimated cost (database-specific units) */
  estimatedCost?: number
  /** Filter condition if any */
  filter?: string
  /** Additional details specific to this operation */
  details?: string
  /** Nested/child operations */
  children?: ExplainOperation[]
}
 
/**
 * Result of an EXPLAIN query
 * Provides both normalized structure and raw output
 */
export interface ExplainResult {
  /** Normalized hierarchical plan as operations */
  operations: ExplainOperation[]
  /** Summary statistics */
  summary: ExplainSummary
  /** Raw EXPLAIN output as text (for display) */
  raw: string
  /** Original SQL query */
  sql: {
    sql: string
    params?: unknown[]
  }
}
 
/**
 * Summary statistics from the execution plan
 */
export interface ExplainSummary {
  /** Database engine type */
  database: 'postgres' | 'mysql' | 'sqlite' | 'duckdb'
  /** Planning time in milliseconds (if available) */
  planningTime?: number
  /** Execution time in milliseconds (if ANALYZE was used) */
  executionTime?: number
  /** Total estimated cost */
  totalCost?: number
  /** Quick flag: true if any sequential scans detected */
  hasSequentialScans: boolean
  /** List of indexes used in the plan */
  usedIndexes: string[]
}
 
/**
 * A recommendation from AI analysis of an execution plan
 */
export interface ExplainRecommendation {
  /** Type of recommendation */
  type: 'index' | 'table' | 'cube' | 'general'
  /** Severity/priority of the recommendation */
  severity: 'critical' | 'warning' | 'suggestion'
  /** Short actionable title */
  title: string
  /** Detailed explanation of why this helps */
  description: string
  /** Actionable SQL statement (e.g., CREATE INDEX) - for index/table recommendations */
  sql?: string
  /** TypeScript code snippet to add to cube definition - for cube recommendations */
  cubeCode?: string
  /** Which cube to modify - for cube recommendations */
  cubeName?: string
  /** Affected database table */
  table?: string
  /** Affected columns */
  columns?: string[]
  /** Expected performance improvement */
  estimatedImpact?: string
}
 
/**
 * Issue identified in the execution plan
 */
export interface ExplainIssue {
  /** Type of issue */
  type: 'sequential_scan' | 'missing_index' | 'high_cost' | 'sort_operation' | string
  /** Description of the issue */
  description: string
  /** Severity level */
  severity: 'high' | 'medium' | 'low'
}
 
/**
 * AI-generated analysis of an execution plan
 */
export interface AIExplainAnalysis {
  /** One-sentence description of what the query does */
  summary: string
  /** Overall performance assessment */
  assessment: 'good' | 'warning' | 'critical'
  /** Reason for the assessment */
  assessmentReason: string
  /** Detailed explanation of the query's purpose and structure */
  queryUnderstanding: string
  /** Issues identified in the execution plan */
  issues: ExplainIssue[]
  /** Actionable recommendations for improvement */
  recommendations: ExplainRecommendation[]
}
 
/**
 * Information about a database index
 */
export interface IndexInfo {
  /** Table the index belongs to */
  table_name: string
  /** Name of the index */
  index_name: string
  /** Columns included in the index (in order) */
  columns: string[]
  /** Whether the index enforces uniqueness */
  is_unique: boolean
  /** Whether this is the primary key index */
  is_primary: boolean
}