forRootAsync breaking when passing named Connection
kikar opened this issue · comments
I'm submitting a...
[ ] Regression
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
When using a name
parameter in forRootAsync, it doens't register the provider correctly
Expected behavior
Should register a connection provider correctly with the right name
Minimal reproduction of the problem with instructions
import { Module } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [
TypeOrmModule.forRootAsync({
useFactory: () => ({
name: 'replica',
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
database: 'test',
entities: [],
synchronize: false,
}),
}),
],
controllers: [],
providers: [],
})
export class AppModule {}
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.close();
}
bootstrap();
Throws the following error:
(node:75307) UnhandledPromiseRejectionWarning: Error: Nest could not find replicaConnection element (this provider does not exist in the current context)
at InstanceLinksHost.get (.../node_modules/@nestjs/core/injector/instance-links-host.js:18:19)
at Object.find (.../node_modules/@nestjs/core/injector/module-ref.js:38:55)
at Object.get (.../node_modules/@nestjs/core/injector/module.js:339:28)
at TypeOrmCoreModule.<anonymous> (.../node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:95:47)
at Generator.next (<anonymous>)
at .../node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:20:71
at new Promise (<anonymous>)
at __awaiter (.../node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:16:12)
at TypeOrmCoreModule.onApplicationShutdown (.../node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:91:16)
at Object.callAppShutdownHook (.../node_modules/@nestjs/core/hooks/on-app-shutdown.hook.js:51:35)
What is the motivation / use case for changing the behavior?
I need to set multiple connections, and to do that I need then to be named.
If I use the forRoot
method, then it works.
Environment
Nest version: 7.5.1
Nest Typeorm version: 7.1.5
For Tooling issues:
- Node version: 14.15.5
- Platform: MacOS 10.15.7
You need to have the name
property on the same level as the useFactory
and inside the options for the connection. This is due to how the nest injection tokens work and how TypeORM is a different system under the hood.
Ohh that works. Drove me crazy for a while. Perhaps the docs should point that out
Open PR for it here
Thank you!