From 906fe9ac376eccea5915671f2db8c05e3374b21c Mon Sep 17 00:00:00 2001 From: Simon Axelsson Date: Mon, 7 Sep 2026 18:01:56 +0200 Subject: [PATCH] feat: add durable dataset version repository --- .../dataset-version-repository.ts | 173 ++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 src/repositories/dataset-version-repository.ts diff --git a/src/repositories/dataset-version-repository.ts b/src/repositories/dataset-version-repository.ts new file mode 100644 index 0000000..865c61c --- /dev/null +++ b/src/repositories/dataset-version-repository.ts @@ -0,0 +1,173 @@ +import { resolveStoreMode } from "../platform/dependency-mode"; +import { SiaxError } from "../platform/errors"; +import type { CreateDatasetVersionInput, DatasetVersion } from "../types/dataset-version"; +import { getRepositorySql, repositoryDatabaseUrl, type RepositorySql } from "./db"; + +export interface DatasetVersionRepository { + create(input: CreateDatasetVersionInput): Promise; + list(datasetId: string, tenantId: string): Promise; + get(datasetId: string, tenantId: string, version: number): Promise; +} + +interface VersionRow { + dataset_id: string; + version: number; + tenant_id: string; + project_id: string; + artifact_refs: string[]; + schema_version: string; + record_count: string | number | null; + description: string | null; + created_at: string | Date; + created_by: string; + correlation_id: string | null; + idempotency_key: string | null; +} + +function toVersion(row: VersionRow): DatasetVersion { + return { + dataset_id: row.dataset_id, + version: Number(row.version), + tenant_id: row.tenant_id, + project_id: row.project_id, + artifact_refs: [...row.artifact_refs], + schema_version: row.schema_version, + ...(row.record_count == null ? {} : { record_count: Number(row.record_count) }), + ...(row.description ? { description: row.description } : {}), + created_at: row.created_at instanceof Date ? row.created_at.toISOString() : new Date(row.created_at).toISOString(), + created_by: row.created_by, + ...(row.correlation_id ? { correlation_id: row.correlation_id } : {}), + }; +} + +function sameInput(row: VersionRow | DatasetVersion, input: CreateDatasetVersionInput): boolean { + return row.dataset_id === input.dataset_id + && row.tenant_id === input.tenant_id + && row.project_id === input.project_id + && row.schema_version === input.schema_version + && (row.record_count ?? undefined) === (input.record_count ?? undefined) + && (row.description ?? undefined) === (input.description ?? undefined) + && row.created_by === input.created_by + && JSON.stringify(row.artifact_refs) === JSON.stringify(input.artifact_refs); +} + +export class InMemoryDatasetVersionRepository implements DatasetVersionRepository { + private readonly versions = new Map(); + private readonly idempotency = new Map(); + + async create(input: CreateDatasetVersionInput): Promise { + if (input.idempotency_key) { + const existing = this.idempotency.get(`${input.tenant_id}:${input.idempotency_key}`); + if (existing) { + if (!sameInput(existing, input)) { + throw new SiaxError("CONFLICT", "0pt.dataset", "idempotency key reused with different dataset-version input"); + } + return { ...existing, artifact_refs: [...existing.artifact_refs] }; + } + } + const list = this.versions.get(input.dataset_id) ?? []; + const version: DatasetVersion = { + dataset_id: input.dataset_id, + version: (list[list.length - 1]?.version ?? 0) + 1, + tenant_id: input.tenant_id, + project_id: input.project_id, + artifact_refs: [...input.artifact_refs], + schema_version: input.schema_version, + ...(input.record_count === undefined ? {} : { record_count: input.record_count }), + ...(input.description ? { description: input.description } : {}), + created_at: new Date().toISOString(), + created_by: input.created_by, + ...(input.correlation_id ? { correlation_id: input.correlation_id } : {}), + }; + list.push(version); + this.versions.set(input.dataset_id, list); + if (input.idempotency_key) this.idempotency.set(`${input.tenant_id}:${input.idempotency_key}`, version); + return { ...version, artifact_refs: [...version.artifact_refs] }; + } + + async list(datasetId: string, tenantId: string): Promise { + return (this.versions.get(datasetId) ?? []) + .filter((version) => version.tenant_id === tenantId) + .map((version) => ({ ...version, artifact_refs: [...version.artifact_refs] })); + } + + async get(datasetId: string, tenantId: string, version: number): Promise { + const found = (await this.list(datasetId, tenantId)).find((item) => item.version === version); + if (!found) throw new SiaxError("NOT_FOUND", "0pt.dataset", `dataset ${datasetId} has no version ${version}`); + return found; + } +} + +export class PostgresDatasetVersionRepository implements DatasetVersionRepository { + constructor(private readonly sql: RepositorySql) {} + + async create(input: CreateDatasetVersionInput): Promise { + return this.sql.begin(async (tx) => { + const parent = await tx<{ dataset_id: string; tenant_id: string }[]>` + SELECT dataset_id, tenant_id + FROM opt_datasets + WHERE dataset_id = ${input.dataset_id} + FOR UPDATE`; + if (!parent[0] || parent[0].tenant_id !== input.tenant_id) { + throw new SiaxError("NOT_FOUND", "0pt.dataset", `dataset ${input.dataset_id} not found`); + } + + if (input.idempotency_key) { + const existing = await tx` + SELECT * FROM opt_dataset_versions + WHERE tenant_id = ${input.tenant_id} + AND idempotency_key = ${input.idempotency_key} + LIMIT 1`; + if (existing[0]) { + if (!sameInput(existing[0], input)) { + throw new SiaxError("CONFLICT", "0pt.dataset", "idempotency key reused with different dataset-version input"); + } + return toVersion(existing[0]); + } + } + + const next = await tx<{ version: number }[]>` + SELECT COALESCE(max(version), 0)::int + 1 AS version + FROM opt_dataset_versions + WHERE dataset_id = ${input.dataset_id}`; + const version = next[0]?.version ?? 1; + + const rows = await tx` + INSERT INTO opt_dataset_versions + (dataset_id, version, tenant_id, project_id, artifact_refs, + schema_version, record_count, description, created_by, + correlation_id, idempotency_key) + VALUES + (${input.dataset_id}, ${version}, ${input.tenant_id}, ${input.project_id}, + ${input.artifact_refs}, ${input.schema_version}, ${input.record_count ?? null}, + ${input.description ?? null}, ${input.created_by}, ${input.correlation_id ?? null}, + ${input.idempotency_key ?? null}) + RETURNING *`; + return toVersion(rows[0]); + }); + } + + async list(datasetId: string, tenantId: string): Promise { + const rows = await this.sql` + SELECT * FROM opt_dataset_versions + WHERE dataset_id = ${datasetId} AND tenant_id = ${tenantId} + ORDER BY version ASC`; + return rows.map(toVersion); + } + + async get(datasetId: string, tenantId: string, version: number): Promise { + const rows = await this.sql` + SELECT * FROM opt_dataset_versions + WHERE dataset_id = ${datasetId} AND tenant_id = ${tenantId} AND version = ${version} + LIMIT 1`; + if (!rows[0]) throw new SiaxError("NOT_FOUND", "0pt.dataset", `dataset ${datasetId} has no version ${version}`); + return toVersion(rows[0]); + } +} + +export function createDatasetVersionRepository(databaseUrl?: string): DatasetVersionRepository { + const mode = resolveStoreMode("0pt.dataset_versions", databaseUrl ?? repositoryDatabaseUrl()); + return mode.mode === "postgres" + ? new PostgresDatasetVersionRepository(getRepositorySql(mode.url)) + : new InMemoryDatasetVersionRepository(); +}