Class serializer
@node-ts/bus-class-serializer used class-transformer and @Type decorators to restore Dates and classes in messages. It was removed in 2.0. The default serializer now does the same from generated message types, with no decorators or reflect-metadata. This page covers moving over.
Remove the packages
Remove
@node-ts/bus-class-serializer,class-transformerandreflect-metadatafrom your dependencies, and theimport 'reflect-metadata'line from your entry point.Remove the decorators
Remove
@Type(...), and any other class-transformer decorators, from your messages and workflow state. If nothing else uses decorators, removeexperimentalDecoratorsandemitDecoratorMetadatafrom your tsconfig. A message with aDatefield needs nothing else:tsimport { Event } from '@node-ts/bus-messages' export class CreditCardCharged extends Event { /** * A unique name that identifies the message, in a namespace style such as * organisation/domain/event-name. The bus routes messages by this name. */ static NAME = 'my-app/accounts/credit-card-charged' $name = CreditCardCharged.NAME /** * The contract version of this message. Increment it when the message's * fields change in a way that isn't backwards compatible. */ $version = 1 /** * A credit card was successfully charged * @param creditCardToken Identifies the card that was charged * @param amount The amount, in USD, that the card was charged for * @param chargedAt When the card was charged */ constructor( readonly creditCardToken: string, readonly amount: number, readonly chargedAt: Date ) { super() } }Generate the message types
In the package that declares your messages:
shnpm i -D @node-ts/bus-cli typescript npx bus generate-message-types --entry 'src/**/*.ts' --out src/message-types.generated.tsInclude the files that declare your workflow state, so their Dates and classes are restored too.
Pass them to the bus
Remove
.withSerializer(new ClassSerializer()), and pass the generatedmessageTypesof every message library the service uses, and its own, towithMessageTypes().
The wire format doesn't change, so messages already in your queues and workflow state already saved are read the same way. The full upgrade notes, including what behaves differently, are in Upgrading to 2.0.