/** * src/repositories/postgres-source-repository.ts * ================================================= * Real Postgres backing for SourceRepository, against `opt_sources` * (sql/002_source_registry.sql). Same validation/error semantics as * InMemorySourceRepository ("already exists", "name is required", "not * found" -- exact message text preserved so every existing caller/test that * matches on those strings keeps working unmodified). */ import type { CreateSourceInput, Source, SourceStatus, SourceType } from "../types/source"; import type { RepositorySql } from "./db"; import type { SourceListFilter, SourceRepository } from "./source-repository"; interface SourceRow { source_id: string; tenant_id: string; project_id: string; source_type: string; name: string; adapter_id: string | null; access: string | null; description: string | null; status: string; correlation_id: string | null; created_at: string | Date; updated_at: string | Date | null; created_by: string | null; owner_id: string | null; } function toIsoString(v: string | Date): string { return v instanceof Date ? v.toISOString() : v; } function toSource(r: SourceRow): Source { return { source_id: r.source_id, tenant_id: r.tenant_id, project_id: r.project_id, source_type: r.source_type as SourceType, name: r.name, adapter_id: r.adapter_id ?? undefined, access: (r.access as Source["access"]) ?? undefined, description: r.description ?? undefined, status: r.status as SourceStatus, correlation_id: r.correlation_id ?? undefined, created_at: toIsoString(r.created_at), updated_at: r.updated_at ? toIsoString(r.updated_at) : undefined, created_by: r.created_by ?? undefined, owner_id: r.owner_id ?? undefined, }; } export class PostgresSourceRepository implements SourceRepository { constructor(private readonly sql: RepositorySql) {} async create(input: CreateSourceInput): Promise { if (!input.name) throw new Error("name is required"); const existing = await this.sql<{ source_id: string }[]>` SELECT source_id FROM opt_sources WHERE source_id = ${input.source_id} LIMIT 1`; if (existing.length > 0) { throw new Error(`Source ${input.source_id} already exists`); } const rows = await this.sql` INSERT INTO opt_sources (source_id, tenant_id, project_id, source_type, name, adapter_id, access, description, status, correlation_id, created_by, owner_id) VALUES (${input.source_id}, ${input.tenant_id}, ${input.project_id}, ${input.source_type}, ${input.name}, ${input.adapter_id ?? null}, ${input.access ?? null}, ${input.description ?? null}, ${input.status ?? "pending"}, ${input.correlation_id ?? null}, ${input.created_by ?? null}, ${input.owner_id ?? null}) RETURNING source_id, tenant_id, project_id, source_type, name, adapter_id, access, description, status, correlation_id, created_at, updated_at, created_by, owner_id`; return toSource(rows[0]); } async get(sourceId: string): Promise { const rows = await this.sql` SELECT source_id, tenant_id, project_id, source_type, name, adapter_id, access, description, status, correlation_id, created_at, updated_at, created_by, owner_id FROM opt_sources WHERE source_id = ${sourceId} LIMIT 1`; if (!rows[0]) throw new Error(`Source ${sourceId} not found`); return toSource(rows[0]); } async updateStatus(sourceId: string, status: SourceStatus): Promise { const rows = await this.sql` UPDATE opt_sources SET status = ${status}, updated_at = now() WHERE source_id = ${sourceId} RETURNING source_id, tenant_id, project_id, source_type, name, adapter_id, access, description, status, correlation_id, created_at, updated_at, created_by, owner_id`; if (!rows[0]) throw new Error(`Source ${sourceId} not found`); return toSource(rows[0]); } async list(filter?: SourceListFilter): Promise { const rows = await this.sql` SELECT source_id, tenant_id, project_id, source_type, name, adapter_id, access, description, status, correlation_id, created_at, updated_at, created_by, owner_id FROM opt_sources WHERE 1=1 ${filter?.tenant_id ? this.sql`AND tenant_id = ${filter.tenant_id}` : this.sql``} ${filter?.project_id ? this.sql`AND project_id = ${filter.project_id}` : this.sql``} ${filter?.source_type ? this.sql`AND source_type = ${filter.source_type}` : this.sql``} ${filter?.status ? this.sql`AND status = ${filter.status}` : this.sql``} ORDER BY created_at ASC`; return rows.map(toSource); } }