我有TypeScript應用程序,我正在爲IoC使用Inversify。如何在Inversify中注入異步依賴項?
我有一個連接類:
'use strict';
import { injectable } from 'inversify';
import { createConnection, Connection } from "typeorm";
import { Photo, PhotoMetadata, Author, Album } from '../index';
@injectable()
class DBConnectionManager {
public createPGConnection(): Promise<Connection> {
return createConnection({
driver: {
type: "postgres",
host: "host",
port: 5432,
username: "username",
password: "password",
database: "username"
},
entities: [
Photo, PhotoMetadata, Author, Album
],
autoSchemaSync: true,
});
}
}
export { DBConnectionManager };
我創建了連接後,我想連接到我的容器綁定:
kernel.bind<Connection>('DefaultConnection').toConstantValue(getConnectionManager().get());
,然後我想將它注入另一個類:
import { injectable, inject } from 'inversify';
import { Connection, FindOptions } from "typeorm";
import { IGenericRepository, ObjectType } from '../index';
@injectable()
class GenericRepository<T> implements IGenericRepository<T> {
private connection: Connection;
private type: ObjectType<T>;
constructor(@inject('DefaultConnection') connection: Connection) {
this.connection = connection;
}
所以在我的容器配置中,如何綁定需要等待的DefaultConnection用於創建連接 我可以做異步等待,但我不知道是否有在inversify