import { QueryHookPlugin, QueryExecutionContext } from '../index'; /** * Options for QueryTimeoutPlugin */ export interface QueryTimeoutOptions { /** * Default timeout in milliseconds for all queries (default: 5000ms) */ defaultTimeout?: number; /** * Timeout overrides by query type (optional) * * @example { 'SELECT': 3000, 'INSERT': 10000 } */ timeoutByType?: Record; /** * Timeout overrides by table name pattern (optional) * Uses regex matching * * @example { 'report_.*': 30000 } - Reports can take 30s */ timeoutByTablePattern?: Record; /** * Callback when query times out (optional) * * @param context - Query execution context * @param timeout - The timeout value that was exceeded * * @example * ```typescript * onTimeout: (context, timeout) => { * logger.error(`Query timeout: ${timeout}ms`, { * sql: context.sql, * queryType: context.queryType * }); * } * ``` */ onTimeout?: (context: QueryExecutionContext, timeout: number) => void; /** * Callback when query is approaching timeout (optional) * Triggered when query has used 80% of allowed time * * @param context - Query execution context * @param elapsed - Time elapsed so far in milliseconds * @param limit - Total timeout limit in milliseconds * * @example * ```typescript * onTimeoutWarning: (context, elapsed, limit) => { * logger.warn(`Query running slow: ${elapsed}ms / ${limit}ms`, { * sql: context.sql.substring(0, 100) * }); * } * ``` */ onTimeoutWarning?: (context: QueryExecutionContext, elapsed: number, limit: number) => void; /** * Callback when an error occurs in timeout mechanism (optional) * * @param context - Query execution context * @param error - The error that occurred * * @example * ```typescript * onError: (context, error) => { * logger.error('Timeout mechanism failed', { error, sql: context.sql }); * } * ``` */ onError?: (context: QueryExecutionContext, error: Error) => void; /** * Throw error on timeout (default: true) * When true, throws error to cancel query * When false, just logs warning */ throwOnTimeout?: boolean; /** * Warning threshold as percentage of timeout (default: 0.8 / 80%) * When query reaches this percentage of timeout, onTimeoutWarning is triggered */ warningThreshold?: number; /** * Enable console logging for this plugin (default: false) */ enableLogging?: boolean; } /** * Plugin for managing query timeouts * Prevents long-running queries from blocking operations * * ⏱️ Solves: Queries that hang forever, blocking connection pool * * @example * ```typescript * import { QueryTimeoutPlugin } from 'typeorm-query-hooks/plugins/query-timeout'; * * registerPlugin(QueryTimeoutPlugin({ * defaultTimeout: 5000, * timeoutByType: { * 'SELECT': 3000, * 'INSERT': 10000, * 'UPDATE': 10000 * }, * timeoutByTablePattern: { * 'report_.*': 30000, // Reports can be slower * 'analytics_.*': 60000 * }, * throwOnTimeout: true, * onTimeout: (context, timeout) => { * logger.error(`Query timeout after ${timeout}ms`, { * sql: context.sql.substring(0, 200) * }); * } * })); * ``` */ export declare function QueryTimeoutPlugin(options?: QueryTimeoutOptions): QueryHookPlugin; //# sourceMappingURL=query-timeout.d.ts.map