ホーム 全セッション S06

状態と監査記録の一括保存

9 / 20 目安 1分

ページ一覧

解決策: 状態と監査記録を1つのイベントにして、store へ1回で渡します。

ExaminationStarted は、診察開始のイベントで、変更後の予約状態 aggregateState を持ちます。ユースケースは、このイベント event を examinationStartedStore の store へ1回渡します。store は、transaction の中で予約を再取得して committedEvent を作り直し、状態と監査記録の両方を保存します。コードは抜粋で、transaction の中の全体は「SQLite と in-memory の実装の比較」のページにあります。

状態と監査記録を1回で保存する

修正後: 成功

  1. ユースケースは store(event) を1回だけ呼びます。store の中で storeAtomically が transaction を実行します。fromSafePromise などの変換は「保存先の障害と例外の伝播」のページで確認します。
  2. storeAtomically が transaction を開始します。transaction の中の書き込みは、確定するまで取り消せます。
  3. transaction の中で予約を再度取得し、current に格納します。CheckedIn であるため先へ進みます。
  4. Appointment.startExamination を再度実行し、再取得した最新の予約 current から committedEvent を作り直して保存します。event の reason はユースケースが取得した時点の値で、取得後に変更されると古くなります。committedEvent は current の最新の reason を持つため、古い値で上書きしません。eventId と occurredAt は event の値を再利用します。
  5. saveState が新しい状態を書き込みます。まだ未確定です。
  6. appendAudit が監査記録を書き込みます。これも未確定です。
  7. 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 の中だけにある行、「確定」は書き込みが残った行です。