n8n/packages/@n8n/di
कारतोफ्फेलस्क्रिप्ट™ 39d5e0ff87
Some checks failed
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
Benchmark Docker Image CI / build (push) Has been cancelled
refactor(core): Replace typedi with our custom DI system (no-changelog) (#12389)
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
2025-01-06 10:21:24 +01:00
..
src refactor(core): Replace typedi with our custom DI system (no-changelog) (#12389) 2025-01-06 10:21:24 +01:00
.eslintrc.js refactor(core): Replace typedi with our custom DI system (no-changelog) (#12389) 2025-01-06 10:21:24 +01:00
jest.config.js refactor(core): Replace typedi with our custom DI system (no-changelog) (#12389) 2025-01-06 10:21:24 +01:00
package.json refactor(core): Replace typedi with our custom DI system (no-changelog) (#12389) 2025-01-06 10:21:24 +01:00
README.md refactor(core): Replace typedi with our custom DI system (no-changelog) (#12389) 2025-01-06 10:21:24 +01:00
tsconfig.build.json refactor(core): Replace typedi with our custom DI system (no-changelog) (#12389) 2025-01-06 10:21:24 +01:00
tsconfig.json refactor(core): Replace typedi with our custom DI system (no-changelog) (#12389) 2025-01-06 10:21:24 +01:00

@n8n/di

@n8n/di is a dependency injection (DI) container library, based on typedi.

n8n no longer uses typedi because:

  • typedi is no longer officially maintained
  • Need for future-proofing, e.g. stage-3 decorators
  • Small enough that it is worth the maintenance burden
  • Easier to customize, e.g. to simplify unit tests

Usage

// from https://github.com/typestack/typedi/blob/develop/README.md
import { Container, Service } from 'typedi';

@Service()
class ExampleInjectedService {
  printMessage() {
    console.log('I am alive!');
  }
}

@Service()
class ExampleService {
  constructor(
    // because we annotated ExampleInjectedService with the @Service()
    // decorator TypeDI will automatically inject an instance of
    // ExampleInjectedService here when the ExampleService class is requested
    // from TypeDI.
    public injectedService: ExampleInjectedService
  ) {}
}

const serviceInstance = Container.get(ExampleService);
// we request an instance of ExampleService from TypeDI

serviceInstance.injectedService.printMessage();
// logs "I am alive!" to the console

Requires enabling these flags in tsconfig.json:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}