feat: add durable dataset registry repository
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
import { resolveStoreMode } from "../platform/dependency-mode";
|
||||
import { SiaxError } from "../platform/errors";
|
||||
import type { CreateDatasetInput, Dataset, DatasetStatus, DatasetType } from "../types/dataset";
|
||||
import { getRepositorySql, repositoryDatabaseUrl, type RepositorySql } from "./db";
|
||||
|
||||
export interface DatasetListFilter {
|
||||
tenant_id?: string;
|
||||
project_id?: string;
|
||||
dataset_type?: DatasetType;
|
||||
status?: DatasetStatus;
|
||||
}
|
||||
|
||||
export interface DatasetRepository {
|
||||
create(input: CreateDatasetInput): Promise<Dataset>;
|
||||
get(datasetId: string): Promise<Dataset>;
|
||||
list(filter?: DatasetListFilter): Promise<Dataset[]>;
|
||||
}
|
||||
|
||||
interface DatasetRow {
|
||||
dataset_id: string;
|
||||
tenant_id: string;
|
||||
project_id: string;
|
||||
name: string;
|
||||
dataset_type: DatasetType;
|
||||
status: DatasetStatus;
|
||||
description: string | null;
|
||||
correlation_id: string | null;
|
||||
created_at: string | Date;
|
||||
updated_at: string | Date | null;
|
||||
created_by: string | null;
|
||||
}
|
||||
|
||||
function iso(value: string | Date): string {
|
||||
return value instanceof Date ? value.toISOString() : new Date(value).toISOString();
|
||||
}
|
||||
|
||||
function toDataset(row: DatasetRow): Dataset {
|
||||
return {
|
||||
dataset_id: row.dataset_id,
|
||||
tenant_id: row.tenant_id,
|
||||
project_id: row.project_id,
|
||||
name: row.name,
|
||||
dataset_type: row.dataset_type,
|
||||
status: row.status,
|
||||
...(row.description ? { description: row.description } : {}),
|
||||
...(row.correlation_id ? { correlation_id: row.correlation_id } : {}),
|
||||
created_at: iso(row.created_at),
|
||||
...(row.updated_at ? { updated_at: iso(row.updated_at) } : {}),
|
||||
...(row.created_by ? { created_by: row.created_by } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
function validate(input: CreateDatasetInput): void {
|
||||
if (!input.dataset_id?.trim()) throw new SiaxError("INVALID_REQUEST", "0pt.dataset", "dataset_id is required");
|
||||
if (!input.tenant_id?.trim()) throw new SiaxError("INVALID_REQUEST", "0pt.dataset", "tenant_id is required");
|
||||
if (!input.project_id?.trim()) throw new SiaxError("INVALID_REQUEST", "0pt.dataset", "project_id is required");
|
||||
if (!input.name?.trim()) throw new SiaxError("INVALID_REQUEST", "0pt.dataset", "name is required");
|
||||
}
|
||||
|
||||
export class InMemoryDatasetRepository implements DatasetRepository {
|
||||
private readonly rows = new Map<string, Dataset>();
|
||||
|
||||
async create(input: CreateDatasetInput): Promise<Dataset> {
|
||||
validate(input);
|
||||
if (this.rows.has(input.dataset_id)) {
|
||||
throw new SiaxError("CONFLICT", "0pt.dataset", `dataset ${input.dataset_id} already exists`);
|
||||
}
|
||||
const dataset: Dataset = {
|
||||
dataset_id: input.dataset_id,
|
||||
tenant_id: input.tenant_id,
|
||||
project_id: input.project_id,
|
||||
name: input.name,
|
||||
dataset_type: input.dataset_type,
|
||||
status: input.status ?? "active",
|
||||
...(input.description ? { description: input.description } : {}),
|
||||
...(input.correlation_id ? { correlation_id: input.correlation_id } : {}),
|
||||
created_at: new Date().toISOString(),
|
||||
...(input.created_by ? { created_by: input.created_by } : {}),
|
||||
};
|
||||
this.rows.set(dataset.dataset_id, dataset);
|
||||
return { ...dataset };
|
||||
}
|
||||
|
||||
async get(datasetId: string): Promise<Dataset> {
|
||||
const dataset = this.rows.get(datasetId);
|
||||
if (!dataset) throw new SiaxError("NOT_FOUND", "0pt.dataset", `dataset ${datasetId} not found`);
|
||||
return { ...dataset };
|
||||
}
|
||||
|
||||
async list(filter?: DatasetListFilter): Promise<Dataset[]> {
|
||||
return [...this.rows.values()]
|
||||
.filter((dataset) => !filter?.tenant_id || dataset.tenant_id === filter.tenant_id)
|
||||
.filter((dataset) => !filter?.project_id || dataset.project_id === filter.project_id)
|
||||
.filter((dataset) => !filter?.dataset_type || dataset.dataset_type === filter.dataset_type)
|
||||
.filter((dataset) => !filter?.status || dataset.status === filter.status)
|
||||
.map((dataset) => ({ ...dataset }));
|
||||
}
|
||||
}
|
||||
|
||||
export class PostgresDatasetRepository implements DatasetRepository {
|
||||
constructor(private readonly sql: RepositorySql) {}
|
||||
|
||||
async create(input: CreateDatasetInput): Promise<Dataset> {
|
||||
validate(input);
|
||||
try {
|
||||
const rows = await this.sql<DatasetRow[]>`
|
||||
INSERT INTO opt_datasets
|
||||
(dataset_id, tenant_id, project_id, name, dataset_type, status,
|
||||
description, correlation_id, created_by)
|
||||
VALUES
|
||||
(${input.dataset_id}, ${input.tenant_id}, ${input.project_id}, ${input.name},
|
||||
${input.dataset_type}, ${input.status ?? "active"}, ${input.description ?? null},
|
||||
${input.correlation_id ?? null}, ${input.created_by ?? null})
|
||||
RETURNING *`;
|
||||
return toDataset(rows[0]);
|
||||
} catch (error) {
|
||||
if ((error as { code?: string })?.code === "23505") {
|
||||
throw new SiaxError("CONFLICT", "0pt.dataset", `dataset ${input.dataset_id} already exists`);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async get(datasetId: string): Promise<Dataset> {
|
||||
const rows = await this.sql<DatasetRow[]>`
|
||||
SELECT * FROM opt_datasets WHERE dataset_id = ${datasetId} LIMIT 1`;
|
||||
if (!rows[0]) throw new SiaxError("NOT_FOUND", "0pt.dataset", `dataset ${datasetId} not found`);
|
||||
return toDataset(rows[0]);
|
||||
}
|
||||
|
||||
async list(filter?: DatasetListFilter): Promise<Dataset[]> {
|
||||
const rows = await this.sql<DatasetRow[]>`
|
||||
SELECT *
|
||||
FROM opt_datasets
|
||||
WHERE (${filter?.tenant_id ?? null}::text IS NULL OR tenant_id = ${filter?.tenant_id ?? null})
|
||||
AND (${filter?.project_id ?? null}::text IS NULL OR project_id = ${filter?.project_id ?? null})
|
||||
AND (${filter?.dataset_type ?? null}::text IS NULL OR dataset_type = ${filter?.dataset_type ?? null})
|
||||
AND (${filter?.status ?? null}::text IS NULL OR status = ${filter?.status ?? null})
|
||||
ORDER BY created_at DESC, dataset_id ASC`;
|
||||
return rows.map(toDataset);
|
||||
}
|
||||
}
|
||||
|
||||
export function createDatasetRepository(databaseUrl?: string): DatasetRepository {
|
||||
const mode = resolveStoreMode("0pt.datasets", databaseUrl ?? repositoryDatabaseUrl());
|
||||
return mode.mode === "postgres"
|
||||
? new PostgresDatasetRepository(getRepositorySql(mode.url))
|
||||
: new InMemoryDatasetRepository();
|
||||
}
|
||||
Reference in New Issue
Block a user