Skip to content

Latest commit

 

History

History
110 lines (73 loc) · 2.53 KB

File metadata and controls

110 lines (73 loc) · 2.53 KB

Home > @aws/durable-execution-sdk-js > createClassSerdesWithDates

createClassSerdesWithDates() function

This API is provided as a beta preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Creates a custom Serdes for a class with special handling for Date properties. This implementation is a basic class wrapper and does not support any complex class structures. If you need custom serialization, it is recommended to create your own custom serdes.

Signature:

export declare function createClassSerdesWithDates<T extends object>(
  cls: new () => T,
  dateProps: string[],
): Serdes<T>;

Parameters

Parameter

Type

Description

cls

new () => T

The class constructor (must have no required parameters)

dateProps

string[]

Array of property paths that should be converted to Date objects (supports nested paths like "metadata.createdAt")

Returns:

Serdes<T>

A Serdes that maintains the class type and converts specified properties to Date objects

Example

class Article {
  title: string = "";
  createdAt: Date = new Date();
  metadata: {
    publishedAt: Date;
    updatedAt: Date;
  } = {
    publishedAt: new Date(),
    updatedAt: new Date(),
  };

  getAge() {
    return Date.now() - this.createdAt.getTime();
  }
}

const articleSerdes = createClassSerdesWithDates(Article, [
  "createdAt",
  "metadata.publishedAt",
  "metadata.updatedAt",
]);

// In a durable function:
const article = await context.step(
  "create-article",
  async () => {
    const a = new Article();
    a.title = "My Article";
    return a;
  },
  { serdes: articleSerdes },
);

console.log(article.getAge()); // Works! Dates are properly restored

Limitations: - Class instances becomes plain objects and loses all class information - Constructor must have no parameters - Constructor side-effects will re-run during deserialization - Private fields (#field) cannot be serialized - Getters/setters are not preserved - Nested class instances lose their prototype

For classes with Date properties, use createClassSerdesWithDates instead.