import { QueryHookPlugin, QueryHookContext } from '../index'; /** * Source location information */ export interface SourceLocation { file: string; line: number; column: number; function?: string; relativePath: string; } /** * Extended context with source location */ export interface QueryContextWithSource extends QueryHookContext { sourceLocation?: SourceLocation; stackTrace?: string; } /** * Options for QuerySourceTracerPlugin */ export interface QuerySourceTracerOptions { /** * Base path to filter stack traces (default: process.cwd()) * Only show stack frames from this directory * * @example '/src' - Only show files from src directory * @example process.cwd() + '/app' - Only show files from app directory */ basePath?: string; /** * Attach source location to query context (default: true) * Makes context.sourceLocation available to other plugins */ attachToQueryContext?: boolean; /** * Include full stack trace (default: false) * When true, includes complete stack trace, not just the source location */ includeFullStackTrace?: boolean; /** * Paths to ignore in stack traces (default: ['node_modules']) * Patterns to exclude from stack trace analysis * * @example ['node_modules', 'dist', '.webpack'] */ ignorePaths?: string[]; /** * Callback when query is logged with source information (optional) * * @param context - Query context with source location * @param location - Parsed source location * * @example * ```typescript * onQueryLogged: (context, location) => { * logger.info(`Query from ${location.relativePath}:${location.line}`); * } * ``` */ onQueryLogged?: (context: QueryContextWithSource, location: SourceLocation) => void; /** * Enable console logging for this plugin (default: false) * When true, logs source location for every query */ enableLogging?: boolean; } /** * Plugin for tracing query source location in your code * Answers the question: "Which line of code executed this query?" * * 🎯 Solves: TypeORM's default logger shows SQL but not WHERE in your code it came from * * **The Problem:** * You see a slow query in logs: `SELECT * FROM users WHERE email = '...'` * You have 50 places querying users. Which one is slow? You don't know! * * **The Solution:** * Captures stack trace when query is built and shows the exact file:line that caused it * * @example * ```typescript * import { enableQueryHooks, registerPlugin } from 'typeorm-query-hooks'; * import { QuerySourceTracerPlugin } from 'typeorm-query-hooks/plugins/query-source-tracer'; * * enableQueryHooks(); * * // Basic usage: Show source for all queries * registerPlugin(QuerySourceTracerPlugin({ * basePath: process.cwd() + '/src', // Only show files from src/ * enableLogging: true * })); * * // Now your logs show: * // Query: SELECT * FROM users WHERE email = ? * // Source: src/services/UserService.ts:45:12 in UserService.findByEmail * * // Advanced: Integrate with slow query detection * registerPlugin(QuerySourceTracerPlugin({ * basePath: '/src', * attachToQueryContext: true, * onQueryLogged: (context, location) => { * // Available to other plugins via context.sourceLocation * if (context.executionTime && context.executionTime > 1000) { * logger.error('Slow query detected:', { * duration: context.executionTime, * sql: context.sql.substring(0, 100), * source: `${location.relativePath}:${location.line}`, * function: location.function * }); * } * } * })); * * // Combine with PerformanceMonitor: * registerPlugin(PerformanceMonitorPlugin({ * onSlowQuery: (context) => { * const source = (context as QueryContextWithSource).sourceLocation; * console.error(`Slow query in ${source?.relativePath}:${source?.line}`); * } * })); * ``` */ export declare function QuerySourceTracerPlugin(options?: QuerySourceTracerOptions): QueryHookPlugin; /** * Helper to format source location for logging */ export declare function formatSourceLocation(location: SourceLocation | undefined): string; //# sourceMappingURL=query-source-tracer.d.ts.map