81 lines
2.8 KiB
TypeScript
81 lines
2.8 KiB
TypeScript
/**
|
|
* Owns 0pt.dataset.schema.v1 — the canonical registry of 0pt datasets.
|
|
*
|
|
* Storage is Postgres-backed through DatasetRepository whenever DATABASE_URL
|
|
* is configured. Non-production tests/local development may use the in-memory
|
|
* repository; production without a persistent store fails at construction via
|
|
* resolveStoreMode. Dataset content remains outside this registry.
|
|
*/
|
|
import { createAud0Client, type Aud0Client } from "../clients/aud0-client";
|
|
import {
|
|
createDatasetRepository,
|
|
type DatasetListFilter,
|
|
type DatasetRepository,
|
|
} from "../repositories/dataset-repository";
|
|
import type { Dataset, DatasetStatus, DatasetType, CreateDatasetInput } from "../types/dataset";
|
|
|
|
export type { Dataset, DatasetStatus, DatasetType, CreateDatasetInput };
|
|
export type RegisterDatasetInput = CreateDatasetInput;
|
|
|
|
export interface DatasetServiceOptions {
|
|
aud0BaseUrl?: string;
|
|
databaseUrl?: string;
|
|
repository?: DatasetRepository;
|
|
}
|
|
|
|
function normalizeOptions(input?: string | DatasetServiceOptions): DatasetServiceOptions {
|
|
return typeof input === "string" ? { aud0BaseUrl: input } : input ?? {};
|
|
}
|
|
|
|
export class DatasetService {
|
|
private readonly repo: DatasetRepository;
|
|
private readonly aud0: Aud0Client;
|
|
|
|
/**
|
|
* A string argument remains supported as the historical aud0BaseUrl form;
|
|
* new callers should use the options object so repository/database injection
|
|
* is explicit and testable.
|
|
*/
|
|
constructor(input?: string | DatasetServiceOptions) {
|
|
const opts = normalizeOptions(input);
|
|
this.aud0 = createAud0Client(opts.aud0BaseUrl);
|
|
this.repo = opts.repository ?? createDatasetRepository(opts.databaseUrl);
|
|
}
|
|
|
|
async registerDataset(input: CreateDatasetInput): Promise<Dataset> {
|
|
const dataset = await this.repo.create(input);
|
|
|
|
await this.aud0.ingestEvent({
|
|
event_id: `evt_0pt_dataset_registered_${dataset.dataset_id}`,
|
|
event_type: "0pt.dataset_registered",
|
|
tenant_id: dataset.tenant_id,
|
|
project_id: dataset.project_id,
|
|
actor_id: dataset.created_by ?? "system",
|
|
app_id: "0pt",
|
|
capability_id: "0pt.register_dataset",
|
|
resource_type: "dataset",
|
|
resource_id: dataset.dataset_id,
|
|
risk_level: "L2",
|
|
payload: { name: dataset.name, dataset_type: dataset.dataset_type },
|
|
created_at: dataset.created_at,
|
|
correlation_id: dataset.correlation_id,
|
|
idempotency_key: `dataset-register:${dataset.tenant_id}:${dataset.dataset_id}`,
|
|
});
|
|
|
|
return dataset;
|
|
}
|
|
|
|
async getDataset(datasetId: string): Promise<Dataset> {
|
|
return this.repo.get(datasetId);
|
|
}
|
|
|
|
async listDatasets(filter?: {
|
|
tenant_id?: string;
|
|
project_id?: string;
|
|
dataset_type?: DatasetType;
|
|
status?: DatasetStatus;
|
|
}): Promise<Dataset[]> {
|
|
return this.repo.list(filter as DatasetListFilter | undefined);
|
|
}
|
|
}
|