Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
53 lines
1.3 KiB
Markdown
53 lines
1.3 KiB
Markdown
## @n8n/di
|
|
|
|
`@n8n/di` is a dependency injection (DI) container library, based on [`typedi`](https://github.com/typestack/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
|
|
|
|
```typescript
|
|
// 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`:
|
|
|
|
```json
|
|
{
|
|
"compilerOptions": {
|
|
"experimentalDecorators": true,
|
|
"emitDecoratorMetadata": true
|
|
}
|
|
}
|
|
```
|