@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.
Files changed (118) hide show
  1. package/README.md +169 -0
  2. package/dist/adapters/drizzle/DrizzleAdapter.d.ts +269 -0
  3. package/dist/adapters/drizzle/DrizzleAdapter.d.ts.map +1 -0
  4. package/dist/adapters/index.d.ts +20 -0
  5. package/dist/adapters/index.d.ts.map +1 -0
  6. package/dist/adapters/sql/SQLAdapter.d.ts +282 -0
  7. package/dist/adapters/sql/SQLAdapter.d.ts.map +1 -0
  8. package/dist/adapters/supabase/SupabaseAdapter.d.ts +305 -0
  9. package/dist/adapters/supabase/SupabaseAdapter.d.ts.map +1 -0
  10. package/dist/advanced/backup/BackupService.d.ts +159 -0
  11. package/dist/advanced/backup/BackupService.d.ts.map +1 -0
  12. package/dist/advanced/backup/index.d.ts +2 -0
  13. package/dist/advanced/backup/index.d.ts.map +1 -0
  14. package/dist/advanced/caching/CacheEvict.decorator.d.ts +3 -0
  15. package/dist/advanced/caching/CacheEvict.decorator.d.ts.map +1 -0
  16. package/dist/advanced/caching/Cacheable.decorator.d.ts +99 -0
  17. package/dist/advanced/caching/Cacheable.decorator.d.ts.map +1 -0
  18. package/dist/advanced/caching/RedisCache.d.ts +417 -0
  19. package/dist/advanced/caching/RedisCache.d.ts.map +1 -0
  20. package/dist/advanced/caching/index.d.ts +4 -0
  21. package/dist/advanced/caching/index.d.ts.map +1 -0
  22. package/dist/advanced/connection-pool/DynamicPool.d.ts +234 -0
  23. package/dist/advanced/connection-pool/DynamicPool.d.ts.map +1 -0
  24. package/dist/advanced/connection-pool/index.d.ts +2 -0
  25. package/dist/advanced/connection-pool/index.d.ts.map +1 -0
  26. package/dist/advanced/index.d.ts +8 -0
  27. package/dist/advanced/index.d.ts.map +1 -0
  28. package/dist/advanced/monitoring/AlertManager.d.ts +72 -0
  29. package/dist/advanced/monitoring/AlertManager.d.ts.map +1 -0
  30. package/dist/advanced/monitoring/MetricsCollector.d.ts +81 -0
  31. package/dist/advanced/monitoring/MetricsCollector.d.ts.map +1 -0
  32. package/dist/advanced/monitoring/index.d.ts +3 -0
  33. package/dist/advanced/monitoring/index.d.ts.map +1 -0
  34. package/dist/advanced/multi-tenancy/TenantContext.d.ts +52 -0
  35. package/dist/advanced/multi-tenancy/TenantContext.d.ts.map +1 -0
  36. package/dist/advanced/multi-tenancy/TenantRepository.d.ts +292 -0
  37. package/dist/advanced/multi-tenancy/TenantRepository.d.ts.map +1 -0
  38. package/dist/advanced/multi-tenancy/index.d.ts +3 -0
  39. package/dist/advanced/multi-tenancy/index.d.ts.map +1 -0
  40. package/dist/advanced/read-replica/ReadReplicaAdapter.d.ts +516 -0
  41. package/dist/advanced/read-replica/ReadReplicaAdapter.d.ts.map +1 -0
  42. package/dist/advanced/read-replica/ReadReplicaManager.d.ts +68 -0
  43. package/dist/advanced/read-replica/ReadReplicaManager.d.ts.map +1 -0
  44. package/dist/advanced/read-replica/UseReplica.decorator.d.ts +24 -0
  45. package/dist/advanced/read-replica/UseReplica.decorator.d.ts.map +1 -0
  46. package/dist/advanced/read-replica/index.d.ts +3 -0
  47. package/dist/advanced/read-replica/index.d.ts.map +1 -0
  48. package/dist/advanced/sharding/ShardKey.d.ts +80 -0
  49. package/dist/advanced/sharding/ShardKey.d.ts.map +1 -0
  50. package/dist/advanced/sharding/ShardRouter.d.ts +66 -0
  51. package/dist/advanced/sharding/ShardRouter.d.ts.map +1 -0
  52. package/dist/advanced/sharding/index.d.ts +3 -0
  53. package/dist/advanced/sharding/index.d.ts.map +1 -0
  54. package/dist/builder/query/index.d.ts +7 -0
  55. package/dist/builder/query/index.d.ts.map +1 -0
  56. package/dist/builder/query/orm.d.ts +22 -0
  57. package/dist/builder/query/orm.d.ts.map +1 -0
  58. package/dist/builder/query/sql.d.ts +29 -0
  59. package/dist/builder/query/sql.d.ts.map +1 -0
  60. package/dist/extensions/AuditExtension.d.ts +468 -0
  61. package/dist/extensions/AuditExtension.d.ts.map +1 -0
  62. package/dist/extensions/CachingAdapter.d.ts +451 -0
  63. package/dist/extensions/CachingAdapter.d.ts.map +1 -0
  64. package/dist/extensions/EncryptionExtension.d.ts +95 -0
  65. package/dist/extensions/EncryptionExtension.d.ts.map +1 -0
  66. package/dist/extensions/ReadReplicaAdapter.d.ts +32 -0
  67. package/dist/extensions/ReadReplicaAdapter.d.ts.map +1 -0
  68. package/dist/extensions/SoftDeleteExtension.d.ts +430 -0
  69. package/dist/extensions/SoftDeleteExtension.d.ts.map +1 -0
  70. package/dist/extensions/index.d.ts +79 -0
  71. package/dist/extensions/index.d.ts.map +1 -0
  72. package/dist/factory/AdapterFactory.d.ts +111 -0
  73. package/dist/factory/AdapterFactory.d.ts.map +1 -0
  74. package/dist/factory/createDatabaseService.d.ts +121 -0
  75. package/dist/factory/createDatabaseService.d.ts.map +1 -0
  76. package/dist/index.cjs +8518 -0
  77. package/dist/index.cjs.map +1 -0
  78. package/dist/index.d.ts +19 -0
  79. package/dist/index.d.ts.map +1 -0
  80. package/dist/index.mjs +8480 -0
  81. package/dist/index.mjs.map +1 -0
  82. package/dist/repository/BaseRepository.d.ts +209 -0
  83. package/dist/repository/BaseRepository.d.ts.map +1 -0
  84. package/dist/repository/index.d.ts +80 -0
  85. package/dist/repository/index.d.ts.map +1 -0
  86. package/dist/security/index.cjs +118 -0
  87. package/dist/security/index.cjs.map +1 -0
  88. package/dist/security/index.d.ts +3 -0
  89. package/dist/security/index.d.ts.map +1 -0
  90. package/dist/security/index.mjs +114 -0
  91. package/dist/security/index.mjs.map +1 -0
  92. package/dist/security/sanitizers/html.sanitizer.d.ts +31 -0
  93. package/dist/security/sanitizers/html.sanitizer.d.ts.map +1 -0
  94. package/dist/security/serializers/DataValidation.d.ts +34 -0
  95. package/dist/security/serializers/DataValidation.d.ts.map +1 -0
  96. package/dist/service/DatabaseService.d.ts +136 -0
  97. package/dist/service/DatabaseService.d.ts.map +1 -0
  98. package/dist/service/EventEmitter.d.ts +110 -0
  99. package/dist/service/EventEmitter.d.ts.map +1 -0
  100. package/dist/service/HealthManager.d.ts +166 -0
  101. package/dist/service/HealthManager.d.ts.map +1 -0
  102. package/dist/utils/ConfigMerger.d.ts +227 -0
  103. package/dist/utils/ConfigMerger.d.ts.map +1 -0
  104. package/dist/utils/databaseResultHelpers.d.ts +98 -0
  105. package/dist/utils/databaseResultHelpers.d.ts.map +1 -0
  106. package/dist/utils/index.d.ts +7 -0
  107. package/dist/utils/index.d.ts.map +1 -0
  108. package/dist/utils/normalizeDetails.d.ts +111 -0
  109. package/dist/utils/normalizeDetails.d.ts.map +1 -0
  110. package/dist/utils/pagination.d.ts +77 -0
  111. package/dist/utils/pagination.d.ts.map +1 -0
  112. package/dist/utils/regex.d.ts +199 -0
  113. package/dist/utils/regex.d.ts.map +1 -0
  114. package/dist/utils/typeGuards.d.ts +57 -0
  115. package/dist/utils/typeGuards.d.ts.map +1 -0
  116. package/dist/utils/validation.d.ts +146 -0
  117. package/dist/utils/validation.d.ts.map +1 -0
  118. package/package.json +156 -0
@@ -0,0 +1,451 @@
1
+ import type { DatabaseAdapterType, DatabaseResult, DBCacheConfig, Filter, DatabaseHealthStatus, PaginatedResult, QueryOptions, Transaction } from "@plyaz/types/db";
2
+ /**
3
+ * CACHING ADAPTER - Query Result Caching Layer
4
+ *
5
+ * Caching extension that stores query results in memory for improved performance.
6
+ * Third layer in the adapter chain.
7
+ *
8
+ * **Adapter Chain Position:**
9
+ * ReadReplica → Audit → **Cache** → SoftDelete → Encryption → Base Adapter
10
+ *
11
+ * **What this adapter does:**
12
+ * 1. Intercepts findById() operations → checks cache first, stores results
13
+ * 2. Intercepts write operations → invalidates related cache entries
14
+ * 3. Manages TTL-based cache expiration
15
+ * 4. Honors cache configuration (enabled, ttl, invalidation strategy)
16
+ *
17
+ * **Called by:** AuditAdapter (or ReadReplicaAdapter if no audit)
18
+ * **Calls:** SoftDeleteAdapter (or next adapter in chain)
19
+ * **Cache Strategy:** In-memory Map with TTL expiration
20
+ *
21
+ * **Cache Flow:**
22
+ * - **READ:** Check cache → Return if hit → Query DB → Cache result
23
+ * - **WRITE:** Execute operation → Invalidate related cache entries
24
+ *
25
+ * @example
26
+ * ### Configuration
27
+ * ```typescript
28
+ * cache: {
29
+ * enabled: true,
30
+ * ttl: 300, // 5 minutes
31
+ * invalidation: 'write', // Invalidate on writes
32
+ * maxSize: 1000 // Max cache entries
33
+ * }
34
+ * ```
35
+ *
36
+ * @example
37
+ * ### Cache Behavior
38
+ * ```typescript
39
+ * // First call - cache miss, queries database
40
+ * const user1 = await db.findById('users', 'user-123'); // DB query
41
+ *
42
+ * // Second call - cache hit, returns from memory
43
+ * const user2 = await db.findById('users', 'user-123'); // Cache hit
44
+ *
45
+ * // Write operation invalidates cache
46
+ * await db.update('users', 'user-123', { name: 'New Name' });
47
+ *
48
+ * // Next read - cache miss again, queries database
49
+ * const user3 = await db.findById('users', 'user-123'); // DB query
50
+ * ```
51
+ */
52
+ export declare class CachingAdapter implements DatabaseAdapterType {
53
+ private baseAdapter;
54
+ private config;
55
+ private cache;
56
+ /**
57
+ * Creates a new CachingAdapter instance.
58
+ *
59
+ * **RESPONSIBILITY:** Wraps base adapter with caching functionality
60
+ * **CONFIGURATION:** Sets up cache TTL, invalidation strategy, and size limits
61
+ *
62
+ * @param baseAdapter - The underlying database adapter to wrap
63
+ * @param config - Cache configuration options
64
+ *
65
+ * @example
66
+ * ```typescript
67
+ * const cachingAdapter = new CachingAdapter(baseAdapter, {
68
+ * enabled: true,
69
+ * ttl: 300, // 5 minutes
70
+ * invalidation: 'write', // Invalidate on writes
71
+ * maxSize: 1000 // Max entries
72
+ * });
73
+ * ```
74
+ */
75
+ constructor(baseAdapter: DatabaseAdapterType, config: DBCacheConfig);
76
+ /**
77
+ * Initializes the caching adapter and underlying adapter.
78
+ *
79
+ * **RESPONSIBILITY:** Passes initialization to base adapter
80
+ * **BEHAVIOR:** No additional initialization needed for caching
81
+ *
82
+ * @returns Promise resolving to initialization result
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * const result = await cachingAdapter.initialize();
87
+ * if (result.success) {
88
+ * console.log('Caching adapter initialized');
89
+ * }
90
+ * ```
91
+ */
92
+ initialize(): Promise<DatabaseResult<void>>;
93
+ /**
94
+ * Establishes database connection through base adapter.
95
+ *
96
+ * **RESPONSIBILITY:** Delegates connection to underlying adapter
97
+ * **BEHAVIOR:** No additional connection logic needed
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * await cachingAdapter.connect();
102
+ * console.log('Connected with caching support');
103
+ * ```
104
+ */
105
+ connect(): Promise<void>;
106
+ /**
107
+ * Closes database connection through base adapter.
108
+ *
109
+ * **RESPONSIBILITY:** Delegates disconnection to underlying adapter
110
+ * **BEHAVIOR:** Cache is cleared on disconnect
111
+ *
112
+ * @example
113
+ * ```typescript
114
+ * await cachingAdapter.disconnect();
115
+ * console.log('Disconnected and cache cleared');
116
+ * ```
117
+ */
118
+ disconnect(): Promise<void>;
119
+ /**
120
+ * Gets the underlying database client.
121
+ *
122
+ * **RESPONSIBILITY:** Provides access to raw database client
123
+ * **USE CASE:** For operations that bypass caching
124
+ *
125
+ * @returns Database client object
126
+ *
127
+ * @example
128
+ * ```typescript
129
+ * const client = cachingAdapter.getClient();
130
+ * // Use for direct database operations if needed
131
+ * ```
132
+ */
133
+ getClient(): object;
134
+ /**
135
+ * Executes raw SQL query through base adapter.
136
+ *
137
+ * **RESPONSIBILITY:** Passes raw SQL to base adapter without caching
138
+ * **BEHAVIOR:** Does not cache raw SQL results
139
+ * **NOTE:** Use findById/findMany for automatic caching
140
+ *
141
+ * @param sql - SQL query string
142
+ * @param params - Query parameters
143
+ * @returns Query results
144
+ *
145
+ * @example
146
+ * ```typescript
147
+ * // Raw SQL bypasses caching
148
+ * const results = await adapter.query(
149
+ * 'SELECT * FROM users WHERE status = $1',
150
+ * ['active']
151
+ * );
152
+ *
153
+ * // Use findMany for automatic caching
154
+ * const users = await adapter.findMany('users', { filter: ... });
155
+ * ```
156
+ */
157
+ query<T>(sql: string, params?: T[]): Promise<T[]>;
158
+ /**
159
+ * Registers a table schema with the base adapter.
160
+ *
161
+ * **RESPONSIBILITY:** Passes table registration to base adapter
162
+ * **BEHAVIOR:** No additional registration logic needed
163
+ *
164
+ * @param name - Table name
165
+ * @param table - Table schema
166
+ * @param idColumn - Primary key column
167
+ *
168
+ * @example
169
+ * ```typescript
170
+ * cachingAdapter.registerTable('users', userSchema, 'id');
171
+ * // Table now supports cached operations
172
+ * ```
173
+ */
174
+ registerTable<T, U>(name: string, table: T, idColumn?: U): void;
175
+ findById<T>(table: string, id: string): Promise<DatabaseResult<T | null>>;
176
+ /**
177
+ * Finds multiple records without caching.
178
+ *
179
+ * **RESPONSIBILITY:** Retrieves multiple records, bypassing cache
180
+ * **BEHAVIOR:** Complex queries are not cached currently
181
+ * **FUTURE:** Could implement query result caching with cache keys
182
+ *
183
+ * @param table - Table name
184
+ * @param options - Query options
185
+ * @returns Paginated results
186
+ *
187
+ * @example
188
+ * ```typescript
189
+ * // Complex queries bypass cache (for now)
190
+ * const users = await adapter.findMany('users', {
191
+ * filter: { field: 'status', operator: 'eq', value: 'active' },
192
+ * pagination: { page: 1, limit: 10 },
193
+ * sort: { field: 'name', direction: 'asc' }
194
+ * });
195
+ *
196
+ * // Always queries database directly
197
+ * ```
198
+ */
199
+ findMany<T>(table: string, options?: QueryOptions): Promise<DatabaseResult<PaginatedResult<T>>>;
200
+ /**
201
+ * Creates a new record with cache invalidation.
202
+ *
203
+ * **RESPONSIBILITY:** Creates record and invalidates related cache entries
204
+ * **INVALIDATION:** Clears table cache on successful write
205
+ * **STRATEGY:** Based on config.invalidation setting
206
+ *
207
+ * @param table - Table name
208
+ * @param data - Record data
209
+ * @returns Created record
210
+ *
211
+ * @example
212
+ * ```typescript
213
+ * // Create user - invalidates users table cache
214
+ * const result = await adapter.create('users', {
215
+ * name: 'John Doe',
216
+ * email: 'john@example.com'
217
+ * });
218
+ *
219
+ * if (result.success) {
220
+ * console.log('User created:', result.value.id);
221
+ * // All cached users:* entries are now invalid
222
+ * }
223
+ * ```
224
+ */
225
+ create<T>(table: string, data: T): Promise<DatabaseResult<T>>;
226
+ /**
227
+ * Updates an existing record with cache invalidation.
228
+ *
229
+ * **RESPONSIBILITY:** Updates record and invalidates related cache entries
230
+ * **INVALIDATION:** Clears specific record and table cache
231
+ * **STRATEGY:** Invalidates both table:id and table:* patterns
232
+ *
233
+ * @param table - Table name
234
+ * @param id - Record ID
235
+ * @param data - Partial record data
236
+ * @returns Updated record
237
+ *
238
+ * @example
239
+ * ```typescript
240
+ * // Update user - invalidates specific cache entry
241
+ * const result = await adapter.update('users', 'user-123', {
242
+ * name: 'Jane Doe'
243
+ * });
244
+ *
245
+ * if (result.success) {
246
+ * console.log('User updated:', result.value.name);
247
+ * // Cache entries users:user-123 and users:* are invalidated
248
+ * }
249
+ * ```
250
+ */
251
+ update<T>(table: string, id: string, data: Partial<T>): Promise<DatabaseResult<T>>;
252
+ /**
253
+ * Deletes a record with cache invalidation.
254
+ *
255
+ * **RESPONSIBILITY:** Deletes record and invalidates related cache entries
256
+ * **INVALIDATION:** Clears specific record and table cache
257
+ * **STRATEGY:** Removes both table:id and table:* patterns
258
+ *
259
+ * @param table - Table name
260
+ * @param id - Record ID
261
+ * @returns Deletion result
262
+ *
263
+ * @example
264
+ * ```typescript
265
+ * // Delete user - invalidates cache entries
266
+ * const result = await adapter.delete('users', 'user-123');
267
+ *
268
+ * if (result.success) {
269
+ * console.log('User deleted successfully');
270
+ * // Cache entries users:user-123 and users:* are invalidated
271
+ * }
272
+ * ```
273
+ */
274
+ delete(table: string, id: string): Promise<DatabaseResult<void>>;
275
+ /**
276
+ * Executes operations within a transaction.
277
+ *
278
+ * **RESPONSIBILITY:** Passes transaction to base adapter
279
+ * **BEHAVIOR:** Cache invalidation happens per operation within transaction
280
+ * **ATOMICITY:** Cache invalidation is not rolled back if transaction fails
281
+ *
282
+ * @param callback - Transaction callback function
283
+ * @returns Transaction result
284
+ *
285
+ * @example
286
+ * ```typescript
287
+ * const result = await adapter.transaction(async (trx) => {
288
+ * // Each operation invalidates cache independently
289
+ * const user = await trx.create('users', { name: 'John' });
290
+ * await trx.update('profiles', 'profile-1', { userId: user.id });
291
+ * return user;
292
+ * });
293
+ *
294
+ * // Cache invalidation happens even if transaction fails
295
+ * ```
296
+ */
297
+ transaction<T>(callback: (trx: Transaction) => Promise<T>): Promise<DatabaseResult<T>>;
298
+ /**
299
+ * Checks if a record exists without caching.
300
+ *
301
+ * **RESPONSIBILITY:** Verifies record existence, bypassing cache
302
+ * **BEHAVIOR:** Existence checks are not cached currently
303
+ * **PERFORMANCE:** Always queries database for existence
304
+ *
305
+ * @param table - Table name
306
+ * @param id - Record ID
307
+ * @returns True if record exists
308
+ *
309
+ * @example
310
+ * ```typescript
311
+ * // Existence checks bypass cache
312
+ * const userExists = await adapter.exists('users', 'user-123');
313
+ * if (userExists.value) {
314
+ * console.log('User exists');
315
+ * }
316
+ *
317
+ * // Always queries database directly
318
+ * ```
319
+ */
320
+ exists(table: string, id: string): Promise<DatabaseResult<boolean>>;
321
+ /**
322
+ * Counts records without caching.
323
+ *
324
+ * **RESPONSIBILITY:** Counts records, bypassing cache
325
+ * **BEHAVIOR:** Count operations are not cached currently
326
+ * **PERFORMANCE:** Always queries database for counts
327
+ *
328
+ * @param table - Table name
329
+ * @param filter - Optional filter conditions
330
+ * @returns Count of records
331
+ *
332
+ * @example
333
+ * ```typescript
334
+ * // Count operations bypass cache
335
+ * const activeUsers = await adapter.count('users', {
336
+ * field: 'status',
337
+ * operator: 'eq',
338
+ * value: 'active'
339
+ * });
340
+ *
341
+ * console.log('Active users:', activeUsers.value);
342
+ * // Always queries database directly
343
+ * ```
344
+ */
345
+ count(table: string, filter?: Filter): Promise<DatabaseResult<number>>;
346
+ /**
347
+ * Performs health check through base adapter.
348
+ *
349
+ * **RESPONSIBILITY:** Delegates health check to underlying adapter
350
+ * **BEHAVIOR:** No additional health metrics for cache
351
+ *
352
+ * @returns Health status from base adapter
353
+ *
354
+ * @example
355
+ * ```typescript
356
+ * const health = await adapter.healthCheck();
357
+ * if (health.success && health.value?.isHealthy) {
358
+ * console.log('Database healthy with caching support');
359
+ * }
360
+ * ```
361
+ */
362
+ healthCheck(): Promise<DatabaseResult<DatabaseHealthStatus>>;
363
+ /**
364
+ * Retrieves value from cache with TTL validation.
365
+ *
366
+ * **RESPONSIBILITY:** Gets cached value and validates expiration
367
+ * **TTL HANDLING:** Removes expired entries automatically
368
+ * **RETURN:** Cached value or null if not found/expired
369
+ *
370
+ * @private
371
+ * @param key - Cache key to retrieve
372
+ * @returns Cached value or null
373
+ *
374
+ * @example
375
+ * ```typescript
376
+ * // Internal usage
377
+ * const user = this.getFromCache<User>('users:user-123');
378
+ * if (user) {
379
+ * console.log('Cache hit:', user.name);
380
+ * } else {
381
+ * console.log('Cache miss or expired');
382
+ * }
383
+ * ```
384
+ */
385
+ private getFromCache;
386
+ /**
387
+ * Stores value in cache with TTL expiration.
388
+ *
389
+ * **RESPONSIBILITY:** Caches value with calculated expiration time
390
+ * **TTL:** Uses config.ttl or default 5 minutes (300 seconds)
391
+ * **EXPIRY:** Calculates absolute expiration timestamp
392
+ *
393
+ * @private
394
+ * @param key - Cache key to store
395
+ * @param value - Value to cache
396
+ *
397
+ * @example
398
+ * ```typescript
399
+ * // Internal usage after database query
400
+ * this.setCache('users:user-123', userData);
401
+ *
402
+ * // Value will expire after TTL seconds
403
+ * // Default: 5 minutes from now
404
+ * ```
405
+ */
406
+ private setCache;
407
+ /**
408
+ * Removes specific key from cache.
409
+ *
410
+ * **RESPONSIBILITY:** Invalidates single cache entry
411
+ * **USE CASE:** Called after record updates/deletes
412
+ * **PATTERN:** Removes exact key match
413
+ *
414
+ * @private
415
+ * @param key - Cache key to invalidate
416
+ *
417
+ * @example
418
+ * ```typescript
419
+ * // Internal usage after record update
420
+ * this.invalidateKey('users:user-123');
421
+ *
422
+ * // Only users:user-123 is removed from cache
423
+ * // Other users:* entries remain cached
424
+ * ```
425
+ */
426
+ private invalidateKey;
427
+ /**
428
+ * Removes all cache entries for a table.
429
+ *
430
+ * **RESPONSIBILITY:** Invalidates all entries matching table pattern
431
+ * **USE CASE:** Called after any write operation to table
432
+ * **PATTERN:** Removes all keys starting with 'table:'
433
+ *
434
+ * @private
435
+ * @param table - Table name to invalidate
436
+ *
437
+ * @example
438
+ * ```typescript
439
+ * // Internal usage after any write to users table
440
+ * this.invalidateTable('users');
441
+ *
442
+ * // Removes all cache entries:
443
+ * // - users:user-123
444
+ * // - users:user-456
445
+ * // - users:admin-789
446
+ * // But keeps: profiles:profile-1, posts:post-1, etc.
447
+ * ```
448
+ */
449
+ private invalidateTable;
450
+ }
451
+ //# sourceMappingURL=CachingAdapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CachingAdapter.d.ts","sourceRoot":"","sources":["../../src/extensions/CachingAdapter.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,mBAAmB,EACnB,cAAc,EACd,aAAa,EACb,MAAM,EACN,oBAAoB,EACpB,eAAe,EACf,YAAY,EACZ,WAAW,EACZ,MAAM,iBAAiB,CAAC;AACzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,qBAAa,cAAe,YAAW,mBAAmB;IA+BtD,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,MAAM;IA/BhB,OAAO,CAAC,KAAK,CAQT;IAEJ;;;;;;;;;;;;;;;;;;OAkBG;gBAEO,WAAW,EAAE,mBAAmB,EAChC,MAAM,EAAE,aAAa;IAG/B;;;;;;;;;;;;;;;OAeG;IACG,UAAU,IAAI,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;IAIjD;;;;;;;;;;;OAWG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAI9B;;;;;;;;;;;OAWG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAIjC;;;;;;;;;;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B,QAAQ,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,GACzC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;IAI7B;;;;;;;;;;;;;;;;;;;;;OAqBG;IACG,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IAIzE;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACG,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IAI5E;;;;;;;;;;;;;;;OAeG;IACG,WAAW,IAAI,OAAO,CAAC,cAAc,CAAC,oBAAoB,CAAC,CAAC;IAIlE;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,OAAO,CAAC,YAAY;IAYpB;;;;;;;;;;;;;;;;;;;OAmBG;IACH,OAAO,CAAC,QAAQ;IAWhB;;;;;;;;;;;;;;;;;;OAkBG;IACH,OAAO,CAAC,aAAa;IAIrB;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,OAAO,CAAC,eAAe;CAOxB"}
@@ -0,0 +1,95 @@
1
+ import { type DatabaseAdapterType, type DatabaseResult, type QueryOptions, type PaginatedResult, type Transaction, type Filter, type DatabaseHealthStatus } from "@plyaz/types/db";
2
+ /**
3
+ * ENCRYPTION ADAPTER — Field-Level Encryption Layer
4
+ *
5
+ * Encryption extension that automatically encrypts/decrypts specified fields.
6
+ * Second layer in the adapter chain (after the base adapter).
7
+ *
8
+ * **Adapter Chain Position:**
9
+ * ReadReplica → Audit → Cache → SoftDelete → **Encryption** → Base Adapter
10
+ *
11
+ * **What this adapter does:**
12
+ * 1. Receives operations from SoftDeleteAdapter (or the next layer)
13
+ * 2. Encrypts sensitive fields before write operations (create, update)
14
+ * 3. Delegates to the base adapter with encrypted data
15
+ * 4. Decrypts fields in response data after read operations
16
+ * 5. Passes results back up the chain
17
+ *
18
+ * **Called by:** SoftDeleteAdapter (or CachingAdapter if no soft delete)
19
+ * **Calls:** Base adapter methods (DrizzleAdapter, SupabaseAdapter, etc.)
20
+ * **Wraps:** Base database adapter with transparent encryption
21
+ *
22
+ * **Encryption Flow:**
23
+ * - **Writes:** Plain data → Encrypt fields → Store encrypted → Return decrypted
24
+ * - **Reads:** Retrieve encrypted → Decrypt fields → Return plain data
25
+ *
26
+ * @example
27
+ * ### Configuration
28
+ * ```typescript
29
+ * encryption: {
30
+ * enabled: true,
31
+ * key: process.env.ENCRYPTION_KEY,
32
+ * fields: {
33
+ * [Tables.USERS]: ['ssn', 'taxId'],
34
+ * [Tables.PAYMENTS]: ['cardNumber', 'cvv']
35
+ * }
36
+ * }
37
+ * ```
38
+ *
39
+ * @example
40
+ * ### Transparent Usage
41
+ * ```typescript
42
+ * // Application code — encryption is transparent
43
+ * await db.create(Tables.USERS, {
44
+ * name: 'John Doe',
45
+ * ssn: '123-45-6789' // Automatically encrypted before storage
46
+ * });
47
+ *
48
+ * // Retrieved data is automatically decrypted
49
+ * const user = await db.get(Tables.USERS, userId);
50
+ * console.log(user.value.ssn); // '123-45-6789' (decrypted)
51
+ * ```
52
+ */
53
+ export declare class EncryptionAdapter implements DatabaseAdapterType {
54
+ private baseAdapter;
55
+ private config;
56
+ constructor(baseAdapter: DatabaseAdapterType, config: {
57
+ enabled: boolean;
58
+ key: string;
59
+ fields: Record<string, string[]>;
60
+ algorithm?: string;
61
+ });
62
+ initialize(): Promise<DatabaseResult<void>>;
63
+ connect(): Promise<void>;
64
+ disconnect(): Promise<void>;
65
+ getClient(): object;
66
+ query<T>(sql: string, params?: T[]): Promise<T[]>;
67
+ registerTable<T, U>(name: string, table: T, idColumn?: U): void;
68
+ findById<T>(table: string, id: string): Promise<DatabaseResult<T | null>>;
69
+ findMany<T>(table: string, options?: QueryOptions): Promise<DatabaseResult<PaginatedResult<T>>>;
70
+ create<T>(table: string, data: T): Promise<DatabaseResult<T>>;
71
+ update<T>(table: string, id: string, data: Partial<T>): Promise<DatabaseResult<T>>;
72
+ delete(table: string, id: string): Promise<DatabaseResult<void>>;
73
+ transaction<T>(callback: (trx: Transaction) => Promise<T>): Promise<DatabaseResult<T>>;
74
+ exists(table: string, id: string): Promise<DatabaseResult<boolean>>;
75
+ count(table: string, filter?: Filter): Promise<DatabaseResult<number>>;
76
+ healthCheck(): Promise<DatabaseResult<DatabaseHealthStatus>>;
77
+ private encryptFields;
78
+ private shouldProcessFields;
79
+ private encryptSingleField;
80
+ private decryptFields;
81
+ private decryptSingleField;
82
+ private encrypt;
83
+ private validateEncryptionInput;
84
+ private createCipher;
85
+ private performEncryption;
86
+ private decrypt;
87
+ private validateDecryptionInput;
88
+ private parseEncryptedText;
89
+ private extractDecryptionParts;
90
+ private createDecipher;
91
+ private performDecryption;
92
+ private isEncryptedValue;
93
+ private getKeyBuffer;
94
+ }
95
+ //# sourceMappingURL=EncryptionExtension.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EncryptionExtension.d.ts","sourceRoot":"","sources":["../../src/extensions/EncryptionExtension.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,mBAAmB,EACxB,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,KAAK,MAAM,EACX,KAAK,oBAAoB,EAE1B,MAAM,iBAAiB,CAAC;AAKzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AAEH,qBAAa,iBAAkB,YAAW,mBAAmB;IAIzD,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,MAAM;gBADN,WAAW,EAAE,mBAAmB,EAChC,MAAM,EAAE;QACd,OAAO,EAAE,OAAO,CAAC;QACjB,GAAG,EAAE,MAAM,CAAC;QACZ,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;QACjC,SAAS,CAAC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Ac,CAAC,CAAC,CAAC,CAAC;IAIvB,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IAInE,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IAItE,WAAW,IAAI,OAAO,CAAC,cAAc,CAAC,oBAAoB,CAAC,CAAC;IAIlE,OAAO,CAAC,aAAa;IAarB,OAAO,CAAC,mBAAmB;IAQ3B,OAAO,CAAC,kBAAkB;IAY1B,OAAO,CAAC,aAAa;IAarB,OAAO,CAAC,kBAAkB;IAc1B,OAAO,CAAC,OAAO;IAYf,OAAO,CAAC,uBAAuB;IAuB/B,OAAO,CAAC,YAAY;IAWpB,OAAO,CAAC,iBAAiB;IASzB,OAAO,CAAC,OAAO;IAUf,OAAO,CAAC,uBAAuB;IAuB/B,OAAO,CAAC,kBAAkB;IAe1B,OAAO,CAAC,sBAAsB;IAW9B,OAAO,CAAC,cAAc;IAiBtB,OAAO,CAAC,iBAAiB;IASzB,OAAO,CAAC,gBAAgB;IAMxB,OAAO,CAAC,YAAY;CAkBrB"}
@@ -0,0 +1,32 @@
1
+ import type { DatabaseAdapterType, DatabaseResult, QueryOptions, PaginatedResult, Transaction, Filter, DatabaseHealthStatus, ReadReplicaConfig } from "@plyaz/types/db";
2
+ /**
3
+ * Read replica extension that routes read operations to replicas
4
+ * This is the outermost wrapper in the adapter chain
5
+ */
6
+ export declare class ReadReplicaAdapter implements DatabaseAdapterType {
7
+ private primaryAdapter;
8
+ private config;
9
+ private currentReplicaIndex;
10
+ constructor(primaryAdapter: DatabaseAdapterType, config: ReadReplicaConfig);
11
+ initialize(): Promise<DatabaseResult<void>>;
12
+ connect(): Promise<void>;
13
+ disconnect(): Promise<void>;
14
+ getClient(): object;
15
+ query<T>(sql: string, params?: T[]): Promise<T[]>;
16
+ registerTable<T, U>(name: string, table: T, idColumn?: U): void;
17
+ findById<T>(table: string, id: string): Promise<DatabaseResult<T | null>>;
18
+ findMany<T>(table: string, options?: QueryOptions): Promise<DatabaseResult<PaginatedResult<T>>>;
19
+ create<T>(table: string, data: T): Promise<DatabaseResult<T>>;
20
+ update<T>(table: string, id: string, data: Partial<T>): Promise<DatabaseResult<T>>;
21
+ delete(table: string, id: string): Promise<DatabaseResult<void>>;
22
+ transaction<T>(callback: (trx: Transaction) => Promise<T>): Promise<DatabaseResult<T>>;
23
+ exists(table: string, id: string): Promise<DatabaseResult<boolean>>;
24
+ count(table: string, filter?: Filter): Promise<DatabaseResult<number>>;
25
+ healthCheck(): Promise<DatabaseResult<DatabaseHealthStatus>>;
26
+ /**
27
+ * Get the appropriate adapter for read operations
28
+ * Routes to replicas using the configured strategy
29
+ */
30
+ private getReadAdapter;
31
+ }
32
+ //# sourceMappingURL=ReadReplicaAdapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ReadReplicaAdapter.d.ts","sourceRoot":"","sources":["../../src/extensions/ReadReplicaAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,cAAc,EACd,YAAY,EACZ,eAAe,EACf,WAAW,EACX,MAAM,EACN,oBAAoB,EACpB,iBAAiB,EAClB,MAAM,iBAAiB,CAAC;AAEzB;;;GAGG;AACH,qBAAa,kBAAmB,YAAW,mBAAmB;IAI1D,OAAO,CAAC,cAAc;IACtB,OAAO,CAAC,MAAM;IAJhB,OAAO,CAAC,mBAAmB,CAAK;gBAGtB,cAAc,EAAE,mBAAmB,EACnC,MAAM,EAAE,iBAAiB;IAG7B,UAAU,IAAI,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;IAI3C,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxB,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAIjC,SAAS,IAAI,MAAM;IAIb,KAAK,CAAC,CAAC,EAAE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