import { QueryBuilder, QueryRunner } from 'typeorm'; /** * Base context object passed to plugin hooks */ export interface QueryHookContext { builder: QueryBuilder; sql: string; timestamp: Date; parameters?: any[]; queryType?: 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE' | 'select' | 'insert' | 'update' | 'delete'; } /** * Context for pre-query modification hook * Allows modifying the SQL before execution */ export interface PreQueryContext extends QueryHookContext { /** * Modify the SQL that will be executed * @param newSql - The modified SQL string */ setSql: (newSql: string) => void; /** * Modify the query parameters * @param newParams - The modified parameters array */ setParameters: (newParams: any[]) => void; } /** * Context for query execution hooks */ export interface QueryExecutionContext extends QueryHookContext { executionTime?: number; methodName: string; } /** * Context for query result hooks */ export interface QueryResultContext extends QueryExecutionContext { result: any; rowCount?: number; isEmpty: boolean; } /** * Context for transaction lifecycle hooks */ export interface TransactionContext { queryRunner: QueryRunner; timestamp: Date; transactionId?: string; } /** * Context for transaction completion */ export interface TransactionCompleteContext extends TransactionContext { executionTime: number; queriesExecuted?: string[]; } /** * Context for transaction failure */ export interface TransactionErrorContext extends TransactionCompleteContext { error: Error; } /** * Context for raw SQL queries (executed via QueryRunner) * These queries bypass QueryBuilder and include DDL, raw SQL, migrations, etc. */ export interface RawQueryContext { sql: string; parameters?: any[]; timestamp: Date; queryRunner: QueryRunner; } /** * Context for connection pool events */ export interface ConnectionPoolContext { timestamp: Date; activeConnections?: number; idleConnections?: number; maxConnections?: number; waitingCount?: number; } /** * Generic interface for query hook plugins * Plugins can implement any combination of these hooks */ export interface QueryHookPlugin { /** * Unique name for the plugin */ name: string; /** * Called when the plugin is registered (optional initialization) */ onRegister?: () => void; /** * Called when hooks are enabled (optional setup) */ onEnable?: () => void; /** * Called when a query is built (getQuery is called) * Use this to inspect the query before execution */ onQueryBuild?: (context: QueryHookContext) => void; /** * Called before query execution - allows modification of SQL and parameters * Return false to cancel the query execution */ onBeforeQuery?: (context: PreQueryContext) => boolean | void; /** * Called when query execution starts */ onQueryStart?: (context: QueryExecutionContext) => void; /** * Called when query execution completes successfully */ onQueryComplete?: (context: QueryExecutionContext) => void; /** * Called when query execution fails */ onQueryError?: (context: QueryExecutionContext & { error: Error; }) => void; /** * Called when a slow query is detected (threshold configurable) */ onSlowQuery?: (context: QueryExecutionContext) => void; /** * Called after query execution with results */ onQueryResult?: (context: QueryResultContext) => void; /** * Called when a query returns no results */ onEmptyResult?: (context: QueryResultContext) => void; /** * Called when a query returns a large result set (threshold configurable) */ onLargeResult?: (context: QueryResultContext) => void; /** * Called when a raw SQL query is executed via QueryRunner * This captures DDL, migrations, raw SQL, and other queries that bypass QueryBuilder */ onRawQuery?: (context: RawQueryContext) => void; /** * Called when a raw SQL query completes */ onRawQueryComplete?: (context: RawQueryContext & { executionTime: number; result?: any; }) => void; /** * Called when a raw SQL query fails */ onRawQueryError?: (context: RawQueryContext & { error: Error; }) => void; /** * Called when a transaction starts */ onTransactionStart?: (context: TransactionContext) => void; /** * Called when a transaction commits successfully */ onTransactionCommit?: (context: TransactionCompleteContext) => void; /** * Called when a transaction rolls back */ onTransactionRollback?: (context: TransactionErrorContext) => void; /** * Called when a transaction ends (regardless of commit or rollback) */ onTransactionEnd?: (context: TransactionCompleteContext) => void; /** * Called when a connection is acquired from the pool */ onConnectionAcquired?: (context: ConnectionPoolContext) => void; /** * Called when a connection is released back to the pool */ onConnectionReleased?: (context: ConnectionPoolContext) => void; /** * Called when the connection pool is full (all connections in use) */ onConnectionPoolFull?: (context: ConnectionPoolContext) => void; /** * Called when a connection error occurs */ onConnectionError?: (context: ConnectionPoolContext & { error: Error; }) => void; } /** * Register a plugin to receive query hooks * @param plugin - The plugin to register */ export declare function registerPlugin(plugin: QueryHookPlugin): void; /** * Unregister a plugin by name * @param pluginName - Name of the plugin to remove */ export declare function unregisterPlugin(pluginName: string): boolean; /** * Get all registered plugins */ export declare function getRegisteredPlugins(): ReadonlyArray; /** * Configuration options for enableQueryHooks */ export interface QueryHooksOptions { /** * Enable detailed logging for debugging (default: false) */ verbose?: boolean; } /** * Enable TypeORM query hooks by patching QueryBuilder classes * This should be called once at application startup, before any queries are executed * * @param options Configuration options */ export declare function enableQueryHooks(options?: QueryHooksOptions): void; /** * NOTE: These functions are DISABLED in v6.4.0 because QueryRunner hooks were found to * interfere with TypeORM's internal execution flow, causing crashes and state corruption. * They are commented out to avoid linting errors for unused functions. * * See: Line 673 where patchTransactionHooks() is commented out * * Future versions may implement a safer approach to capturing raw SQL queries. */ /** * Check if hooks are currently enabled */ export declare function isHooksEnabled(): boolean; export * from './plugins/table-extractor'; export * from './plugins/query-logger'; export * from './plugins/query-metadata-registry'; export * from './plugins/query-type-detector'; export * from './plugins/performance-monitor'; export * from './plugins/result-validator'; export * from './plugins/query-modifier'; export * from './plugins/cache-invalidation'; export * from './plugins/audit-logging'; export * from './plugins/bulk-operations'; export * from './plugins/query-complexity'; export * from './plugins/n-plus-one-detector'; export * from './plugins/query-source-tracer'; export * from './plugins/safety-guard'; export * from './plugins/slow-query-analyzer'; export * from './plugins/idle-transaction-monitor'; export * from './plugins/query-timeout'; export * from './plugins/connection-leak-detector'; export * from './plugins/lazy-loading-detector'; export * from './plugins/query-result-transformer'; export * from './nestjs'; export * from './context-store'; //# sourceMappingURL=index.d.ts.map