Skip to content

The older shapes of one event, so that events stored long ago still reach projections and view models in today’s shape. The log is never rewritten: old payloads are upcast as they are read.

const AccountOpenedV1 = Schema.Struct({ owner: Schema.String })
const Account = Aggregate.make<State>()({
name: "account",
events: AccountEvent,
initial,
projection,
versions: {
AccountOpened: EventVersions.from(AccountOpenedV1).next(
AccountEvent.cases.AccountOpened,
({ owner }) => ({ owner, currency: "CHF" })
)
}
})

The first schema is version 1, the shape events had before the history was declared, and every next adds a version. The last one must be the event’s current schema, which is what new events are written with.

Defined in: src/EventVersions.ts:44

Schemas extends ReadonlyArray<VersionSchema>

Latest

readonly optional ~latest?: Latest

Defined in: src/EventVersions.ts:50

The type of the last version, which an aggregate checks against its event.

readonly schemas: Schemas

Defined in: src/EventVersions.ts:46

Oldest first: schemas[0] is version 1.

readonly upcasts: readonly ((previous) => unknown)[]

Defined in: src/EventVersions.ts:48

upcasts[i] turns version i + 1 into version i + 2.

next<Next>(schema, upcast): EventVersions<readonly [Schemas, Next], Next["Type"]>

Defined in: src/EventVersions.ts:52

Adds a version, with the function that turns the previous one into it.

Next extends VersionSchema

Next

(previous) => Untagged<Next["Type"]>

EventVersions<readonly [Schemas, Next], Next["Type"]>

Any<Latest> = EventVersions<ReadonlyArray<VersionSchema>, Latest>

Defined in: src/EventVersions.ts:59

Any history whose last version is Latest.

Latest = any


SchemaAt<H, V> = H extends EventVersions<infer Schemas, any> ? [never, ...Schemas][V] extends infer S ? S : never : never

Defined in: src/EventVersions.ts:62

The schema of one version, or never when the history has no such version.

H extends Any

V extends number


Untagged<T> = T extends object ? Omit<T, "_tag"> : T

Defined in: src/EventVersions.ts:38

What an upcast returns: the next version, without the _tag the event’s name already gives.

T


VersionSchema = Schema.Top & object

Defined in: src/EventVersions.ts:32

A schema that decodes and encodes without services, as stored payloads need.

readonly DecodingServices: never

readonly EncodingServices: never

current(versions): number

Defined in: src/EventVersions.ts:80

The version new events of this kind are written with.

Any<any> | undefined

number


decoders(tag, versions): readonly Decoder<unknown, never>[]

Defined in: src/EventVersions.ts:90

Builds one decoder per old version, each going all the way to the current one. The chains are composed once with Schema.decodeTo, so reading an old event costs one decode however many versions it is behind.

decoders[v - 1] decodes a JSON payload stored at version v. The current version has none, as the aggregate’s own schema reads it.

string

Any

readonly Decoder<unknown, never>[]


from<V1>(schema): EventVersions<readonly [V1], V1["Type"]>

Defined in: src/EventVersions.ts:76

Starts a history at version 1.

V1 extends VersionSchema

V1

EventVersions<readonly [V1], V1["Type"]>