import { QueryHookPlugin } from '../index'; /** * Audit log entry structure */ export interface AuditLogEntry { timestamp: Date; userId?: string | number; action: 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE' | string; tables: string[]; sql: string; parameters?: any[]; executionTime?: number; success: boolean; error?: string; metadata?: Record; } /** * Options for AuditLoggingPlugin */ export interface AuditLoggingOptions { /** * Callback to log audit entries (required) * This function should persist the audit log (database, file, external service, etc.) * * @param entry - The audit log entry to persist * * @example * ```typescript * onAudit: async (entry) => { * await auditLogRepository.save({ * userId: entry.userId, * action: entry.action, * tables: entry.tables.join(', '), * timestamp: entry.timestamp * }); * } * ``` */ onAudit: (entry: AuditLogEntry) => void | Promise; /** * Function to get current user ID for audit logs (default: undefined) * Should return the currently authenticated user's ID * * @example () => getCurrentUser()?.id */ getUserId?: () => string | number | undefined; /** * Query types to audit (default: ['INSERT', 'UPDATE', 'DELETE'] - only write operations) * Specify which query types should be logged * * @example ['INSERT', 'UPDATE', 'DELETE', 'SELECT'] - Audit all queries including reads */ auditTypes?: Array<'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE' | 'select' | 'insert' | 'update' | 'delete'>; /** * Specific tables to audit (default: [] - all tables) * If empty array, all tables will be audited * If provided, only these tables will be audited * * @example ['users', 'financial_records', 'sensitive_data'] - Only audit sensitive tables */ auditTables?: string[]; /** * Include SQL query in audit log (default: true) * When false, only table names and action are logged (better for privacy) */ includeSql?: boolean; /** * Include query parameters in audit log (default: false) * When true, logs the actual values used in the query (may contain sensitive data) */ includeParameters?: boolean; /** * Additional metadata to include in audit logs (default: undefined) * Can be static object or function that returns dynamic metadata * * @example () => ({ environment: process.env.NODE_ENV, serverIp: getServerIP() }) */ metadata?: Record | (() => Record); /** * Enable console logging for this plugin (default: false) * When true, logs audit entries to console */ enableLogging?: boolean; } /** * Plugin for comprehensive audit logging of database operations * Tracks who did what, when, and on which tables * * 🔥 Priority: HIGH - Critical for compliance, security, and debugging * * @example * ```typescript * import { enableQueryHooks, registerPlugin } from 'typeorm-query-hooks'; * import { AuditLoggingPlugin } from 'typeorm-query-hooks/plugins/audit-logging'; * * enableQueryHooks(); * * // Basic usage: Audit all write operations * registerPlugin(AuditLoggingPlugin({ * getUserId: () => getCurrentUser()?.id, * onAudit: async (entry) => { * await auditLogRepository.save(entry); * } * })); * * // Advanced: Audit specific sensitive tables including reads * registerPlugin(AuditLoggingPlugin({ * auditTypes: ['SELECT', 'INSERT', 'UPDATE', 'DELETE'], // Audit everything * auditTables: ['users', 'financial_records', 'sensitive_data'], * includeSql: true, * includeParameters: false, // Don't log parameters (security) * getUserId: () => getCurrentUser()?.id, * metadata: () => ({ * environment: process.env.NODE_ENV, * serverIp: getServerIP(), * requestId: getRequestId() * }), * enableLogging: true, * onAudit: async (entry) => { * // Save to database * await auditLogRepository.save(entry); * * // Also send to SIEM for security monitoring * if (entry.tables.includes('financial_records')) { * await siem.sendEvent({ * type: 'database_access', * severity: 'high', * data: entry * }); * } * } * })); * ``` */ export declare function AuditLoggingPlugin(options: AuditLoggingOptions): QueryHookPlugin; //# sourceMappingURL=audit-logging.d.ts.map