@plyaz/db 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +169 -0
- package/dist/adapters/drizzle/DrizzleAdapter.d.ts +269 -0
- package/dist/adapters/drizzle/DrizzleAdapter.d.ts.map +1 -0
- package/dist/adapters/index.d.ts +20 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/sql/SQLAdapter.d.ts +282 -0
- package/dist/adapters/sql/SQLAdapter.d.ts.map +1 -0
- package/dist/adapters/supabase/SupabaseAdapter.d.ts +305 -0
- package/dist/adapters/supabase/SupabaseAdapter.d.ts.map +1 -0
- package/dist/advanced/backup/BackupService.d.ts +159 -0
- package/dist/advanced/backup/BackupService.d.ts.map +1 -0
- package/dist/advanced/backup/index.d.ts +2 -0
- package/dist/advanced/backup/index.d.ts.map +1 -0
- package/dist/advanced/caching/CacheEvict.decorator.d.ts +3 -0
- package/dist/advanced/caching/CacheEvict.decorator.d.ts.map +1 -0
- package/dist/advanced/caching/Cacheable.decorator.d.ts +99 -0
- package/dist/advanced/caching/Cacheable.decorator.d.ts.map +1 -0
- package/dist/advanced/caching/RedisCache.d.ts +417 -0
- package/dist/advanced/caching/RedisCache.d.ts.map +1 -0
- package/dist/advanced/caching/index.d.ts +4 -0
- package/dist/advanced/caching/index.d.ts.map +1 -0
- package/dist/advanced/connection-pool/DynamicPool.d.ts +234 -0
- package/dist/advanced/connection-pool/DynamicPool.d.ts.map +1 -0
- package/dist/advanced/connection-pool/index.d.ts +2 -0
- package/dist/advanced/connection-pool/index.d.ts.map +1 -0
- package/dist/advanced/index.d.ts +8 -0
- package/dist/advanced/index.d.ts.map +1 -0
- package/dist/advanced/monitoring/AlertManager.d.ts +72 -0
- package/dist/advanced/monitoring/AlertManager.d.ts.map +1 -0
- package/dist/advanced/monitoring/MetricsCollector.d.ts +81 -0
- package/dist/advanced/monitoring/MetricsCollector.d.ts.map +1 -0
- package/dist/advanced/monitoring/index.d.ts +3 -0
- package/dist/advanced/monitoring/index.d.ts.map +1 -0
- package/dist/advanced/multi-tenancy/TenantContext.d.ts +52 -0
- package/dist/advanced/multi-tenancy/TenantContext.d.ts.map +1 -0
- package/dist/advanced/multi-tenancy/TenantRepository.d.ts +292 -0
- package/dist/advanced/multi-tenancy/TenantRepository.d.ts.map +1 -0
- package/dist/advanced/multi-tenancy/index.d.ts +3 -0
- package/dist/advanced/multi-tenancy/index.d.ts.map +1 -0
- package/dist/advanced/read-replica/ReadReplicaAdapter.d.ts +516 -0
- package/dist/advanced/read-replica/ReadReplicaAdapter.d.ts.map +1 -0
- package/dist/advanced/read-replica/ReadReplicaManager.d.ts +68 -0
- package/dist/advanced/read-replica/ReadReplicaManager.d.ts.map +1 -0
- package/dist/advanced/read-replica/UseReplica.decorator.d.ts +24 -0
- package/dist/advanced/read-replica/UseReplica.decorator.d.ts.map +1 -0
- package/dist/advanced/read-replica/index.d.ts +3 -0
- package/dist/advanced/read-replica/index.d.ts.map +1 -0
- package/dist/advanced/sharding/ShardKey.d.ts +80 -0
- package/dist/advanced/sharding/ShardKey.d.ts.map +1 -0
- package/dist/advanced/sharding/ShardRouter.d.ts +66 -0
- package/dist/advanced/sharding/ShardRouter.d.ts.map +1 -0
- package/dist/advanced/sharding/index.d.ts +3 -0
- package/dist/advanced/sharding/index.d.ts.map +1 -0
- package/dist/builder/query/index.d.ts +7 -0
- package/dist/builder/query/index.d.ts.map +1 -0
- package/dist/builder/query/orm.d.ts +22 -0
- package/dist/builder/query/orm.d.ts.map +1 -0
- package/dist/builder/query/sql.d.ts +29 -0
- package/dist/builder/query/sql.d.ts.map +1 -0
- package/dist/extensions/AuditExtension.d.ts +468 -0
- package/dist/extensions/AuditExtension.d.ts.map +1 -0
- package/dist/extensions/CachingAdapter.d.ts +451 -0
- package/dist/extensions/CachingAdapter.d.ts.map +1 -0
- package/dist/extensions/EncryptionExtension.d.ts +95 -0
- package/dist/extensions/EncryptionExtension.d.ts.map +1 -0
- package/dist/extensions/ReadReplicaAdapter.d.ts +32 -0
- package/dist/extensions/ReadReplicaAdapter.d.ts.map +1 -0
- package/dist/extensions/SoftDeleteExtension.d.ts +430 -0
- package/dist/extensions/SoftDeleteExtension.d.ts.map +1 -0
- package/dist/extensions/index.d.ts +79 -0
- package/dist/extensions/index.d.ts.map +1 -0
- package/dist/factory/AdapterFactory.d.ts +111 -0
- package/dist/factory/AdapterFactory.d.ts.map +1 -0
- package/dist/factory/createDatabaseService.d.ts +121 -0
- package/dist/factory/createDatabaseService.d.ts.map +1 -0
- package/dist/index.cjs +8518 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +8480 -0
- package/dist/index.mjs.map +1 -0
- package/dist/repository/BaseRepository.d.ts +209 -0
- package/dist/repository/BaseRepository.d.ts.map +1 -0
- package/dist/repository/index.d.ts +80 -0
- package/dist/repository/index.d.ts.map +1 -0
- package/dist/security/index.cjs +118 -0
- package/dist/security/index.cjs.map +1 -0
- package/dist/security/index.d.ts +3 -0
- package/dist/security/index.d.ts.map +1 -0
- package/dist/security/index.mjs +114 -0
- package/dist/security/index.mjs.map +1 -0
- package/dist/security/sanitizers/html.sanitizer.d.ts +31 -0
- package/dist/security/sanitizers/html.sanitizer.d.ts.map +1 -0
- package/dist/security/serializers/DataValidation.d.ts +34 -0
- package/dist/security/serializers/DataValidation.d.ts.map +1 -0
- package/dist/service/DatabaseService.d.ts +136 -0
- package/dist/service/DatabaseService.d.ts.map +1 -0
- package/dist/service/EventEmitter.d.ts +110 -0
- package/dist/service/EventEmitter.d.ts.map +1 -0
- package/dist/service/HealthManager.d.ts +166 -0
- package/dist/service/HealthManager.d.ts.map +1 -0
- package/dist/utils/ConfigMerger.d.ts +227 -0
- package/dist/utils/ConfigMerger.d.ts.map +1 -0
- package/dist/utils/databaseResultHelpers.d.ts +98 -0
- package/dist/utils/databaseResultHelpers.d.ts.map +1 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/normalizeDetails.d.ts +111 -0
- package/dist/utils/normalizeDetails.d.ts.map +1 -0
- package/dist/utils/pagination.d.ts +77 -0
- package/dist/utils/pagination.d.ts.map +1 -0
- package/dist/utils/regex.d.ts +199 -0
- package/dist/utils/regex.d.ts.map +1 -0
- package/dist/utils/typeGuards.d.ts +57 -0
- package/dist/utils/typeGuards.d.ts.map +1 -0
- package/dist/utils/validation.d.ts +146 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/package.json +156 -0
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
import type { DatabaseAdapterType, DatabaseResult, Filter, DatabaseHealthStatus, PaginatedResult, QueryOptions, Transaction, AuditContext, AuditEvent } from "@plyaz/types/db";
|
|
2
|
+
/**
|
|
3
|
+
* AUDIT ADAPTER - Compliance Logging Layer
|
|
4
|
+
*
|
|
5
|
+
* Audit extension that automatically logs all database operations for compliance.
|
|
6
|
+
* Fifth layer in the adapter chain (second from outermost).
|
|
7
|
+
*
|
|
8
|
+
* **Adapter Chain Position:**
|
|
9
|
+
* ReadReplica � **Audit** � Cache � SoftDelete � Encryption � Base Adapter
|
|
10
|
+
*
|
|
11
|
+
* **What this adapter does:**
|
|
12
|
+
* 1. Captures before-state for update/delete operations
|
|
13
|
+
* 2. Delegates to next adapter in chain
|
|
14
|
+
* 3. Captures after-state and writes audit record to daily audit tables
|
|
15
|
+
* 4. Calls onAuditAfterWrite event handler (if configured)
|
|
16
|
+
* 5. Honors skipAudit flag from operation config
|
|
17
|
+
*
|
|
18
|
+
* **Called by:** ReadReplicaAdapter (or DatabaseService if no read replicas)
|
|
19
|
+
* **Calls:** CachingAdapter (or next adapter in chain)
|
|
20
|
+
* **Writes to:** Daily audit tables (audit_YYYYMMDD)
|
|
21
|
+
*
|
|
22
|
+
* **Audit Flow:**
|
|
23
|
+
* 1. **CREATE:** Records after-state only
|
|
24
|
+
* 2. **UPDATE:** Records before-state, after-state, and changed fields
|
|
25
|
+
* 3. **DELETE:** Records before-state only
|
|
26
|
+
* 4. **SOFT_DELETE:** Records before/after state with deletedAt change
|
|
27
|
+
*
|
|
28
|
+
* **Audit Context:** Uses context from DatabaseService.setAuditContext()
|
|
29
|
+
* - userId, requestId, ipAddress, userAgent
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ### Configuration
|
|
33
|
+
* ```typescript
|
|
34
|
+
* audit: {
|
|
35
|
+
* enabled: true,
|
|
36
|
+
* retentionDays: 90,
|
|
37
|
+
* excludeFields: ['password', 'token'],
|
|
38
|
+
* excludeTables: ['temp_data'],
|
|
39
|
+
* onAuditAfterWrite: async (event) => {
|
|
40
|
+
* await complianceService.recordAudit(event);
|
|
41
|
+
* }
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ### Audit Record Structure
|
|
47
|
+
* ```typescript
|
|
48
|
+
* // Written to audit_20241201 table
|
|
49
|
+
* {
|
|
50
|
+
* operation: 'UPDATE',
|
|
51
|
+
* table: 'users',
|
|
52
|
+
* recordId: 'user-123',
|
|
53
|
+
* userId: 'admin-456',
|
|
54
|
+
* requestId: 'req-789',
|
|
55
|
+
* changes: {
|
|
56
|
+
* before: { name: 'John Doe', email: 'john@old.com' },
|
|
57
|
+
* after: { name: 'John Smith', email: 'john@new.com' },
|
|
58
|
+
* fields: ['name', 'email']
|
|
59
|
+
* },
|
|
60
|
+
* timestamp: '2024-12-01T10:30:00Z',
|
|
61
|
+
* ipAddress: '192.168.1.1',
|
|
62
|
+
* userAgent: 'Mozilla/5.0...'
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare class AuditAdapter implements DatabaseAdapterType {
|
|
67
|
+
private baseAdapter;
|
|
68
|
+
private config;
|
|
69
|
+
private auditContext;
|
|
70
|
+
/**
|
|
71
|
+
* Creates a new AuditAdapter instance.
|
|
72
|
+
*
|
|
73
|
+
* **RESPONSIBILITY:** Wraps base adapter with audit logging functionality
|
|
74
|
+
* **CONFIGURATION:** Sets up audit rules, retention, and event handlers
|
|
75
|
+
*
|
|
76
|
+
* @param baseAdapter - The underlying database adapter to wrap
|
|
77
|
+
* @param config - Audit configuration options
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* const auditAdapter = new AuditAdapter(baseAdapter, {
|
|
82
|
+
* enabled: true,
|
|
83
|
+
* retentionDays: 90,
|
|
84
|
+
* excludeFields: ['password', 'token'],
|
|
85
|
+
* excludeTables: ['temp_data'],
|
|
86
|
+
* onAuditAfterWrite: async (event) => {
|
|
87
|
+
* await complianceService.recordAudit(event);
|
|
88
|
+
* }
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
constructor(baseAdapter: DatabaseAdapterType, config: {
|
|
93
|
+
enabled: boolean;
|
|
94
|
+
retentionDays?: number;
|
|
95
|
+
excludeFields?: string[];
|
|
96
|
+
excludeTables?: string[];
|
|
97
|
+
onAuditAfterWrite?: (event: AuditEvent) => void | Promise<void>;
|
|
98
|
+
});
|
|
99
|
+
/**
|
|
100
|
+
* Initializes the audit adapter and underlying adapter.
|
|
101
|
+
*
|
|
102
|
+
* **RESPONSIBILITY:** Passes initialization to base adapter
|
|
103
|
+
* **BEHAVIOR:** No additional initialization needed for audit
|
|
104
|
+
*
|
|
105
|
+
* @returns Promise resolving to initialization result
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```typescript
|
|
109
|
+
* const result = await auditAdapter.initialize();
|
|
110
|
+
* if (result.success) {
|
|
111
|
+
* console.log('Audit adapter initialized');
|
|
112
|
+
* }
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
initialize(): Promise<DatabaseResult<void>>;
|
|
116
|
+
/**
|
|
117
|
+
* Establishes database connection through base adapter.
|
|
118
|
+
*
|
|
119
|
+
* **RESPONSIBILITY:** Delegates connection to underlying adapter
|
|
120
|
+
* **BEHAVIOR:** No additional connection logic needed
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* await auditAdapter.connect();
|
|
125
|
+
* console.log('Connected with audit support');
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
connect(): Promise<void>;
|
|
129
|
+
/**
|
|
130
|
+
* Closes database connection through base adapter.
|
|
131
|
+
*
|
|
132
|
+
* **RESPONSIBILITY:** Delegates disconnection to underlying adapter
|
|
133
|
+
* **BEHAVIOR:** No additional cleanup needed for audit
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* await auditAdapter.disconnect();
|
|
138
|
+
* console.log('Disconnected gracefully');
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
disconnect(): Promise<void>;
|
|
142
|
+
/**
|
|
143
|
+
* Gets the underlying database client.
|
|
144
|
+
*
|
|
145
|
+
* **RESPONSIBILITY:** Provides access to raw database client
|
|
146
|
+
* **USE CASE:** For operations that bypass audit logging
|
|
147
|
+
*
|
|
148
|
+
* @returns Database client object
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* const client = auditAdapter.getClient();
|
|
153
|
+
* // Use for direct database operations if needed
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
getClient(): object;
|
|
157
|
+
/**
|
|
158
|
+
* Executes raw SQL query through base adapter.
|
|
159
|
+
*
|
|
160
|
+
* **RESPONSIBILITY:** Passes raw SQL to base adapter without audit logging
|
|
161
|
+
* **BEHAVIOR:** Does not audit raw SQL operations
|
|
162
|
+
* **NOTE:** Use CRUD methods for automatic audit logging
|
|
163
|
+
*
|
|
164
|
+
* @param sql - SQL query string
|
|
165
|
+
* @param params - Query parameters
|
|
166
|
+
* @returns Query results
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* // Raw SQL bypasses audit logging
|
|
171
|
+
* const results = await adapter.query(
|
|
172
|
+
* 'SELECT * FROM users WHERE status = $1',
|
|
173
|
+
* ['active']
|
|
174
|
+
* );
|
|
175
|
+
*
|
|
176
|
+
* // Use findMany for automatic audit logging
|
|
177
|
+
* const users = await adapter.findMany('users', { filter: ... });
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
query<T>(sql: string, params?: T[]): Promise<T[]>;
|
|
181
|
+
/**
|
|
182
|
+
* Registers a table schema with the base adapter.
|
|
183
|
+
*
|
|
184
|
+
* **RESPONSIBILITY:** Passes table registration to base adapter
|
|
185
|
+
* **BEHAVIOR:** No additional registration logic needed
|
|
186
|
+
*
|
|
187
|
+
* @param name - Table name
|
|
188
|
+
* @param table - Table schema
|
|
189
|
+
* @param idColumn - Primary key column
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```typescript
|
|
193
|
+
* auditAdapter.registerTable('users', userSchema, 'id');
|
|
194
|
+
* // Table now supports audited operations
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
registerTable<T, U>(name: string, table: T, idColumn?: U): void;
|
|
198
|
+
/**
|
|
199
|
+
* Sets audit context for tracking user actions.
|
|
200
|
+
*
|
|
201
|
+
* **RESPONSIBILITY:** Stores context information for audit records
|
|
202
|
+
* **CONTEXT:** userId, requestId, ipAddress, userAgent
|
|
203
|
+
* **USAGE:** Called by DatabaseService.setAuditContext()
|
|
204
|
+
*
|
|
205
|
+
* @param context - Audit context information
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```typescript
|
|
209
|
+
* // Usually called by middleware
|
|
210
|
+
* auditAdapter.setAuditContext({
|
|
211
|
+
* userId: 'user-123',
|
|
212
|
+
* requestId: 'req-456',
|
|
213
|
+
* ipAddress: '192.168.1.1',
|
|
214
|
+
* userAgent: 'Mozilla/5.0...'
|
|
215
|
+
* });
|
|
216
|
+
*
|
|
217
|
+
* // Subsequent operations will include this context in audit logs
|
|
218
|
+
* await adapter.create('users', userData); // Audited with context
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
setAuditContext(context: AuditContext): void;
|
|
222
|
+
/**
|
|
223
|
+
* Finds a record by ID without audit logging.
|
|
224
|
+
*
|
|
225
|
+
* **RESPONSIBILITY:** Retrieves single record without creating audit trail
|
|
226
|
+
* **BEHAVIOR:** Read operations are not audited by default
|
|
227
|
+
* **PERFORMANCE:** No audit overhead for read operations
|
|
228
|
+
*
|
|
229
|
+
* @param table - Table name
|
|
230
|
+
* @param id - Record ID
|
|
231
|
+
* @returns Found record or null
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* ```typescript
|
|
235
|
+
* // Read operations don't create audit records
|
|
236
|
+
* const user = await adapter.findById('users', 'user-123');
|
|
237
|
+
* if (user.success && user.value) {
|
|
238
|
+
* console.log('User found:', user.value.name);
|
|
239
|
+
* }
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
findById<T>(table: string, id: string): Promise<DatabaseResult<T | null>>;
|
|
243
|
+
/**
|
|
244
|
+
* Finds multiple records without audit logging.
|
|
245
|
+
*
|
|
246
|
+
* **RESPONSIBILITY:** Retrieves multiple records without creating audit trail
|
|
247
|
+
* **BEHAVIOR:** Read operations are not audited by default
|
|
248
|
+
* **PERFORMANCE:** No audit overhead for read operations
|
|
249
|
+
*
|
|
250
|
+
* @param table - Table name
|
|
251
|
+
* @param options - Query options
|
|
252
|
+
* @returns Paginated results
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```typescript
|
|
256
|
+
* // Read operations don't create audit records
|
|
257
|
+
* const users = await adapter.findMany('users', {
|
|
258
|
+
* filter: { field: 'status', operator: 'eq', value: 'active' },
|
|
259
|
+
* pagination: { page: 1, limit: 10 }
|
|
260
|
+
* });
|
|
261
|
+
*
|
|
262
|
+
* console.log('Found users:', users.value?.data.length);
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
findMany<T>(table: string, options?: QueryOptions): Promise<DatabaseResult<PaginatedResult<T>>>;
|
|
266
|
+
create<T>(table: string, data: T): Promise<DatabaseResult<T>>;
|
|
267
|
+
private validateCreateParams;
|
|
268
|
+
private handleCreateAudit;
|
|
269
|
+
update<T>(table: string, id: string, data: Partial<T>): Promise<DatabaseResult<T>>;
|
|
270
|
+
delete(table: string, id: string): Promise<DatabaseResult<void>>;
|
|
271
|
+
/**
|
|
272
|
+
* Executes operations within a transaction with audit logging.
|
|
273
|
+
*
|
|
274
|
+
* **RESPONSIBILITY:** Passes transaction to base adapter
|
|
275
|
+
* **BEHAVIOR:** Individual operations within transaction are audited
|
|
276
|
+
* **ATOMICITY:** Audit records are part of the transaction
|
|
277
|
+
*
|
|
278
|
+
* @param callback - Transaction callback function
|
|
279
|
+
* @returns Transaction result
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```typescript
|
|
283
|
+
* const result = await adapter.transaction(async (trx) => {
|
|
284
|
+
* // Each operation creates audit record within transaction
|
|
285
|
+
* const user = await trx.create('users', { name: 'John' });
|
|
286
|
+
* await trx.update('profiles', 'profile-1', { userId: user.id });
|
|
287
|
+
* return user;
|
|
288
|
+
* });
|
|
289
|
+
*
|
|
290
|
+
* // If transaction fails, audit records are also rolled back
|
|
291
|
+
* ```
|
|
292
|
+
*/
|
|
293
|
+
transaction<T>(callback: (trx: Transaction) => Promise<T>): Promise<DatabaseResult<T>>;
|
|
294
|
+
/**
|
|
295
|
+
* Checks if a record exists without audit logging.
|
|
296
|
+
*
|
|
297
|
+
* **RESPONSIBILITY:** Verifies record existence without creating audit trail
|
|
298
|
+
* **BEHAVIOR:** Read operations are not audited by default
|
|
299
|
+
* **PERFORMANCE:** No audit overhead for existence checks
|
|
300
|
+
*
|
|
301
|
+
* @param table - Table name
|
|
302
|
+
* @param id - Record ID
|
|
303
|
+
* @returns True if record exists
|
|
304
|
+
*
|
|
305
|
+
* @example
|
|
306
|
+
* ```typescript
|
|
307
|
+
* // Existence checks don't create audit records
|
|
308
|
+
* const userExists = await adapter.exists('users', 'user-123');
|
|
309
|
+
* if (userExists.value) {
|
|
310
|
+
* console.log('User exists');
|
|
311
|
+
* }
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
exists(table: string, id: string): Promise<DatabaseResult<boolean>>;
|
|
315
|
+
/**
|
|
316
|
+
* Counts records without audit logging.
|
|
317
|
+
*
|
|
318
|
+
* **RESPONSIBILITY:** Counts records without creating audit trail
|
|
319
|
+
* **BEHAVIOR:** Read operations are not audited by default
|
|
320
|
+
* **PERFORMANCE:** No audit overhead for counting operations
|
|
321
|
+
*
|
|
322
|
+
* @param table - Table name
|
|
323
|
+
* @param filter - Optional filter conditions
|
|
324
|
+
* @returns Count of records
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```typescript
|
|
328
|
+
* // Count operations don't create audit records
|
|
329
|
+
* const activeUsers = await adapter.count('users', {
|
|
330
|
+
* field: 'status',
|
|
331
|
+
* operator: 'eq',
|
|
332
|
+
* value: 'active'
|
|
333
|
+
* });
|
|
334
|
+
*
|
|
335
|
+
* console.log('Active users:', activeUsers.value);
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
338
|
+
count(table: string, filter?: Filter): Promise<DatabaseResult<number>>;
|
|
339
|
+
/**
|
|
340
|
+
* Performs health check through base adapter.
|
|
341
|
+
*
|
|
342
|
+
* **RESPONSIBILITY:** Delegates health check to underlying adapter
|
|
343
|
+
* **BEHAVIOR:** No additional health metrics for audit
|
|
344
|
+
*
|
|
345
|
+
* @returns Health status from base adapter
|
|
346
|
+
*
|
|
347
|
+
* @example
|
|
348
|
+
* ```typescript
|
|
349
|
+
* const health = await adapter.healthCheck();
|
|
350
|
+
* if (health.success && health.value?.isHealthy) {
|
|
351
|
+
* console.log('Database healthy with audit support');
|
|
352
|
+
* }
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
355
|
+
healthCheck(): Promise<DatabaseResult<DatabaseHealthStatus>>;
|
|
356
|
+
/**
|
|
357
|
+
* Determines if a table should be audited.
|
|
358
|
+
*
|
|
359
|
+
* **RESPONSIBILITY:** Checks if table is enabled for audit logging
|
|
360
|
+
* **CONFIGURATION:** Based on enabled flag and excludeTables array
|
|
361
|
+
*
|
|
362
|
+
* @private
|
|
363
|
+
* @param table - Table name to check
|
|
364
|
+
* @returns True if table should be audited
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```typescript
|
|
368
|
+
* // Configuration: { enabled: true, excludeTables: ['temp_data'] }
|
|
369
|
+
*
|
|
370
|
+
* this.shouldAudit('users'); // true - will be audited
|
|
371
|
+
* this.shouldAudit('temp_data'); // false - excluded from audit
|
|
372
|
+
* ```
|
|
373
|
+
*/
|
|
374
|
+
private shouldAudit;
|
|
375
|
+
/**
|
|
376
|
+
* Logs an audit event to the audit table and executes custom handlers.
|
|
377
|
+
*
|
|
378
|
+
* **RESPONSIBILITY:** Orchestrates audit record creation and event handling
|
|
379
|
+
* **PROCESS:** Validates event → Writes to audit table → Executes custom handler
|
|
380
|
+
*
|
|
381
|
+
* @private
|
|
382
|
+
* @param event - Audit event to log
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* ```typescript
|
|
386
|
+
* // Internal usage after successful operation
|
|
387
|
+
* await this.logAudit({
|
|
388
|
+
* operation: 'CREATE',
|
|
389
|
+
* table: 'users',
|
|
390
|
+
* recordId: 'user-123',
|
|
391
|
+
* changes: { after: userData },
|
|
392
|
+
* userId: 'admin-456',
|
|
393
|
+
* timestamp: new Date()
|
|
394
|
+
* });
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
private logAudit;
|
|
398
|
+
/**
|
|
399
|
+
* Validates audit event has required fields.
|
|
400
|
+
*
|
|
401
|
+
* **RESPONSIBILITY:** Ensures audit event is properly formed
|
|
402
|
+
* **VALIDATION:** Checks for required operation and table fields
|
|
403
|
+
*
|
|
404
|
+
* @private
|
|
405
|
+
* @param event - Audit event to validate
|
|
406
|
+
* @throws {DatabaseError} When event is invalid
|
|
407
|
+
*
|
|
408
|
+
* @example
|
|
409
|
+
* ```typescript
|
|
410
|
+
* // Internal validation before logging
|
|
411
|
+
* this.validateAuditEvent({
|
|
412
|
+
* operation: 'CREATE', // Required
|
|
413
|
+
* table: 'users', // Required
|
|
414
|
+
* recordId: 'user-123'
|
|
415
|
+
* });
|
|
416
|
+
* ```
|
|
417
|
+
*/
|
|
418
|
+
private validateAuditEvent;
|
|
419
|
+
/**
|
|
420
|
+
* Writes audit record to daily audit table.
|
|
421
|
+
*
|
|
422
|
+
* **RESPONSIBILITY:** Persists audit event to database
|
|
423
|
+
* **TABLE NAMING:** Uses daily tables (audit_YYYYMMDD)
|
|
424
|
+
* **STRUCTURE:** Converts event to database record format
|
|
425
|
+
*
|
|
426
|
+
* @private
|
|
427
|
+
* @param event - Audit event to write
|
|
428
|
+
* @throws {DatabaseError} When write operation fails
|
|
429
|
+
*
|
|
430
|
+
* @example
|
|
431
|
+
* ```typescript
|
|
432
|
+
* // Internal usage - writes to audit_20241201 table
|
|
433
|
+
* await this.writeAuditRecord({
|
|
434
|
+
* operation: 'UPDATE',
|
|
435
|
+
* table: 'users',
|
|
436
|
+
* recordId: 'user-123',
|
|
437
|
+
* changes: { before: {...}, after: {...} },
|
|
438
|
+
* timestamp: new Date('2024-12-01')
|
|
439
|
+
* });
|
|
440
|
+
* ```
|
|
441
|
+
*/
|
|
442
|
+
private writeAuditRecord;
|
|
443
|
+
/**
|
|
444
|
+
* Executes custom audit event handler if configured.
|
|
445
|
+
*
|
|
446
|
+
* **RESPONSIBILITY:** Calls user-defined audit event handler
|
|
447
|
+
* **ERROR HANDLING:** Logs handler errors without failing the operation
|
|
448
|
+
* **USE CASE:** Integration with compliance systems, notifications
|
|
449
|
+
*
|
|
450
|
+
* @private
|
|
451
|
+
* @param event - Audit event to handle
|
|
452
|
+
*
|
|
453
|
+
* @example
|
|
454
|
+
* ```typescript
|
|
455
|
+
* // Configuration with custom handler
|
|
456
|
+
* {
|
|
457
|
+
* onAuditAfterWrite: async (event) => {
|
|
458
|
+
* await complianceService.recordAudit(event);
|
|
459
|
+
* await notificationService.sendAuditAlert(event);
|
|
460
|
+
* }
|
|
461
|
+
* }
|
|
462
|
+
*
|
|
463
|
+
* // Handler is called after successful audit record write
|
|
464
|
+
* ```
|
|
465
|
+
*/
|
|
466
|
+
private executeCustomHandler;
|
|
467
|
+
}
|
|
468
|
+
//# sourceMappingURL=AuditExtension.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuditExtension.d.ts","sourceRoot":"","sources":["../../src/extensions/AuditExtension.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,mBAAmB,EACnB,cAAc,EACd,MAAM,EACN,oBAAoB,EACpB,eAAe,EACf,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,UAAU,EACX,MAAM,iBAAiB,CAAC;AAGzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,qBAAa,YAAa,YAAW,mBAAmB;IA2BpD,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,MAAM;IA3BhB,OAAO,CAAC,YAAY,CAAoB;IAGxC;;;;;;;;;;;;;;;;;;;;;OAqBG;gBAEO,WAAW,EAAE,mBAAmB,EAChC,MAAM,EAAE;QACd,OAAO,EAAE,OAAO,CAAC;QACjB,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;QACzB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;QACzB,iBAAiB,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;KACjE;IAGH;;;;;;;;;;;;;;;OAeG;IACG,UAAU,IAAI,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;IAIjD;;;;;;;;;;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