import { QueryHookPlugin, PreQueryContext } from '../index'; /** * Options for QueryModifierPlugin */ export interface QueryModifierOptions { /** * Callback to modify SQL before execution * Return modified SQL or undefined to keep original */ modifySql?: (context: PreQueryContext) => string | undefined | void; /** * Callback to modify parameters before execution * Return modified parameters or undefined to keep original */ modifyParameters?: (context: PreQueryContext) => any[] | undefined | void; /** * Callback to decide if query should be executed * Return false to cancel the query */ shouldExecute?: (context: PreQueryContext) => boolean; /** * Callback when SQL was modified (optional) * Triggered after modifySql changes the query * * @param context - Query context * @param originalSql - Original SQL before modification * @param newSql - Modified SQL * * @example * ```typescript * onSqlModified: (context, originalSql, newSql) => { * logger.info('SQL modified', { * original: originalSql.substring(0, 100), * modified: newSql.substring(0, 100) * }); * } * ``` */ onSqlModified?: (context: PreQueryContext, originalSql: string, newSql: string) => void; /** * Callback when parameters were modified (optional) * Triggered after modifyParameters changes the params * * @param context - Query context * @param originalParams - Original parameters before modification * @param newParams - Modified parameters * * @example * ```typescript * onParametersModified: (context, originalParams, newParams) => { * logger.info('Parameters modified', { * originalCount: originalParams.length, * newCount: newParams.length * }); * } * ``` */ onParametersModified?: (context: PreQueryContext, originalParams: any[], newParams: any[]) => void; /** * Callback when an error occurs during modification (optional) * * @param context - Query context * @param error - The error that occurred * * @example * ```typescript * onError: (context, error) => { * logger.error('Query modification failed', { error, sql: context.sql }); * } * ``` */ onError?: (context: PreQueryContext, error: Error) => void; /** * Enable console logging (default: false) */ enableLogging?: boolean; } /** * Plugin for modifying queries before execution * Allows dynamic SQL transformation, parameter modification, and query cancellation * * @example * ```typescript * import { enableQueryHooks, registerPlugin } from 'typeorm-query-hooks'; * import { QueryModifierPlugin } from 'typeorm-query-hooks/plugins/query-modifier'; * * enableQueryHooks(); * * // Example 1: Add query hints for optimization * registerPlugin(QueryModifierPlugin({ * modifySql: (context) => { * if (context.sql.includes('SELECT') && context.sql.includes('users')) { * // Add index hint * return context.sql.replace('FROM users', 'FROM users USE INDEX (idx_email)'); * } * } * })); * * // Example 2: Inject tenant filtering for multi-tenancy * registerPlugin(QueryModifierPlugin({ * modifySql: (context) => { * const tenantId = getCurrentTenantId(); // Your tenant context * if (context.sql.includes('SELECT') && context.sql.includes('FROM orders')) { * const modified = context.sql.replace( * 'FROM orders', * `FROM orders WHERE tenant_id = ${tenantId}` * ); * return modified; * } * }, * enableLogging: true * })); * * // Example 3: Block dangerous queries in production * registerPlugin(QueryModifierPlugin({ * shouldExecute: (context) => { * if (process.env.NODE_ENV === 'production') { * // Block DELETE without WHERE clause * if (context.sql.match(/DELETE\s+FROM\s+\w+\s*$/i)) { * console.error('Blocked: DELETE without WHERE clause'); * return false; * } * } * return true; * } * })); * ``` */ export declare function QueryModifierPlugin(options?: QueryModifierOptions): QueryHookPlugin; /** * Pre-built modifier: Add tenant filtering for multi-tenancy * * @example * ```typescript * import { TenantFilterModifier } from 'typeorm-query-hooks/plugins/query-modifier'; * * registerPlugin(TenantFilterModifier({ * getTenantId: () => getCurrentUser().tenantId, * tables: ['orders', 'products', 'customers'] * })); * ``` */ export declare function TenantFilterModifier(config: { getTenantId: () => string | number; tables: string[]; tenantColumn?: string; }): QueryHookPlugin; /** * Pre-built modifier: Block dangerous queries in production * * @example * ```typescript * import { SafetyModifier } from 'typeorm-query-hooks/plugins/query-modifier'; * * if (process.env.NODE_ENV === 'production') { * registerPlugin(SafetyModifier()); * } * ``` */ export declare function SafetyModifier(): QueryHookPlugin; //# sourceMappingURL=query-modifier.d.ts.map