Skip to content

MigrationChain

Defined in: src/persistence/migration/MigrationChain.ts:52

Linear chain of MigrationSteps for a single manifest, terminating at a currentVersion known to the running code. On upcast(stored), the chain locates the step starting at stored.version and applies steps forward until it reaches currentVersion.

The chain is intentionally narrow: one manifest, one current version, one linear path. Multiple types → multiple chains, kept inside an EventAdapter.fromJournal switch by manifest.

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

chain.upcast({ manifest: 'BankAccount.Deposited', version: 1, payload }) returns a DepositedV2.

Errors:

  • manifest mismatch → MigrationError
  • stored.version > currentVersion (downgrade) → MigrationError
  • no step available at the current cursor before currentVersion is reached (chain gap) → MigrationError with the gap printed.

Current

get currentVersion(): number

Defined in: src/persistence/migration/MigrationChain.ts:131

Latest version this chain knows how to produce.

number


get manifest(): string

Defined in: src/persistence/migration/MigrationChain.ts:128

Manifest this chain is bound to.

string

add<F, T>(step): MigrationChain<Current>

Defined in: src/persistence/migration/MigrationChain.ts:71

Append an upcaster. Returns this for chaining.

F

T

MigrationStep<F, T>

MigrationChain<Current>


addDown<F, T>(step): MigrationChain<Current>

Defined in: src/persistence/migration/MigrationChain.ts:103

Append a downcaster — the inverse of MigrationStep. Required when the actor is going to be configured with a writeVersion lower than currentVersion (#7). Steps move backward: fromVersion > toVersion.

chain.addDown({ fromVersion: 2, toVersion: 1, downcast: (e: DepositedV2): DepositedV1 => { const { currency, …rest } = e; void currency; return rest; }, });

F

T

DowncastStep<F, T>

MigrationChain<Current>


downcast(current, targetVersion): unknown

Defined in: src/persistence/migration/MigrationChain.ts:177

Convert a current-shape value down to targetVersion for a write-with-old-shape rolling deploy (#7). Walks the registered downcasters from currentVersion toward targetVersion.

Errors:

  • targetVersion > currentVersionMigrationError (we’re already at or above the target; user should call toJournalAt with version: currentVersion).
  • targetVersion < 1MigrationError.
  • chain gap (no downcaster for the current cursor) → MigrationError with the missing step printed.

Current

number

unknown


toJournalAt(current, writeVersion?): OutboundFrame

Defined in: src/persistence/migration/MigrationChain.ts:213

Build an OutboundFrame for the write path at writeVersion (defaults to currentVersion). Convenience that combines the downcast + frame-shape — exactly what migratingAdapter calls from its toJournal.

Current

number

OutboundFrame


upcast(stored): Current

Defined in: src/persistence/migration/MigrationChain.ts:134

Apply the chain to a stored frame, returning a current-version value.

StoredFrame

Current


static for<C>(manifest, currentVersion): MigrationChain<C>

Defined in: src/persistence/migration/MigrationChain.ts:66

Build an empty chain for manifest whose latest known version is currentVersion.

C

string

number

MigrationChain<C>