How do I use tsyringe for dependency injection?
See original GitHub issueIssue 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:
- Created 4 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 aget
function - TSyringe has named itresolve
, hence the thin wrapper inuseContainer
.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 newConnectionManager
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 theConnectionManager
anduseContainer
before creating any TypeORM connections 😃Hope this clears things up!
Found this issue via Google. I’ve got TypeORM working with TSyringe with the following snippet: