Skip to content

Autowire

dannysmc95 edited this page Feb 24, 2022 · 2 revisions

The autowire module is a simple way to automatically register and intialise your controllers, services, tasks, fibres, etc.

The autowire module will scan for .ts files in your /src folder on bootup (if enabled) and will attempt to import, register and initialise them, currently plugins must be registered manually due to the differences in the way they are handled.

Autowire will attempt to load the exported classes from the file, this will consist of loading the default or the first available class exported from the file, once done it will read any metadata on the class to determine if firstly it's an engine module and then it will pass it to the engine's register service which will register it with the engine.

To enable autowiring, simply define autowire: true in your engine config, see below:

import { Engine } from '@symbux/turbo';

const engine = new Engine({
	autowire: true,
	...
});

If you decide not to use autowire please be aware that providers may not be available as they will need to be manually imported.


Scanning specific folders

Sometimes you may not want autowire to scan all folders in your /src folder, to do this, you can define a list of folders to scan in your engine config, and then tell the engine to only scan defined folders, to do this you do:

import { Engine } from '@symbux/turbo';
import { resolve } from 'path';

const engine = new Engine({
	// ...
	autowire: true,
	scanFoldersOnly: true,
	folders: [
		resolve(__dirname, './src/controllers'),
	],
	// ...
});

Please be aware this will also affect the injection modules, because autowire scans all folders in the project, it automatically loads all modules in the /src into memory which will trigger the DI module to pickup and register the modules, if you want to use autowire to only scan a specific folder, you will need to define the providers in the folders list, or import them directly like so:

import './some/path/to/some/provider';

Features

Plugins

Future Plans

Resources

Clone this wiki locally