ExaminationStarted は、診察開始のイベントで、変更後の予約状態 aggregateState を持ちます。ユースケースは、このイベント event を examinationStartedStore の store へ1回渡します。store は、transaction の中で予約を再取得して committedEvent を作り直し、状態と監査記録の両方を保存します。コードは抜粋で、transaction の中の全体は「SQLite と in-memory の実装の比較」のページにあります。
状態と監査記録を1回で保存する
修正後: 成功
- ユースケースは
store(event)を1回だけ呼びます。storeの中でstoreAtomicallyが transaction を実行します。fromSafePromiseなどの変換は「保存先の障害と例外の伝播」のページで確認します。 storeAtomicallyが transaction を開始します。transaction の中の書き込みは、確定するまで取り消せます。- transaction の中で予約を再度取得し、
currentに格納します。CheckedInであるため先へ進みます。 Appointment.startExaminationを再度実行し、再取得した最新の予約currentからcommittedEventを作り直して保存します。eventのreasonはユースケースが取得した時点の値で、取得後に変更されると古くなります。committedEventはcurrentの最新のreasonを持つため、古い値で上書きしません。eventIdとoccurredAtはeventの値を再利用します。saveStateが新しい状態を書き込みます。まだ未確定です。appendAuditが監査記録を書き込みます。これも未確定です。database.transactionに渡した関数が例外を投げずに完了すると、transaction が確定し、状態と監査記録が同時に残ります。関数の戻り値はok(undefined)です。
呼び出し元のコード
examples/session-06/src/useCase/startExamination.ts:69-70 呼び出し元 await deps.stateStore. save(event.aggregateState);
await deps.eventLog. append(event); examples/session-06-solution/src/useCase/startExamination.ts:89 呼び出し元 .andThrough(deps.examinationStartedStore.store) 実装のコード
examples/session-06/src/adaptor/secondary/sqlite/appointmentStore.ts:182, 100-102 実装 save: async (appointment) => saveState(database, appointment),
…
executor
.insert(appointmentsTable)
.values(row) examples/session-06/src/adaptor/secondary/sqlite/appointmentStore.ts:185, 118, 120 実装 append: async (event) => appendAudit(database, event),
…
executor. insert(auditLogsTable). values(toAuditRow(event)). run();
…
throw new AppointmentPersistenceError("append-audit", cause); examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:185-188 実装 store: (event) =>
ResultAsync. fromSafePromise(
Promise.resolve(). then(() => storeAtomically(event)),
). andThen((result) => result), examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:148-149 実装 const storeAtomically = (event: ExaminationStarted) =>
database. transaction((transaction) => {
… examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:150-151 実装 const current = findInTransaction(transaction, event.appointmentId);
if (current === undefined || current.kind !== "CheckedIn") {
… examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:158-161 実装 const committedEvent = Appointment. startExamination({
eventId: event.eventId,
occurredAt: event.occurredAt,
})(current, event.aggregateState.veterinarianId); examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:151-156 実装 if (current === undefined || current.kind !== "CheckedIn") {
return err({
kind: "AppointmentConflict",
appointmentId: event.appointmentId,
} as const);
} examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:163, 99-101, 106 実装 saveState(transaction, committedEvent.aggregateState);
…
executor
.insert(appointmentsTable)
.values(row)
…
.run(); examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:164, 117, 119 実装 appendAudit(transaction, committedEvent);
…
executor. insert(auditLogsTable). values(toAuditRow(event)). run();
…
throw new AppointmentPersistenceError("append-audit", cause); examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:149, 165-166 実装 database. transaction((transaction) => {
…
return ok(undefined);
}); event.aggregateState.reason "skin check" 取得時の値 committedEvent.aggregateState.reason "vaccination" 最新の値 「保存済み」ははじめからある行、「未確定」は transaction の中だけにある行、「確定」は書き込みが残った行です。