Skip to main content

Factory

Callable

  • Factory<T>(type: string | Constructable<T>, options?: { metadata?: {} }): (constructor: { create: (...args: unknown[]) => Promise<T> }) => void

  • A @Factory is a class decorator to be placed on top of a class that represents a factory. A factory is responsible for creating instances of a class.

    It is possible to generate multiple factories for the same class by assigning a unique set of metadata to the factory. Of all registered factories, only one can be the default factory.

    @example
    // Factory definition.
    @Factory<MyService>(MyService, { metadata: { key: 'value' } })
    export class MyServiceFactory {
    static async create(arg1, arg2, ...): Promise<MyService> { ... }>
    }

    // Get instance using default factory.
    const myService = await Container.get(MyService);

    // Get instance using specific factory.
    const myService = await Container.get(MyService, { metadata: { key: 'value' } });

    // Pass args to factory.
    const myService = await Container.get(MyService, { args: [arg1, arg2, ...] });

    Type parameters

    • T

    Parameters

    • type: string | Constructable<T>

      The type of class that the factory creates.

    • optionaloptions: { metadata?: {} }

      Allows registering multiple factories for the same class that differ by metadata.

    Returns (constructor: { create: (...args: unknown[]) => Promise<T> }) => void

    The decorated class.

      • (constructor: { create: (...args: unknown[]) => Promise<T> }): void
      • Parameters

        • constructor: { create: (...args: unknown[]) => Promise<T> }

        Returns void