51 lines
2.3 KiB
TypeScript
51 lines
2.3 KiB
TypeScript
/**
|
|
* src/repositories/source-repository.ts
|
|
* =======================================
|
|
* Storage seam for src/services/source-service.ts (0pt.source.schema.v1).
|
|
* Extracted from what used to be a bare `new Map()` inside SourceService
|
|
* itself so the same business logic (validation, "already exists"/"not
|
|
* found" semantics, filtering) can run against either an in-memory Map
|
|
* (tests, local dev, non-production with no DATABASE_URL) or real Postgres
|
|
* (sql/002_source_registry.sql's `opt_sources` table) without the service
|
|
* layer knowing which.
|
|
*
|
|
* `createSourceRepository` is the single choice point, delegating to
|
|
* `resolveStoreMode` (src/platform/dependency-mode.ts) -- same
|
|
* production-safety contract as every other P0 store: no DATABASE_URL in a
|
|
* production runtime throws DEPENDENCY_UNAVAILABLE at construction rather
|
|
* than silently serving the in-memory fallback.
|
|
*/
|
|
import { resolveStoreMode } from "../platform/dependency-mode";
|
|
import type { CreateSourceInput, Source, SourceStatus, SourceType } from "../types/source";
|
|
import { getRepositorySql, repositoryDatabaseUrl } from "./db";
|
|
import { InMemorySourceRepository } from "./in-memory-source-repository";
|
|
import { PostgresSourceRepository } from "./postgres-source-repository";
|
|
|
|
export interface SourceListFilter {
|
|
tenant_id?: string;
|
|
project_id?: string;
|
|
source_type?: SourceType;
|
|
status?: SourceStatus;
|
|
}
|
|
|
|
export interface SourceRepository {
|
|
create(input: CreateSourceInput): Promise<Source>;
|
|
/** Throws (message contains "not found") when no such source exists -- matches the pre-migration Map behavior every caller already handles. */
|
|
get(sourceId: string): Promise<Source>;
|
|
list(filter?: SourceListFilter): Promise<Source[]>;
|
|
/**
|
|
* P060: the only way a source's lifecycle status changes. Deliberately NOT
|
|
* a general `update` -- status is the field the activation gate protects,
|
|
* and a generic patch method would be the obvious way around it.
|
|
*/
|
|
updateStatus(sourceId: string, status: SourceStatus): Promise<Source>;
|
|
}
|
|
|
|
export function createSourceRepository(databaseUrl?: string): SourceRepository {
|
|
const mode = resolveStoreMode("0pt.source_registry.sources", databaseUrl ?? repositoryDatabaseUrl());
|
|
if (mode.mode === "postgres") {
|
|
return new PostgresSourceRepository(getRepositorySql(mode.url));
|
|
}
|
|
return new InMemorySourceRepository();
|
|
}
|