typeorm-typedi-extensions icon indicating copy to clipboard operation
typeorm-typedi-extensions copied to clipboard

fix: Using Typeorm DataSource and Typegraphql: Cannot get Connection with name \"default\" from the ConnectionManager.

Open nelsonfleig opened this issue 2 years ago • 6 comments

Description

Using Typeorm's new way of connecting to a data source with new Typeorm.DataSource({...}) , we cannot access the global typeorm connection through the typedi container. I'm using Typegraphql and when I run a query I get the response error:

Cannot get Connection with name \"default\" from the ConnectionManager. Make sure you have created the connection and called \"useContainer(Container)\" in your application before establishing a connection and importing any entity into TypeORM.

It only works if we create the connection using the now deprecated Typeorm.createConnection.

As a side note, Typeorm has deprecated all container-related features in 0.3.0. How will this affect the future of this package and Typedi as a whole?

Minimal code-snippet showcasing the problem

import * as TypeGraphQL from 'type-graphql';
import { Container } from 'typeorm-typedi-extensions';

TypeORM.useContainer(Container);

export const AppDataSource = new DataSource(typeormOptions); 
await AppDataSource.initialize(); // this will establish the connection, but Typegraphql can't find the connection

// await TypeORM.createConnection(typeormOptions); // this still works however

const schema = await TypeGraphQL.buildSchema({
   ...
   container: Container,
});

Expected behavior

I expected to my query to be able to find the injected Repository and Connection and run the query.

Actual behavior

Typegraphql sends back the response: Cannot get Connection with name \"default\" from the ConnectionManager. Make sure you have created the connection and called \"useContainer(Container)\" in your application before establishing a connection and importing any entity into TypeORM.

nelsonfleig avatar Mar 27 '22 17:03 nelsonfleig

@nelsonfleig I'm just going through the process of updating (or at least considering updating) typeorm to the newest version with all of the depreciations you're talking about.

Have you had any luck working around (possibly with a fork, etc) the issues you mentioned?

kevin-mitchell avatar Apr 26 '22 14:04 kevin-mitchell

I hate breaking changes 😭

JefferyHus avatar May 17 '22 23:05 JefferyHus

Hey someone fix that shit ?

alexisvisco avatar Jun 12 '22 15:06 alexisvisco

This is so problematic. I'm stuck with updating to new Version which has deprecated containers

paramsinghvc avatar Jun 26 '22 13:06 paramsinghvc

Hey, anyone has fixed this issue? I'm also stuck on this. Please help.

yugank-zangula avatar Nov 07 '22 11:11 yugank-zangula

Same here 👎🏻

sciotta avatar Dec 20 '22 00:12 sciotta