question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't configure multiple databases using TypeOrmModule.forRootAsync

See original GitHub issue

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:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

22reactions
SevenWaysEOcommented, Mar 26, 2019

@miguelchico You have to set connection name outside useFactory. Immediately after: imports: [ConfigModule], name: 'connection2'

0reactions
ateeqrehmanicommented, Jun 20, 2022

what is the solution for “forRoot” for the same issue ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

NestJS + TypeORM: Use two or more databases?
I just tried setting up TypeORM with multiple databases and a ormconfig.json and it did not work for me at all. It seemed...
Read more >
NestJS Multiple DB Setup with TypeORM - Level Up Coding
In this post, I will describe how to setup and use multiple database connections with a simple example. NestJS Documentation is great for...
Read more >
Database | NestJS - A progressive Node.js framework
In this case, use the forRootAsync() method, which provides several ways to deal with async configuration. One approach is to use a factory...
Read more >
Dynamic databases connections with NestJS - Jnesis
How to handle multiple database connections with NestJS using a dynamic configuration pattern which does not require any codebase change.
Read more >
Multiple Databases | Nestjs-query - Blog
This section will walk you through a short example indicating how to connect your ... Then setup multiple database connections. ... TypeOrmModule.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found