Releases: tsedio/tsed
Releases · tsedio/tsed
v5.27.5
v5.27.4
v5.27.3
v5.27.2
v5.27.1
v5.27.0
5.27.0 (2019-08-20)
Features
- di: Add isAsync method and useAsyncFactory in Provider class (f3bb044)
- di: Support async provider with InjectorService.loadAsync() (ce9fc8b), closes #547
Provider.useFactory
The useAsyncFactory
is a way of creating asynchronous providers dynamically.
The actual provider will be equal to a returned value of the factory function.
The factory function can either depend on several different providers or stay completely independent.
It means that factory may accept arguments, that DI will resolve and pass during the instantiation process.
Example:
import {ServerSettingsService} from "@tsed/common";
import {registerProvider} from "@tsed/di";
import {DatabaseConnection} from "connection-lib";
export const CONNECTION = Symbol.for("CONNECTION");
registerProvider({
provide: CONNECTION,
deps: [ServerSettingsService],
async useAsyncFactory(settings: ServerSettingsService) {
const options = settings.get("myOptions");
const connection = new DatabaseConnection(options);
await connection.connect();
return connection;
}
});
In order to inject custom provider, we use the Inject
decorator. This decorator takes a single argument - the token.
import {Inject, Injectable} from "@tsed/common";
import {CONNECTION} from "./connection";
@Injectable()
export class MyService {
constructor(@Inject(CONNECTION) connection: any) {
}
}