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.

NestJS import "forRootAsync" only first one

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

@Module({
  providers: [
    DatabaseService,
  ],
  exports: [
    DatabaseService,
  ],
  imports: [
    TypeOrmModule.forRootAsync({
      useFactory: async (config: ConfigService) => {
        const options = config.get('database.databases.email');

        options.name = 'default';
        options.entities = [__dirname + '/../app/entities/*.entity{.ts,.js}'];

        return options;
      },
      inject: [ConfigService],
    }),
    TypeOrmModule.forRootAsync({
      useFactory: async (config: ConfigService) => {
        console.log('I AM IGNORED');

        const options = config.get('database.databases.app');

        options.name = 'app';
        options.entities = [__dirname + '/../app/entities/*.entity{.ts,.js}'];

        return options;
      },
      inject: [ConfigService],
    }),
  ],
})

Expected behavior

Nest only load’s first import. The second one is ignored. The module should load all async modules.

BTW. Maybe it would be better if module can accept array of connection options?

Environment


Nest version: 5.3.7

 
For Tooling issues:
- Node version: v9.4.0
- Platform: Mac

Others:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
kamilmysliwieccommented, Sep 20, 2018

See that name: https://github.com/nestjs/typeorm/blob/master/lib/interfaces/typeorm-options.interface.ts should be passed at the same level as useFactory in case of the multiple connections.

 TypeOrmModule.forRootAsync({
      name: 'gameServerMainConnection',
      useFactory: (config: ConfigService) => ({
        ...
        type: 'mysql',
        host: '127.0.0.1',
     }),
})
1reaction
acepacecommented, Apr 9, 2020

The documentation seems to be wrong as the result of useFactory is ignored if there is a config named default.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic modules | NestJS - A progressive Node.js framework
First, we'll define a UsersModule to provide and export a UsersService . ... In other words, dynamic modules provide an API for importing...
Read more >
Nest js convert module from forRoot to forRootAsync
First you need to create a new file. For example auth.module-definition.ts . Now in this file we need to create ConfigurableModuleBuilder .
Read more >
Creating Dynamic Modules in Nest JS Part-1
In this case, we're going to accept a simple options object with suitable properties, which is the typical case. Lets have a look...
Read more >
Database | NestJS - A progressive Node.js framework - Netlify
import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm' ... One-to-one, Every row in the primary table has one and...
Read more >
@glcap-forks/typegraphql-nestjs - npm
The first one is TypeGraphQLModule.forRoot() which you should call on your root module, just like with the official GraphQLModule . The only ......
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