Assessor_2.0/server/remediation/remediation-store.ts

27 lines
1.4 KiB
TypeScript

import type { Pool } from 'pg';
import type { RemediationResult } from '../../src/remediation/remediation-engine';
export interface RemediationStore {
read(scopeKey: string): Promise<RemediationResult | undefined>;
save(scopeKey: string, result: RemediationResult): Promise<void>;
}
export class MemoryRemediationStore implements RemediationStore {
private readonly results = new Map<string, RemediationResult>();
async read(key: string) { const result = this.results.get(key); return result ? structuredClone(result) : undefined; }
async save(key: string, result: RemediationResult) { this.results.set(key, structuredClone(result)); }
}
/** All writes belong to the separate engine schema. No Core table is read or written. */
export class PostgresRemediationStore implements RemediationStore {
constructor(private readonly pool: Pool) {}
async read(key: string): Promise<RemediationResult | undefined> {
const response = await this.pool.query<{ data: RemediationResult }>(
'SELECT data FROM assessor_remediation.package_snapshots WHERE scope_key = $1', [key]);
return response.rows[0]?.data;
}
async save(key: string, result: RemediationResult): Promise<void> {
await this.pool.query('INSERT INTO assessor_remediation.package_snapshots (scope_key, data) VALUES ($1, $2::jsonb) ON CONFLICT (scope_key) DO NOTHING',
[key, JSON.stringify(result)]);
}
}