forked from Talenttrust/Talenttrust-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.ts
More file actions
571 lines (527 loc) · 18.7 KB
/
Copy pathstorage.ts
File metadata and controls
571 lines (527 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/**
* Storage abstraction layer for retained data
*
* Provides a flexible interface for storing, retrieving, and managing
* retained data with support for different storage backends.
*
* @module retention/storage
*/
import { RetainedData, ArchivalStorageType, DataClassification } from './types';
import Database from '../db/betterSqlite3';
import type * as BetterSqlite3 from 'better-sqlite3';
import { getDb } from '../db/database';
/**
* Maximum records returned by a single bounded `listPaginated` call.
* The bound protects callers from accidentally materialising an entire
* archive into memory and matches the conservative cap used elsewhere
* in the codbase ({@link database.getContractMetadataByContractId}).
*/
export const RETENTION_PAGE_MAX_LIMIT = 1000;
/**
* Abstract storage provider interface
*
* Allows implementation of different storage backends (local, cloud, encrypted, etc.)
*
* @interface IStorageProvider
*/
export interface IStorageProvider {
/**
* Store a data entity
* @param {RetainedData} data - Data to store
* @returns {Promise<string>} Storage location/ID
*/
store(data: RetainedData): Promise<string>;
/**
* Retrieve stored data
* @param {string} id - Data identifier
* @returns {Promise<RetainedData | null>} Retrieved data or null if not found
*/
retrieve(id: string): Promise<RetainedData | null>;
/**
* Delete stored data
* @param {string} id - Data identifier
* @returns {Promise<boolean>} Success status
*/
delete(id: string): Promise<boolean>;
/**
* List all stored data
* @returns {Promise<RetainedData[]>} All stored data
*/
list(): Promise<RetainedData[]>;
/**
* Bounded/paginated list of stored data ordered by `created_at` then `id`,
* giving callers a stable cursor across pages.
*
* @param {number} limit - Maximum records to return. Values <= 0 are clamped up
* to 1; values above {@link RETENTION_PAGE_MAX_LIMIT} are clamped down.
* @param {number} [offset=0] - Records to skip before the first result.
* Negative offsets are clamped to 0.
* @returns {Promise<RetainedData[]>} A page of stored data.
*/
listPaginated(limit: number, offset?: number): Promise<RetainedData[]>;
/**
* Check if data exists
* @param {string} id - Data identifier
* @returns {Promise<boolean>} Existence status
*/
exists(id: string): Promise<boolean>;
}
/**
* In-memory storage provider implementation
*
* Suitable for development, testing, and small-scale deployments.
* Data is stored in application memory.
*
* @class InMemoryStorageProvider
* @implements {IStorageProvider}
*/
export class InMemoryStorageProvider implements IStorageProvider {
private storage: Map<string, RetainedData> = new Map();
/**
* Store data in memory
* @param {RetainedData} data - Data to store
* @returns {Promise<string>} Data ID
*/
async store(data: RetainedData): Promise<string> {
this.storage.set(data.id, { ...data });
return data.id;
}
/**
* Retrieve data from memory
* @param {string} id - Data identifier
* @returns {Promise<RetainedData | null>}
*/
async retrieve(id: string): Promise<RetainedData | null> {
return this.storage.get(id) || null;
}
/**
* Delete data from memory
* @param {string} id - Data identifier
* @returns {Promise<boolean>}
*/
async delete(id: string): Promise<boolean> {
return this.storage.delete(id);
}
/**
* List all data in memory
* @returns {Promise<RetainedData[]>}
*/
async list(): Promise<RetainedData[]> {
return Array.from(this.storage.values());
}
/**
* Bounded/paginated list backed by the in-memory map.
*
* Pagination uses the same ordering as {@link InMemoryStorageProvider.list}
* (insertion order → conventionally `created_at` ascending) so call sites
* share a stable cursor between full and paged reads.
*
* @inheritDoc IStorageProvider.listPaginated
*/
async listPaginated(limit: number, offset: number = 0): Promise<RetainedData[]> {
const boundedLimit = clampPageLimit(limit);
const boundedOffset = Math.max(0, Math.floor(offset));
return Array.from(this.storage.values()).slice(boundedOffset, boundedOffset + boundedLimit);
}
/**
* Check if data exists
* @param {string} id - Data identifier
* @returns {Promise<boolean>}
*/
async exists(id: string): Promise<boolean> {
return this.storage.has(id);
}
/**
* Clear all data (useful for testing)
* @returns {void}
*/
clear(): void {
this.storage.clear();
}
/**
* Get storage size
* @returns {number}
*/
size(): number {
return this.storage.size;
}
}
/**
* Options accepted by {@link SqliteStorageProvider}.
*
* @interface SqliteStorageProviderOptions
* @property {string} tableName - SQLite table that backs this provider. Each
* provider instance must use a distinct table (the migration creates
* `retention_local` and `retention_archive`); using the same table for
* two providers will cause them to silently alias and reuse each other's
* rows.
* @property {BetterSqlite3.Database} [db] - Optional explicit database handle.
* When omitted, the singleton from {@link getDb} is used. Tests typically
* pass a `:memory:` database for isolation.
*/
export interface SqliteStorageProviderOptions {
tableName: string;
db?: BetterSqlite3.Database;
}
/**
* Row shape persisted by {@link SqliteStorageProvider}. Mirrors the
* `RetainedData` columns with `Date` fields serialised as ISO-8601 strings
* and the `data` / `metadata` payloads as JSON.
*
* @private
*/
interface RetentionRow {
id: string;
entity_type: string;
data: string;
classification: string;
created_at: string;
expires_at: string;
archived_at: string | null;
archived_location: string | null;
is_archived: number;
retention_policy_id: string | null;
metadata: string | null;
updated_at: string;
}
/**
* SQLite-backed {@link IStorageProvider} used by default for the retention
* manager in non-test environments.
*
* Records survive process restarts (the underlying file is durable), writes
* are atomic via SQLite's per-statement transaction model, and reads are
* bounded through {@link SqliteStorageProvider.listPaginated}.
*
* @class SqliteStorageProvider
* @implements {IStorageProvider}
*
* @example
* // Provider for the local retention bucket
* const local = new SqliteStorageProvider({ tableName: 'retention_local' });
* await local.store(myRetainedData);
*
* @example
* // Test-only ephemeral provider that never touches the global DB
* import BetterSqlite3 from '../db/betterSqlite3';
* const db = new BetterSqlite3(':memory:');
* const provider = new SqliteStorageProvider({ tableName: 'retention_local', db });
*/
export class SqliteStorageProvider implements IStorageProvider {
private readonly db: BetterSqlite3.Database;
private readonly tableName: string;
private readonly insertStmt: BetterSqlite3.Statement<[Record<string, unknown>]>;
private readonly selectByIdStmt: BetterSqlite3.Statement<[string]>;
private readonly deleteByIdStmt: BetterSqlite3.Statement<[string]>;
private readonly selectAllStmt: BetterSqlite3.Statement<[]>;
private readonly selectPageStmt: BetterSqlite3.Statement<[number, number]>;
private readonly existsStmt: BetterSqlite3.Statement<[string]>;
/**
* Initialise a SQLite-backed retention provider.
*
* The constructor is synchronous and idempotent: `{tableName}` is ensured
* via `CREATE TABLE IF NOT EXISTS`, so it is safe to instantiate during
* module load and reuse.
*
* @param {SqliteStorageProviderOptions} options - Provider configuration.
*/
constructor(options: SqliteStorageProviderOptions) {
if (!options || !options.tableName || typeof options.tableName !== 'string') {
throw new Error('SqliteStorageProvider requires a non-empty tableName option');
}
// Only allow identifier-shaped table names to defeat SQL injection via
// the table name interpolation in CREATE / DML statements.
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(options.tableName)) {
throw new Error(`SqliteStorageProvider tableName must match /^[A-Za-z_][A-Za-z0-9_]*$/; got '${options.tableName}'`);
}
this.tableName = options.tableName;
// `getDb()` is typed to return its constructor (a pre-existing quirk of
// `src/db/database.ts`); at runtime the cached value is the database
// instance, so we narrow with a one-shot cast here.
this.db = options.db ?? (getDb() as unknown as BetterSqlite3.Database);
this.ensureTable();
// Cache prepared statements for the lifetime of the provider. Re-using
// prepared statements is the primary performance win in better-sqlite3
// and matches the pattern elsewhere in the codebase.
this.insertStmt = this.db.prepare(
`INSERT OR REPLACE INTO ${this.tableName} (
id, entity_type, data, classification,
created_at, expires_at, archived_at, archived_location,
is_archived, retention_policy_id, metadata, updated_at
) VALUES (
@id, @entity_type, @data, @classification,
@created_at, @expires_at, @archived_at, @archived_location,
@is_archived, @retention_policy_id, @metadata, @updated_at
)`,
);
this.selectByIdStmt = this.db.prepare(
`SELECT * FROM ${this.tableName} WHERE id = ?`,
);
this.deleteByIdStmt = this.db.prepare(
`DELETE FROM ${this.tableName} WHERE id = ?`,
);
this.selectAllStmt = this.db.prepare(
`SELECT * FROM ${this.tableName} ORDER BY created_at ASC, id ASC`,
);
this.selectPageStmt = this.db.prepare(
`SELECT * FROM ${this.tableName} ORDER BY created_at ASC, id ASC LIMIT ? OFFSET ?`,
);
this.existsStmt = this.db.prepare(
`SELECT 1 FROM ${this.tableName} WHERE id = ?`,
);
}
/**
* Returns the table name backing this provider. Exposed for tests and diagnostics.
* @returns {string}
*/
getTableName(): string {
return this.tableName;
}
/**
* Persist a single record. `INSERT OR REPLACE` makes repeated calls idempotent —
* the second `store` with the same `data.id` overwrites the previous row rather
* than producing a constraint error.
*
* @inheritDoc IStorageProvider.store
*/
async store(data: RetainedData): Promise<string> {
const now = new Date().toISOString();
this.insertStmt.run({
id: data.id,
entity_type: data.entityType,
data: JSON.stringify(data.data ?? null),
classification: data.classification, created_at: data.createdAt.toISOString(),
expires_at: data.expiresAt.toISOString(),
archived_at: data.archivedAt ? data.archivedAt.toISOString() : null,
archived_location: data.archivedLocation ?? null,
is_archived: data.isArchived ? 1 : 0,
retention_policy_id: data.retentionPolicyId ?? null,
metadata: data.metadata ? JSON.stringify(data.metadata) : null,
updated_at: now,
});
return data.id;
}
/**
* Fetch a record by id or `null` when no row matches.
*
* @inheritDoc IStorageProvider.retrieve
*/
async retrieve(id: string): Promise<RetainedData | null> {
const row = this.selectByIdStmt.get(id) as RetentionRow | undefined;
if (!row) return null;
return rowToRetainedData(row);
}
/**
* Delete a record by id. Returns `true` when a row was actually removed.
*
* @inheritDoc IStorageProvider.delete
*/
async delete(id: string): Promise<boolean> {
const result = this.deleteByIdStmt.run(id);
return result.changes > 0;
}
/**
* List every record in insertion-order (created_at then id).
*
* Prefer {@link SqliteStorageProvider.listPaginated} for large archives;
* this method materialises the full result set.
*
* @inheritDoc IStorageProvider.list
*/
async list(): Promise<RetainedData[]> {
const rows = this.selectAllStmt.all() as RetentionRow[];
return rows.map(rowToRetainedData);
}
/**
* Stable, bounded page read backed by `LIMIT`/`OFFSET`. The order matches
* {@link SqliteStorageProvider.list} so pages compose into a deterministic
* cursor across calls.
*
* @inheritDoc IStorageProvider.listPaginated
*/
async listPaginated(limit: number, offset: number = 0): Promise<RetainedData[]> {
const boundedLimit = clampPageLimit(limit);
const boundedOffset = Math.max(0, Math.floor(offset));
const rows = this.selectPageStmt.all(boundedLimit, boundedOffset) as RetentionRow[];
return rows.map(rowToRetainedData);
}
/**
* Cheap existence probe that avoids materialising the row.
*
* @inheritDoc IStorageProvider.exists
*/
async exists(id: string): Promise<boolean> {
const result = this.existsStmt.get(id);
return result !== undefined;
}
/**
* Make sure the underlying table exists. Mirrors the production migration
* so unit tests can spin up an in-memory provider without running the full
* migration set first.
*
* @private
*/
private ensureTable(): void {
this.db.exec(`
CREATE TABLE IF NOT EXISTS ${this.tableName} (
id TEXT PRIMARY KEY,
entity_type TEXT NOT NULL,
data TEXT NOT NULL,
classification TEXT NOT NULL,
created_at TEXT NOT NULL,
expires_at TEXT NOT NULL,
archived_at TEXT,
archived_location TEXT,
is_archived INTEGER NOT NULL CHECK (is_archived IN (0, 1)),
retention_policy_id TEXT,
metadata TEXT,
updated_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_${this.tableName}_entity_type
ON ${this.tableName}(entity_type);
CREATE INDEX IF NOT EXISTS idx_${this.tableName}_is_archived
ON ${this.tableName}(is_archived);
CREATE INDEX IF NOT EXISTS idx_${this.tableName}_expires_at
ON ${this.tableName}(expires_at);
CREATE INDEX IF NOT EXISTS idx_${this.tableName}_created_at
ON ${this.tableName}(created_at);
`);
}
}
/**
* Storage manager for handling multiple storage backends
*
* @class StorageManager
*/
export class StorageManager {
private localProvider: IStorageProvider;
private archiveProvider: IStorageProvider;
/**
* Initialize storage manager with providers
* @param {IStorageProvider} [localProvider] - Local storage provider (defaults to in-memory)
* @param {IStorageProvider} [archiveProvider] - Archive storage provider (defaults to in-memory)
*/
constructor(
localProvider?: IStorageProvider,
archiveProvider?: IStorageProvider,
) {
this.localProvider = localProvider || new InMemoryStorageProvider();
this.archiveProvider = archiveProvider || new InMemoryStorageProvider();
}
/**
* Store data with appropriate provider based on archival status
* @param {RetainedData} data - Data to store
* @param {ArchivalStorageType} [storageType='local'] - Storage type
* @returns {Promise<string>} Storage location
*/
async store(data: RetainedData, storageType: ArchivalStorageType = ArchivalStorageType.LOCAL): Promise<string> {
const provider = this.getProvider(storageType);
return provider.store(data);
}
/**
* Retrieve data from appropriate storage
* @param {string} id - Data identifier
* @param {ArchivalStorageType} [storageType='local'] - Storage type
* @returns {Promise<RetainedData | null>}
*/
async retrieve(id: string, storageType: ArchivalStorageType = ArchivalStorageType.LOCAL): Promise<RetainedData | null> {
const provider = this.getProvider(storageType);
return provider.retrieve(id);
}
/**
* Move data between storage types
* @param {string} id - Data identifier
* @param {ArchivalStorageType} fromType - Source storage type
* @param {ArchivalStorageType} toType - Destination storage type
* @returns {Promise<boolean>} Success status
*/
async moveData(
id: string,
fromType: ArchivalStorageType,
toType: ArchivalStorageType,
): Promise<boolean> {
const data = await this.retrieve(id, fromType);
if (!data) return false;
const toProvider = this.getProvider(toType);
const stored = await toProvider.store(data);
if (stored) {
const fromProvider = this.getProvider(fromType);
await fromProvider.delete(id);
return true;
}
return false;
}
/**
* Delete data from specified storage
* @param {string} id - Data identifier
* @param {ArchivalStorageType} [storageType='local'] - Storage type
* @returns {Promise<boolean>}
*/
async delete(id: string, storageType: ArchivalStorageType = ArchivalStorageType.LOCAL): Promise<boolean> {
const provider = this.getProvider(storageType);
return provider.delete(id);
}
/**
* Get provider for storage type
* @param {ArchivalStorageType} storageType - Storage type
* @returns {IStorageProvider}
*/
public getProvider(storageType: ArchivalStorageType): IStorageProvider {
switch (storageType) {
case ArchivalStorageType.COLD_STORAGE:
case ArchivalStorageType.ENCRYPTED_ARCHIVE:
return this.archiveProvider;
default:
return this.localProvider;
}
}
}
/**
* Clamp a caller-supplied pagination limit into [1, RETENTION_PAGE_MAX_LIMIT].
* Bad inputs (NaN, negatives, non-numbers) are pushed to 1; oversized limits
* collapse to the cap.
*
* @private
*/
function clampPageLimit(limit: number): number {
if (!Number.isFinite(limit) || limit <= 0) return 1;
return Math.min(Math.floor(limit), RETENTION_PAGE_MAX_LIMIT);
}
/**
* Hydrate a {@link RetentionRow} into a {@link RetainedData}. The conversion
* is intentionally defensive — `null` vs `undefined` matches the optional-vs-
* present semantics from the in-memory implementation and JSON-safe defaults
* avoid `JSON.parse('null')` crashes on empty payloads.
*
* @private
*/
function rowToRetainedData(row: RetentionRow): RetainedData {
let parsedPayload: unknown = null;
if (row.data && row.data !== 'null') {
try {
parsedPayload = JSON.parse(row.data);
} catch {
parsedPayload = row.data;
}
}
let parsedMetadata: Record<string, unknown> | undefined;
if (row.metadata) {
try {
parsedMetadata = JSON.parse(row.metadata);
} catch {
parsedMetadata = undefined;
}
}
return {
id: row.id,
entityType: row.entity_type as RetainedData['entityType'],
data: parsedPayload,
classification: row.classification as DataClassification,
createdAt: new Date(row.created_at),
expiresAt: new Date(row.expires_at),
archivedAt: row.archived_at ? new Date(row.archived_at) : undefined,
archivedLocation: row.archived_location ?? undefined,
isArchived: Boolean(row.is_archived),
retentionPolicyId: row.retention_policy_id ?? undefined,
metadata: parsedMetadata,
};
}