question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I use tsyringe for dependency injection?

See original GitHub issue

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
FreakyBytescommented, May 11, 2020

Sure thing @fhenriques-meli !

TypeORM provides a mechanism to use an arbitrary Dependency Injection Container via useContainer. However, TypeORM expects the DI container to have a get function - TSyringe has named it resolve, hence the thin wrapper in useContainer.

Another caveat is, that TypeORM does not register anything with the DI container. (it only expects a get function) TSyringe now has the caveat of rolling with unknown injection tokens. As long as they are classes, the DI container will just create an instance. Well, this is really bad in case of TypeORM’s ConnectionManager, since this is the mechanism for TypeORM to check if a connection is available. Now, if TSyringe creates a new ConnectionManager every time TypeORM asks - no connection will ever be available (to the rest of the application). To counteract that, we manually register a Singleton Factory for it.

TL;DR: just call container.register with a singleton factory for the ConnectionManager and useContainer before creating any TypeORM connections 😃

Hope this clears things up!

4reactions
FreakyBytescommented, May 6, 2020

Found this issue via Google. I’ve got TypeORM working with TSyringe with the following snippet:

import { container, instanceCachingFactory } from 'tsyringe';
import { ConnectionManager, createConnections, useContainer } from 'typeorm';

// Register TypeORM's connection manager as singleton
// TypeORM does not register anything itself with TSyringe and therefore would get a new ConnectionManager every time
container.register<ConnectionManager>(ConnectionManager, { useFactory: instanceCachingFactory(() => new ConnectionManager()) });
// configure TypeORM to use a dependency injection container
useContainer(
  // wrapper because TypeORM expects `get` function from IoC container
  { get: someClass => container.resolve(someClass as any) },
);
const connections = await createConnections(options);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency Injection in Typescript with tsyringe
When using dependency injection, classes can be provided their dependencies through a constructor, and those dependencies can be swapped out ...
Read more >
microsoft/tsyringe: Lightweight dependency injection container ...
Class decorator factory that allows the class' dependencies to be injected at runtime. TSyringe relies on several decorators in order to collect metadata...
Read more >
Using tsyringe for dependency injection without using the ...
I'm working on a tiny toy app that doesn't use class based syntax though, it rather comprises dozens of tiny small functions, 5-10...
Read more >
How to use the tsyringe.injectable function in tsyringe - Snyk
To help you get started, we've selected a few tsyringe.injectable ... export default class KeymapUseCase { constructor( @inject('KeymapRepository') private ...
Read more >
Dependency Injection in TypeScript from scratch using TSyringe
TSyringe. A lightweight dependency injection container for TypeScript/JavaScript for constructor injection. In this post, I will show how to add ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found