状態の保存と監査記録の保存を、別々に続けて2回実行する形を、dual-write と呼びます。2回目の保存が失敗すると、予約は診察中ですが、監査記録が残りません。順序を入れ替えると、監査記録だけが残ります。
状態と監査記録を1回で保存する
修正前: 2回の保存
eventの生成が完了しました。予約はまだCheckedInで、監査記録はありません。stateStore.saveはsaveState(database, …)で、databaseに直接書き込みます。transaction が無いため、InExaminationの書き込みはこの時点で確定します(図の表示は「書き込み済み」)。- 次に
eventLog.appendが監査記録の書き込みを試みますが失敗し、例外を投げます。 - 外側が例外を受け取り、500 を返します。状態は
InExaminationのまま、ExaminationStartedの監査記録はありません。誰が診察を開始したかを後から確認できません。
呼び出し元のコード
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);
}); 「保存済み」ははじめからある行、「書き込み済み」は書き込まれた行です。破線の空枠は、まだ行がない場所です。