Can't configure multiple databases using TypeOrmModule.forRootAsync
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
Can't configure multiple databases using TypeOrmModule.forRootAsync
Expected behavior
Can configure multipla databases with forRootAsync
Minimal reproduction of the problem with instructions
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from '../config/config.module';
import { ConfigService } from '../config/config.service';
import { Test1 } from '../feature/internal/test1.entity';
import { Test2 } from "../feature/internal/test2.entity";
@Module({
imports: [
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
type: 'mysql',
host: configService.frMysqlHost,
port: 3306,
username: configService.frMysqlUsername,
password: configService.frMysqlPass,
database: 'database',
entities: [Test1],
synchronize: false,
logging: true,
name: 'connection1'
}),
inject: [ConfigService]
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
type: 'sqlite',
database: configService.sqlDb,
entities: [Test2],
synchronize: true,
logging: true,
name: 'connection2'
})
}),
TypeOrmModule.forFeature([Test2], 'connection2')
]
})
export class AppModule {}
Error: Nest can't resolve dependencies of the Test2Repository (?). Please make sure that the argument at index [0] is available in the TypeOrmModule context.
at Injector.lookupComponentInExports (/Users/xxx/demo/node_modules/@nestjs/core/injector/injector.js:144:19)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
at Function.Module.runMain (module.js:695:11)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
1: node::Abort() [/Users/xxx/.nvm/versions/node/v8.11.1/bin/node]
2: node::Chdir(v8::FunctionCallbackInfo<v8::Value> const&) [/Users/xxx/.nvm/versions/node/v8.11.1/bin/node]
3: v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) [/Users/xxx/.nvm/versions/node/v8.11.1/bin/node]
4: v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, v8::internal::BuiltinArguments) [/Users/xxx/.nvm/versions/node/v8.11.1/bin/node]
5: v8::internal::Builtin_Impl_HandleApiCall(v8::internal::BuiltinArguments, v8::internal::Isolate*) [/Users/xxx/.nvm/versions/node/v8.11.1/bin/node]
6: 0x1ddb44842fd
If I change the second connection code to just use forRoot everything works fine. But then I can't use config service
What is the motivation / use case for changing the behavior?
I would like to configure more than 1 connection usign a configuration service
Environment
Nest version: 5.5.0
For Tooling issues:
- Node version: 8.11.1 & 10.5.0
- Platform: Mac
Others:
Please, provide a minimal repository which reproduces your issue.
@miguelchico
You have to set connection name outside useFactory. Immediately after:
imports: [ConfigModule],
name: 'connection2'
@SevenWaysEO You are completely right!
@kamilmysliwiec do you think the documentation can be improved to avoid this mistake in the future?
Thank you both
@miguelchico you're right. I'd suggest creating an issue in this repo :)
@miguelchico You have to set connection name outside useFactory. Immediately after:
imports: [ConfigModule], name: 'connection2'
coool
This should be documented in NestJs's Documentation
This should be documented in NestJs's Documentation
@alfredoPacheco do you want to create a PR for it?