examples/session-06-solution の examinationStartedStore の2つの実装を、左の SQLite と右の in-memory で比較します。
SQLite と in-memory の実装の比較
SQLite と in-memory の違い
- SQLite は、
database.transactionに渡した関数の中でsaveStateとappendAuditを呼びます。関数が例外を投げずに完了すると、transaction が確定し、状態と監査記録が同時に残ります。in-memory は、新しいMapと配列nextEventsを作り、appointmentsとeventsを続けて置き換えます。 - SQLite は、
transactionの中で取得したcurrentからAppointment.startExaminationを再度実行し、作り直したcommittedEventの状態を保存します。ユースケースの取得後にreasonなどが変更されていても、変更後の値を古いeventの値で上書きしないためです。in-memory は、渡されたeventのaggregateStateをそのまま保存し、eventもnextEventsに追加します。 - 予約が存在しないとき、SQLite は存在しない場合と
CheckedInではない場合を同じ条件で判定し、どちらもAppointmentConflictをErrで返します。in-memory は存在しない場合にErrorを投げ、reject にします。
examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:149, 163-166 database. transaction((transaction) => {
…
saveState(transaction, committedEvent.aggregateState);
appendAudit(transaction, committedEvent);
return ok(undefined);
}); examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:150, 158-163 const current = findInTransaction(transaction, event.appointmentId);
…
const committedEvent = Appointment. startExamination({
eventId: event.eventId,
occurredAt: event.occurredAt,
})(current, event.aggregateState.veterinarianId);
saveState(transaction, committedEvent.aggregateState); examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:150-156 const current = findInTransaction(transaction, event.appointmentId);
if (current === undefined || current.kind !== "CheckedIn") {
return err({
kind: "AppointmentConflict",
appointmentId: event.appointmentId,
} as const);
} examples/session-06-solution/src/adaptor/secondary/inMemory/inMemoryExaminationStartedStore.ts:59-66 const nextAppointments = new Map(appointments);
nextAppointments. set(event.appointmentId, event.aggregateState);
const nextEvents = [...events, event];
appointments = nextAppointments;
events = nextEvents;
return ok(undefined); examples/session-06-solution/src/adaptor/secondary/inMemory/inMemoryExaminationStartedStore.ts:60-61 nextAppointments. set(event.appointmentId, event.aggregateState);
const nextEvents = [...events, event]; examples/session-06-solution/src/adaptor/secondary/inMemory/inMemoryExaminationStartedStore.ts:48-57 const currentAppointment = appointments. get(event.appointmentId);
if (currentAppointment === undefined) {
throw new Error(`Appointment missing at commit: ${event.appointmentId}`);
}
if (currentAppointment.kind !== "CheckedIn") {
return err({
kind: "AppointmentConflict",
appointmentId: event.appointmentId,
} as const);
} fake である in-memory を使うテストでは、SQLite が予約の欠落を Err で返す動作を確認できません。