File size: 14,624 Bytes
b410f5c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | # Backend module
A backend module is a self-contained unit of backend functionality tied to a specific n8n feature.
Benefits of modularity:
- **Organization:** Keep code structured, focused and discoverable
- **Independence**: Reduce conflicts among teams working independently
- **Decoupling**: Prevent features from becoming entangled with internals
- **Simplicity:** Make logic easier to maintain, test and reason about
- **Efficiency**: Load and run only modules that are explicitly enabled
## File structure
To set up a backend module, run this command at monorepo root:
```sh
pnpm setup-backend-module
```
Your new module will be located at `packages/cli/src/modules/my-feature`. Rename `my-feature` in the dirname and in the filenames to your actual feature name. Use kebab-case.
A moduleβs file structure is as follows:
```sh
.
βββ __tests__
βΒ Β βββ my-feature.controller.test.ts
βΒ Β βββ my-feature.service.test.ts
βββ my-feature.entity.ts # DB model
βββ my-feature.repository.ts # DB access
βββ my-feature.config.ts # env vars
βββ my-feature.constants.ts # constants
βββ my-feature.controller.ts # HTTP REST routes
βββ my-feature.service.ts # business logic
βββ my-feature.module.ts # entrypoint
```
This is only a template - your module may not need all of these files, or it may need more than these and also subdirs to keep things organized. Infixes are currently not required (except for the `.module.ts` entrypoint), but infixes are strongly recommended as they make the contents searchable and discoverable.
Backend modules currently live at `packages/cli/src/modules`, so imports can be:
- from inside the module dir
- from common packages like `@n8n/db`, `@n8n/backend-common`, `@n8n/backend-test-utils`, etc.
- from `cli`
- from third-party libs available in, or added to, `cli`
Modules are managed via env vars:
- To enable a module (activate it on instance startup), use the env var `N8N_ENABLED_MODULES`.
- To disable a module (skip it on instance startup), use the env var `N8N_DISABLED_MODULES`.
- Some modules are **default modules** so they are always enabled unless specifically disabled. To enable a module by default, add it [here](https://github.com/n8n-io/n8n/blob/c0360e52afe9db37d4dd6e00955fa42b0c851904/packages/%40n8n/backend-common/src/modules/module-registry.ts#L26).
Modules that are under a license flag are automatically skipped on startup if the instance is not licensed to use the feature.
## Entrypoint
The `.module.ts` file is the entrypoint of the module and uses the `@BackendModule()` decorator:
```ts
@BackendModule({ name: 'my-feature' })
export class MyFeatureModule implements ModuleInterface {
async init() {
await import('./my-feature.controller');
const { MyFeatureService } = await import('./my-feature.service');
Container.get(MyFeatureService).start();
}
@OnShutdown()
async shutdown() {
const { MyFeatureService } = await import('./my-feature.service');
await Container.get(MyFeatureService).shutdown();
}
async entities() {
const { MyEntity } = await import('./my-feature.entity.ts');
return [MyEntity]
}
async settings() {
const { MyFeatureService } = await import('./my-feature.service');
return Container.get(MyFeatureService).settings();
}
async context() {
const { MyFeatureService } = await import('./my-feature.service');
return { myFeatureProxy: Container.get(MyFeatureService) };
}
}
```
The entrypoint is responsible for providing:
- **initialization logic**, e.g. in insights, start compaction timers,
- **shutdown logic**, e.g. in insights, stop compaction timers,
- **database entities** to register with `typeorm`, e.g. in insights, the three database entities `InsightsMetadata`, `InsightsByPeriod` and `InsightsRaw`
- **settings** to send to the client for adjusting the UI, e.g. in insights, `{ summary: true, dashboard: false }`
- **context** to merge an object into the workflow execution context `WorkflowExecuteAdditionalData`. This allows you to make module functionality available to `core`, namespaced under the module name. For now, you will also need to manually [update the type](https://github.com/n8n-io/n8n/blob/master/packages/core/src/execution-engine/index.ts#L7) of `WorkflowExecuteAdditionalData` to reflect the resulting context.
A module entrypoint may or may not need to implement all of these methods.
Why do we use dynamic imports in entrypoint methods? `await import('...')` ensures we load module-specific logic **only when needed**, so that n8n instances which do not have a module enabled, or do not have licensed access to it, do not pay for the performance cost. Linting enforces that relative imports in the entrypoint use dynamic imports. Loading on demand is also the reason why the entrypoint does not use dependency injection, and forbids it via linting.
A module may be fully behind a license flag:
```ts
@BackendModule({
name: 'external-secrets',
licenseFlag: 'feat:externalSecrets'
})
export class ExternalSecretsModule implements ModuleInterface {
// This module will be activated only if the license flag is true.
}
```
A module may be restricted to specific instance types:
```ts
@BackendModule({
name: 'my-feature',
instanceTypes: ['main', 'webhook']
})
export class MyFeatureModule implements ModuleInterface {
// This module will only be initialized on main and webhook instances,
// not on worker instances.
}
```
If `instanceTypes` is omitted, the module will be initialized on all instance types (`main`, `webhook`, and `worker`).
If a module is only _partially_ behind a license flag, e.g. insights, then use the `@Licensed()` decorator instead:
```ts
class InsightsController {
constructor(private readonly insightsService: InsightsService) {}
@Get('/by-workflow')
@Licensed('feat:insights:viewDashboard')
async getInsightsByWorkflow(
_req: AuthenticatedRequest,
_res: Response,
@Query payload: ListInsightsWorkflowQueryDto,
) {
const dateRangeAndMaxAgeInDays = this.getMaxAgeInDaysAndGranularity({
dateRange: payload.dateRange ?? 'week',
});
return await this.insightsService.getInsightsByWorkflow({
maxAgeInDays: dateRangeAndMaxAgeInDays.maxAgeInDays,
skip: payload.skip,
take: payload.take,
sortBy: payload.sortBy,
});
}
}
```
Module-level decorators to be aware of:
- `@OnShutdown()` to register a method to be called during the instance shutdown sequence
## Controller
To register a controller with the server, simply import the controller file in the module entrypoint:
```ts
@BackendModule({ name: 'my-feature' })
export class MyFeatureModule implements ModuleInterface {
async init() {
await import('./my-feature.controller');
}
}
```
A controller must handle only request-response orchestration, delegating business logic to services.
```ts
@RestController('/my-feature')
export class MyFeatureController {
constructor(private readonly myFeatureService: MyFeatureService) {}
@Get('/summary')
async getSummary(_req: AuthenticatedRequest, _res: Response) {
return await this.myFeatureService.getSummary();
}
}
```
Controller-level decorators to be aware of:
- `@RestController()` to define a REST controller
- `@Get()`, `@Post()`, `@Put()`, etc. to define controller routes
- `@Query()`, `@Body()`, etc. to validate requests
- `@GlobalScope()` to gate a method depending on a user's instance-wide permissions
- `@ProjectScope()` to gate a method depending on a user's project-specific permissions
- `@Licensed()` to gate a method depending on license state
## Services
Services handle business logic, delegating database access to repositories.
To kickstart a service during module init, run the relevant method in the module entrypoint:
```ts
@BackendModule({ name: 'my-feature' })
export class MyFeatureModule implements ModuleInterface {
async init() {
const { MyFeatureService } = await import('./my-feature.service');
Container.get(MyFeatureService).start();
}
}
```
A module typically has one service, but it may have as many as needed.
```ts
@Service()
export class MyFeatureService {
private intervalId?: NodeJS.Timeout;
constructor(
private readonly myFeatureRepository: MyFeatureRepository,
private readonly logger: Logger,
private readonly config: MyFeatureConfig,
) {
this.logger = this.logger.scoped('my-feature');
}
start() {
this.logger.debug('Starting feature work...');
this.intervalId = setInterval(
() => {
this.logger.debug('Running scheduled task...');
},
this.config.taskInterval * 60 * 1000,
);
}
```
Service-level decorators to be aware of:
- `@Service()` to make a service usable by the dependency injection container
- `@OnLifecycleEvent()` to register a class method to be called on an execution lifecycle event, e.g. `nodeExecuteBefore`, `nodeExecuteAfter`, `workflowExecuteBefore`, and `workflowExecuteAfter`
- `@OnPubSubEvent()` to register a class method to be called on receiving a message via Redis pubsub
- `@OnLeaderTakeover()` and `@OnLeaderStepdown` to register a class method to be called on leadership transition in a multi-main setup
## Repositories
Repositories handle database access using `typeorm`, operating on database models ("entities").
```ts
@Service()
export class MyFeatureRepository extends Repository<MyFeatureEntity> {
constructor(dataSource: DataSource) {
super(MyFeatureEntity, dataSource.manager);
}
async getSummary() {
return await /* typeorm query on entities */;
}
}
```
Repository-level decorators to be aware of:
- `@Service()` to make a repository usable by the dependency injection container
## Entities
Entities are database models, typically representing tables in the database.
```ts
@Entity()
export class MyFeatureEntity extends BaseEntity {
@Column()
name: string;
@Column()
count: number;
}
```
We recommend using the `.entity.ts` infix, but omit the `-Entity` suffix from the class name to make it less verbose. (The `-Entity` suffix is being used in the setup example only for consistency with other placeholders.)
Entities must be registered with `typeorm` in the module entrypoint:
```ts
class MyFeatureModule implements ModuleInterface {
async entities() {
const { MyFeatureEntity } = await import('./my-feature.entity');
return [MyFeatureEntity];
}
}
```
Entity-level decorators to be aware of:
- `@Entity()` to define an entity
## Migrations
As an exception, migrations remain centralized at `@n8n/db/src/migrations`, because conditionally running migrations would introduce unwanted complexity at this time. This means that schema changes from modules are _always_ applied to the database, even when modules are disabled.
## Configuration
Module-specific configuration is defined in the the `.config.ts` file:
```ts
@Config
export class MyFeatureConfig {
/**
* How often in minutes to run some task.
* @default 30
*/
@Env('N8N_MY_FEATURE_TASK_INTERVAL')
taskInterval: number = 30;
}
```
Config-level decorators to be aware of:
- `@Env()` to define environment variables
- `@Config()` to register a config class and make it usable by the dependency injection container
## CLI commands
Occasionally, a module may need to define a module-specific CLI command. To do so, set up a `.command.ts` file and use the `@Command()` decorator. Currently there are no module-specific commands, so use any of the existing global CLI commands at `packages/cli/src/commands` as reference.
## Tests
Place unit and integration tests for a backend module at `packages/cli/src/modules/{featureName}/__tests__`. Use the `.test.ts` infix.
Currently, testing utilities live partly at `cli` and partly at `@n8n/backend-test-utils`. In future, all testing utilities will be moved to common packages, to make modules more decoupled from `cli`.
## Future work
1. A few aspects of modules continue to be defined outside a module's dir:
- Add a license flag to `LICENSE_FEATURES` at `packages/@n8n/constants/src/index.ts`
- Add a logging scope to `LOG_SCOPES` at `packages/cli/src/logging.config.ts`
- Add a license check to `LicenseState` at `packages/@n8n/backend-common/src/license-state.ts`
- Add a migration (as discussed above) at `packages/@n8n/db/src/migrations`
- Add request payload validation using `zod` at `@n8n/api-types`
- Add a module to default modules at `packages/@n8n/backend-common/src/modules/module-registry.ts`
2. License events (e.g. expiration) currently do not trigger module shutdown or initialization at runtime.
3. Some core functionality is yet to be moved from `cli` into common packages. This is not a blocker for module adoption, but this is desirable so that (a) modules become decoupled from `cli` in the long term, and (b) future external extensions can access some of that functionality.
4. Existing features that are not modules (e.g. LDAP) should be turned into modules over time.
## FAQs
- **What is a good example of a backend module?** Our first backend module is the `insights` module at `packages/@n8n/modules/insights`.
- **My feature is already a separate _package_ at `packages/@n8n/{feature}`. How does this work with modules?** If your feature is already fully decoupled from `cli`, or if you know in advance that your feature will have zero dependencies on `cli`, then you already stand to gain most of the benefits of modularity. In this case, you can add a thin module to `cli` containing an entrypoint to your feature imported from your package, so that your feature is loaded only when needed.
- **Does all new functionality need to be added as a module?** If your feature relies heavily on internals, e.g. workflow archival, then a module may not be a good fit. Consider a module first, but use your best judgment. Reach out if unsure.
- **Are backend modules meant for use by external contributors?** No, they are meant for features developed by the core team.
- **How do I hot reload a module?** Modules are part of `cli` so you can use the usual `watch` command.
- **How do modules interoperate with each other?** This is not supported at this time. Reach out if you need this.
- **I have a use case that is not covered by modules. What should I do?** Modules live in `cli` so any imports from `cli` remain available, i.e. aim for decoupling but do not consider it a blocker for progress. Reach out if you think the module system needs expanding.
|