Add serializable project commands

This commit is contained in:
2026-07-23 21:23:31 +02:00
parent 903d977443
commit b79faed320
9 changed files with 206 additions and 39 deletions
@@ -1,5 +1,6 @@
import crypto from "node:crypto";
import { and, eq } from "drizzle-orm";
import { serializeProjectCommand } from "../../domain/models/project-command.model.js";
import type {
AppendProjectRevisionInput,
AppendedProjectRevision,
@@ -40,6 +41,8 @@ export class ProjectRevisionRepository implements ProjectRevisionStore {
append(input: AppendProjectRevisionInput): AppendedProjectRevision {
this.validateInput(input);
const forwardPayloadJson = serializeProjectCommand(input.forward);
const inversePayloadJson = serializeProjectCommand(input.inverse);
const revisionId = crypto.randomUUID();
const changeSetId = crypto.randomUUID();
const revisionNumber = input.expectedRevision + 1;
@@ -86,10 +89,10 @@ export class ProjectRevisionRepository implements ProjectRevisionStore {
.values({
id: changeSetId,
projectRevisionId: revisionId,
commandType: input.commandType,
commandType: input.forward.type,
payloadSchemaVersion: input.forward.schemaVersion,
forwardPayloadJson: JSON.stringify(input.forward.data),
inversePayloadJson: JSON.stringify(input.inverse.data),
forwardPayloadJson,
inversePayloadJson,
})
.run();
@@ -107,16 +110,11 @@ export class ProjectRevisionRepository implements ProjectRevisionStore {
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."
"Forward and inverse project commands require the same schema version."
);
}
}