feat: add durable dataset version repository
This commit is contained in:
@@ -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<DatasetVersion>;
|
||||
list(datasetId: string, tenantId: string): Promise<DatasetVersion[]>;
|
||||
get(datasetId: string, tenantId: string, version: number): Promise<DatasetVersion>;
|
||||
}
|
||||
|
||||
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<string, DatasetVersion[]>();
|
||||
private readonly idempotency = new Map<string, DatasetVersion>();
|
||||
|
||||
async create(input: CreateDatasetVersionInput): Promise<DatasetVersion> {
|
||||
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<DatasetVersion[]> {
|
||||
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<DatasetVersion> {
|
||||
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<DatasetVersion> {
|
||||
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<VersionRow[]>`
|
||||
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<VersionRow[]>`
|
||||
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<DatasetVersion[]> {
|
||||
const rows = await this.sql<VersionRow[]>`
|
||||
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<DatasetVersion> {
|
||||
const rows = await this.sql<VersionRow[]>`
|
||||
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();
|
||||
}
|
||||
Reference in New Issue
Block a user