予約を取得した後、保存の前に、別の依頼が先に診察を開始していた場合を、競合と呼びます。examinationStartedStore は、保存の時点で予約が CheckedIn でなければ、競合として AppointmentConflict を Err で返します。
状態と監査記録を1回で保存する
修正後: 競合
- ユースケースが予約を取得した後、保存の前に、別の依頼が先に診察を開始していました。データベースの予約はすでに
InExaminationで、audit_logs には先の依頼のExaminationStartedが保存済みです。 - transaction の中で再取得した予約の
current.kindは、CheckedInではありません。 currentがCheckedInではないため、何も書き込まずにerrでAppointmentConflictを返します。current === undefined(予約が存在しない)のときも、同じAppointmentConflictを返します。関数は例外を投げずに完了するため transaction は確定しますが、書き込みの前に返すため、状態も監査記録も増えず、audit_logs は先の依頼の1行のままです。
呼び出し元のコード
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);
}); 「保存済み」ははじめからある行、「未確定」は transaction の中だけにある行、「確定」は書き込みが残った行です。
修正前のコードの保存は Promise<void> を返すだけで、この競合を Err で返せません。この教材のコードでは、SQLite が transaction を1つずつ順に実行するため、再取得と書き込みの間に別の依頼の書き込みが入りません。ほかのデータベースで競合を検出する方法は、Final の条件付き更新で扱います。