Skip to content

migratingAdapter

migratingAdapter<E>(chain, opts?): EventAdapter<E, unknown>

Defined in: src/persistence/migration/migratingAdapter.ts:31

Build an EventAdapter from a MigrationChain — convenience for the common case where one chain handles both directions (upcast on read, optional downcast on write).

writeVersion (defaults to the chain’s currentVersion) is the version actually written to the journal. Set it lower than currentVersion during a rolling deployment so v2 nodes keep emitting v1 events for as long as v1 readers are still in the cluster (#7). When writeVersion < currentVersion, the chain must have downcasters covering every step on the path currentVersion → writeVersion — otherwise toJournal throws.

const chain = MigrationChain.for(‘Deposited’, 2) .add({ fromVersion: 1, toVersion: 2, upcast: (v: DepositedV1): DepositedV2 => ({ …v, currency: ‘USD’ }) }) .addDown({ fromVersion: 2, toVersion: 1, downcast: (v: DepositedV2): DepositedV1 => { const { currency, …rest } = v; void currency; return rest as DepositedV1; } });

// Phase 1 of rollout — write v1 still, read both: const phase1 = migratingAdapter(chain, { writeVersion: 1 });

// Phase 2 once every reader is on the new code — flip: const phase2 = migratingAdapter(chain); // writeVersion = currentVersion = 2

E

MigrationChain<E>

number

EventAdapter<E, unknown>