injector.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { Type } from '@nestjs/common';
  2. import { ContextId, ModuleRef } from '@nestjs/core';
  3. import { getConnectionToken } from '@nestjs/typeorm';
  4. import { Connection } from 'typeorm';
  5. /**
  6. * @description
  7. * The Injector wraps the underlying Nestjs `ModuleRef`, allowing injection of providers
  8. * known to the application's dependency injection container. This is intended to enable the injection
  9. * of services into objects which exist outside of the Nestjs module system, e.g. the various
  10. * Strategies which can be supplied in the VendureConfig.
  11. *
  12. * @docsCategory common
  13. */
  14. export class Injector {
  15. constructor(private moduleRef: ModuleRef) {}
  16. /**
  17. * @description
  18. * Retrieve an instance of the given type from the app's dependency injection container.
  19. * Wraps the Nestjs `ModuleRef.get()` method.
  20. */
  21. get<T, R = T>(typeOrToken: Type<T> | string | symbol): R {
  22. return this.moduleRef.get(typeOrToken, { strict: false });
  23. }
  24. /**
  25. * @description
  26. * Retrieve an instance of the given scoped provider (transient or request-scoped) from the
  27. * app's dependency injection container.
  28. * Wraps the Nestjs `ModuleRef.resolve()` method.
  29. */
  30. resolve<T, R = T>(typeOrToken: Type<T> | string | symbol, contextId?: ContextId): Promise<R> {
  31. return this.moduleRef.resolve(typeOrToken, contextId, { strict: false });
  32. }
  33. }