EventVersions
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.
Interfaces
Section titled “Interfaces”EventVersions
Section titled “EventVersions”Defined in: src/EventVersions.ts:44
Type Parameters
Section titled “Type Parameters”Schemas
Section titled “Schemas”Schemas extends ReadonlyArray<VersionSchema>
Latest
Section titled “Latest”Latest
Properties
Section titled “Properties”~latest?
Section titled “~latest?”
readonlyoptional~latest?:Latest
Defined in: src/EventVersions.ts:50
The type of the last version, which an aggregate checks against its event.
schemas
Section titled “schemas”
readonlyschemas:Schemas
Defined in: src/EventVersions.ts:46
Oldest first: schemas[0] is version 1.
upcasts
Section titled “upcasts”
readonlyupcasts: readonly ((previous) =>unknown)[]
Defined in: src/EventVersions.ts:48
upcasts[i] turns version i + 1 into version i + 2.
Methods
Section titled “Methods”next()
Section titled “next()”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.
Type Parameters
Section titled “Type Parameters”Next extends VersionSchema
Parameters
Section titled “Parameters”schema
Section titled “schema”Next
upcast
Section titled “upcast”(previous) => Untagged<Next["Type"]>
Returns
Section titled “Returns”EventVersions<readonly [Schemas, Next], Next["Type"]>
Type Aliases
Section titled “Type Aliases”Any<
Latest> =EventVersions<ReadonlyArray<VersionSchema>,Latest>
Defined in: src/EventVersions.ts:59
Any history whose last version is Latest.
Type Parameters
Section titled “Type Parameters”Latest
Section titled “Latest”Latest = any
SchemaAt
Section titled “SchemaAt”SchemaAt<
H,V> =HextendsEventVersions<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.
Type Parameters
Section titled “Type Parameters”H extends Any
V extends number
Untagged
Section titled “Untagged”Untagged<
T> =Textendsobject?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.
Type Parameters
Section titled “Type Parameters”T
VersionSchema
Section titled “VersionSchema”VersionSchema =
Schema.Top&object
Defined in: src/EventVersions.ts:32
A schema that decodes and encodes without services, as stored payloads need.
Type Declaration
Section titled “Type Declaration”DecodingServices
Section titled “DecodingServices”
readonlyDecodingServices:never
EncodingServices
Section titled “EncodingServices”
readonlyEncodingServices:never
Functions
Section titled “Functions”current()
Section titled “current()”current(
versions):number
Defined in: src/EventVersions.ts:80
The version new events of this kind are written with.
Parameters
Section titled “Parameters”versions
Section titled “versions”Any<any> | undefined
Returns
Section titled “Returns”number
decoders()
Section titled “decoders()”decoders(
tag,versions): readonlyDecoder<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.
Parameters
Section titled “Parameters”string
versions
Section titled “versions”Returns
Section titled “Returns”readonly Decoder<unknown, never>[]
from()
Section titled “from()”from<
V1>(schema):EventVersions<readonly [V1],V1["Type"]>
Defined in: src/EventVersions.ts:76
Starts a history at version 1.
Type Parameters
Section titled “Type Parameters”V1 extends VersionSchema
Parameters
Section titled “Parameters”schema
Section titled “schema”V1
Returns
Section titled “Returns”EventVersions<readonly [V1], V1["Type"]>