2.0.6 • Published 11 months ago

class-transformer-storage v2.0.6

Weekly downloads
-
License
-
Repository
github
Last release
11 months ago

class-transformer-storage

Extends the powerful package class-transformer with a global storage mechanism for constructors and a dedicated function to dynamically instantiate class instances from constructors stored in the global storage.

Usage

The ideal use case is via Typescript decorators. Any class can be decorated with the @RegisterType() decorator to add to the global storage to then be recalled using the ctPlainToInstance function. By default the function will reference the name property of a plain object to determine which constructor to use. This can be overridden in the parameters with the getName option which takes the plain version of the object as a parameter.

// Define the type and decorate
@RegisterType()
class DemoType {
  public hello: string;

  constructor() {
    this.name = this.constructor.name;
  }
}

const instance = new DemoType();

// Convert to plain object using class transformer
const plain = instanceToPlain(instance);

const recreatedInstance = ctPlainToInstance(plain);

console.log(recreatedInstance instanceof DemoType); // true

The global store can be referenced manually as well by calling the CTStore static methods directly.

2.0.6

11 months ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago