Add project revision foundation

This commit is contained in:
2026-07-23 21:17:48 +02:00
parent 6b6d2c2a42
commit 903d977443
16 changed files with 2052 additions and 9 deletions
@@ -0,0 +1,123 @@
import crypto from "node:crypto";
import { and, eq } from "drizzle-orm";
import type {
AppendProjectRevisionInput,
AppendedProjectRevision,
ProjectRevisionStore,
} from "../../domain/ports/project-revision.store.js";
import type { AppDatabase } from "../database-context.js";
import { projectChangeSets } from "../schema/project-change-sets.js";
import { projectRevisions } from "../schema/project-revisions.js";
import { projects } from "../schema/projects.js";
export class ProjectRevisionConflictError extends Error {
constructor(
readonly projectId: string,
readonly expectedRevision: number,
readonly actualRevision: number | null
) {
super(
actualRevision === null
? `Project ${projectId} does not exist.`
: `Project ${projectId} is at revision ${actualRevision}, expected ${expectedRevision}.`
);
this.name = "ProjectRevisionConflictError";
}
}
export class ProjectRevisionRepository implements ProjectRevisionStore {
constructor(private readonly database: AppDatabase) {}
getCurrentRevision(projectId: string) {
const project = this.database
.select({ currentRevision: projects.currentRevision })
.from(projects)
.where(eq(projects.id, projectId))
.get();
return project?.currentRevision ?? null;
}
append(input: AppendProjectRevisionInput): AppendedProjectRevision {
this.validateInput(input);
const revisionId = crypto.randomUUID();
const changeSetId = crypto.randomUUID();
const revisionNumber = input.expectedRevision + 1;
const createdAtIso = new Date().toISOString();
return this.database.transaction((tx) => {
const revisionUpdate = tx
.update(projects)
.set({ currentRevision: revisionNumber })
.where(
and(
eq(projects.id, input.projectId),
eq(projects.currentRevision, input.expectedRevision)
)
)
.run();
if (revisionUpdate.changes !== 1) {
const current = tx
.select({ currentRevision: projects.currentRevision })
.from(projects)
.where(eq(projects.id, input.projectId))
.get();
throw new ProjectRevisionConflictError(
input.projectId,
input.expectedRevision,
current?.currentRevision ?? null
);
}
tx.insert(projectRevisions)
.values({
id: revisionId,
projectId: input.projectId,
revisionNumber,
createdAtIso,
actorId: input.actorId,
source: input.source,
description: input.description,
})
.run();
tx.insert(projectChangeSets)
.values({
id: changeSetId,
projectRevisionId: revisionId,
commandType: input.commandType,
payloadSchemaVersion: input.forward.schemaVersion,
forwardPayloadJson: JSON.stringify(input.forward.data),
inversePayloadJson: JSON.stringify(input.inverse.data),
})
.run();
return {
revisionId,
changeSetId,
projectId: input.projectId,
revisionNumber,
createdAtIso,
};
});
}
private validateInput(input: AppendProjectRevisionInput) {
if (!Number.isSafeInteger(input.expectedRevision) || input.expectedRevision < 0) {
throw new Error("Expected project revision must be a non-negative integer.");
}
if (!input.commandType.trim()) {
throw new Error("Project revision command type is required.");
}
if (
!Number.isSafeInteger(input.forward.schemaVersion) ||
input.forward.schemaVersion < 1 ||
input.forward.schemaVersion !== input.inverse.schemaVersion
) {
throw new Error(
"Forward and inverse payloads require the same positive schema version."
);
}
}
}
@@ -19,6 +19,7 @@ export class ProjectRepository {
name: input.name,
singlePhaseVoltageV: input.singlePhaseVoltageV ?? 230,
threePhaseVoltageV: input.threePhaseVoltageV ?? 400,
currentRevision: 0,
};
await db.insert(projects).values(project);
return project;