typedi: question: passing Container to TypeORM useContainer fails

Description

registering Container with typeorm’s useContainer does not work anymore as of the latest release 0.9.0 It throws the following error which is fine for services

image

Minimal code-snippet showcasing the problem

import { Container } from 'typedi';
import {  useContainer } from 'typeorm';

....
useContainer(Container);

Unless I am missing something, this should not fail.

Expected behavior

It used to work fine. This has stopped working as of 0.9.0.

Actual behavior

It should not fail on passing typedi Container to typeorm

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 15
  • Comments: 27 (8 by maintainers)

Most upvoted comments

In typeorm-typedi-extensions@0.4.1 has been added custom Container to register with TypeORM. So changing from:

import { Container } from "typedi";
import { useContainer } from "typeorm";
useContainer(Container);

to

import { Container } from "typeorm-typedi-extensions";
import { useContainer } from "typeorm";
useContainer(Container);

helps.

For now I have downgraded to typedi@0.8.0 and it’s working.

@danbeneventano here is a sample repository with steps to set it up and run it. it should fail because it cant inject the proper service.

I can confirm decorating all classes that fail with @Service() fix then issue. Even though it seems unnecessary, it’s good enough for now.

Just incase this helps someone. At the time of writing, I was experiencing the same issue while using the latest versions of the following dependencies. I was able to leave TypeOrm at its latest and simply downgraded typedi and typeorm-typedi-extensions to the versions listed below. Maybe this will help someone.

In my index.js (entry point) file.

... useContainer(Container); ...

"typedi": "^0.8.0", // downgraded
"typeorm": "^0.2.37", // curent latest
"typeorm-typedi-extensions": "^0.2.3", // downgraded

Working

Can you provide a snippet of the code that got it working?

You need to add the @Service decorator from TypeDI to every class you ever request from the container via Container.get().