I am trying to inject TypeORM
data source into repository class using tsyringe
DI library, node
+ express
, like this:
const PostgresDataSource = new DataSource({
type: "postgres",
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});
container.register("DataSource", {
useValue: PostgresDataSource,
});
@AutoInjectable()
export class TypeormExampleRepository implements ExampleRepository {
private readonly repository: Repository<Entity>;
constructor(@inject("DataSource") private readonly dataSource: DataSource) {
this.repository = PostgresDataSource.getRepository(Entity);
}
...
}
But I constantly get Attempted to resolve unregistered dependency token: "DataSource"
error message. How this dependency injection should be executed?