Skip to content

Injections

Danny Springett-McHugh edited this page Oct 9, 2020 · 2 revisions

Injections.

Injections are for injecting controllers and services with models and providers, and as time goes on possibly more. As of now only controllers and services can be injected, and they can only be injected with a model or a provider or many of each/both. You use the @Inject decorator to do this and you can either pass a string or an array of strings, each string is the name of the model or provider, please note as of now, there is no way to map injections, therefore if you have a two models or providers with the same name, they will clash.

For type hinting support you will need to import your model and then assign it.

See example below:

import { Controller, AbstractController, Route, Inject, IReturn } from 'rewyre';
import { UsersModel } from '../model/users';

@Inject('users')
@Controller('/', 'hello')
export class HelloController extends AbstractController {

	// Note we use the TS ! operator to note it WILL be this model.
	protected users!: UsersModel;

	@Route('GET', '/')
	public async index(): Promise<IReturn> {
		const users: any = await this.users.find({});
		return { status: 200, content: users };
	}
}

Useful Links:

Clone this wiki locally