Skip to content

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.

  1. Remove the packages

    Remove @node-ts/bus-class-serializer, class-transformer and reflect-metadata from your dependencies, and the import 'reflect-metadata' line from your entry point.

  2. Remove the decorators

    Remove @Type(...), and any other class-transformer decorators, from your messages and workflow state. If nothing else uses decorators, remove experimentalDecorators and emitDecoratorMetadata from your tsconfig. A message with a Date field needs nothing else:

    ts
    import { 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()
      }
    }
  3. Generate the message types

    In the package that declares your messages:

    sh
    npm i -D @node-ts/bus-cli typescript
    npx bus generate-message-types --entry 'src/**/*.ts' --out src/message-types.generated.ts

    Include the files that declare your workflow state, so their Dates and classes are restored too.

  4. Pass them to the bus

    Remove .withSerializer(new ClassSerializer()), and pass the generated messageTypes of every message library the service uses, and its own, to withMessageTypes().

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.

See also ​

Released under the MIT License.